Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Suppose I have an array

p = {a,b,c,d}

and a function f that takes a variable number of arguments. I want to evaluate

f[a,b,c,d]

It won't do to type

f[p]

because this returns the array {f[a],f[b],f[c],f[d]} which is not at all the right thing.

How do I get f to accept the elements of p (as opposed to p itself) as arguments?

Edited to add: Per a request in comments, here is a concrete example. Suppose p={2,3,4}. I would like an expression that returns Multinomial[2,3,4], which is to say 1260. It doesn't work to type Multinomial[p], because this gives {Multinomial[2],Multinomial[3],Multinomial[4]}={1,1,1}, which is not at all the same as 1260.

share|improve this question
    
Try evaluating p = {p[1], p[2]}, you won't be able to because it will start an endless recursion that will be capped by the recursion limit. –  Pickett Mar 7 at 1:05
    
Please provide a more concrete example, including the definition of the function 'f'. It is unclear at this point what you're looking for. –  rasher Mar 7 at 1:05
    
Pickett: Sorry; I chose a bad name for the array. I'm editing to fix this. The problem remains. –  WillO Mar 7 at 1:06
    
@rasher: How about taking f=Multinomial ? –  WillO Mar 7 at 1:07
    
@WillO: Please, edit your question to include the function, and what it does vs what you expect (or need). Using f=Multinomial against your updated p definition returns precisely what's expected... –  rasher Mar 7 at 1:11
show 1 more comment

2 Answers 2

up vote 2 down vote accepted

This is really easy if you understand the internal form of {a,b,c,d}. Let's look at it:

p={a,b,c,d};
FullForm[p]
(* List[a,b,c,d] *)

as you see what you want is not really far away because basically, you only need to replace List with f. This is exactly what Apply (or as operator @@) does:

f @@ p
(* f[a, b, c, d] *)
share|improve this answer
    
This is crystal clear. All I asked for was a solution, but you gave me both a solution and an understanding, so thank you doubly. –  WillO Mar 7 at 1:21
    
@ halirutan Isn't is nice when someone ask a question about something that Mathematica was beautifully designed to do. –  George Wolfe Mar 7 at 2:17
    
@GeorgeWolfe Indeed and it is even nicer for the user, because it lets him understand some of the mystifying operators like @@ or @@@ very naturally. –  halirutan Mar 7 at 2:22
add comment

You question is not very clear.

a = {p[1], p[2], p[3]}

f[x__] := 2 x

f[a]

(* or *)

f /@ a

(* or *)

Thread[f[a]]

yields, e.g. {2 p[1], 2 p[2], 2 p[3]}

Note also I used a for the vector, else recursion...

If you just want to transmute the vector into an argument list:

f[Sequence @@ p]
share|improve this answer
    
I am sorry the question wasn't clear. I don't think your answer helps with the intended question. What I want to get is f[p[1],p[2],p[3]], not {f[p[1],f[p[2]],f[p[3]}. (I've changed the p[1] etc to a,b,c in the question statement.) –  WillO Mar 7 at 1:10
    
@WillO: see latter part of answer –  rasher Mar 7 at 1:13
    
That works! Thank you. –  WillO Mar 7 at 1:17
    
@WillO: Good, note you can also use Apply (or f@@ as shorthand) to do similar, if it fits with your definitions. –  rasher Mar 7 at 1:19
    
Thank you again, rasher. –  WillO Mar 7 at 1:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.