Evaluate a list

I have a function f(x1,⋯,xn)f(x_1,\cdots,x_n) that takes nn arguments, and I would like to evaluate it using a list of lists {{x1,⋯,xn},⋯,{x′1,⋯,x′n},⋯}\lbrace\lbrace x_1,\cdots,x_n \rbrace,\cdots,\lbrace x_1′,\cdots,x_n’ \rbrace,\cdots \rbrace .
The problem is that when I define


I can’t do


Because mathematica reads f({x1,⋯,xn})f(\lbrace x_1,\cdots,x_n\rbrace) instead of f(x1,⋯,xn)f(x_1,\cdots,x_n)…



2 Answers


You say that func[{1,2,3}] doesn’t work, but in Mathematica we can actually define such functions. We just have to write

func[{a_, b_, c_}] := a^2 + b^2 + c^2;

and boom, now it works. Now you can do

func /@ list



The latter works because of something called “listability”. You can also use that with your original definition of func, using Apply like so: func@@Transpose@list, but there is no advantage to using Apply in this manner rather than the way in which Szalbocs used it.

By the way Map or /@ as it is also written is what you should be using instead of Table[var[[i]],{i,…}]. To begin with you can think of it as the equivalent of that, but with a simpler syntax.

Use Apply. The following will work:

func @@@ list


In[1]:= f @@ {1, 2, 3}
Out[1]= f[1, 2, 3]

In[2]:= f @@@ {{1, 2, 3}, {4, 5, 6}}
Out[2]= {f[1, 2, 3], f[4, 5, 6]}