Re: Map[] and multiple args function
- To: mathgroup at smc.vnet.net
- Subject: [mg96357] Re: Map[] and multiple args function
- From: Yannan <lumeng at gmail.com>
- Date: Thu, 12 Feb 2009 06:36:29 -0500 (EST)
- References: <gmp0ps$c0a$1@smc.vnet.net>
In[10]:= SetAttributes[Foo, Listable]
Foo[x_, y_, z_] := x + 2*y + 3*z
Foo[{1, 1, 1}, {2, 2, 2}, {3, 3, 3}]
Foo[{1, 2, 3}, {2, 3, 4}, {4, 5, 6}]
Out[12]= {14, 14, 14}
Out[13]= {17, 23, 29}
On Feb 9, 4:33 am, sagra... at gmail.com wrote:
> I know this has to be FAQ, but I'm just not able to find an answer
> anywhere, so - here it goes: I have a multiple-arguments function,
> say something like:
> Foo[x_, y_, z_] := x + 2*y + 3*z
> Now, I want to call this function number of times, over different sets
> of arguments, and then to sum results. I thought about storing
> argument triplets into some kind of list, and then employing sequence
> of Map[] and Apply[] to get the job done. After number of trials and
> errors, I came up with following piece of code doing the thing:
> Plus @@ Foo /@ {{1, 2, 3}, {4, 5, 6}} /. List -> Sequence
> but I even don't fully understand what's going here, and also I'm
> wondering is there a "better" way to accomplish this. So - any
> suggestion?
>
> Thanks.