MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Map[] and multiple args function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg96244] Re: Map[] and multiple args function
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Tue, 10 Feb 2009 05:50:09 -0500 (EST)
  • Organization: The Open University, Milton Keynes, UK
  • References: <gmp0ps$c0a$1@smc.vnet.net>

In article <gmp0ps$c0a$1 at smc.vnet.net>, sagrailo 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?

Since you are mapping your function over a list of lists, it is wise to 
define the function as accepting a list of triplets so you do not have 
to use the transformation rule with the Sequence[]. (Note you can have 
multiple definitions for the function, so you may keep the original 
definition if you need it.) To get the sum of the component of a vector, 
you can use Total[]. For instance,

In[1]:= Foo[{x_, y_, z_}] := x + 2*y + 3*z

In[2]:= Plus @@ Foo /@ {{1, 2, 3}, {4, 5, 6}}

Out[2]= 46

In[3]:= Total[Foo /@ {{1, 2, 3}, {4, 5, 6}}]

Out[3]= 46

In[4]:= Foo /@ {{1, 2, 3}, {4, 5, 6}} // Total

Out[4]= 46

Regards,
--Jean-Marc


  • Prev by Date: Re: Data interpolation
  • Next by Date: Re: Map[] and multiple args function
  • Previous by thread: Re: Map[] and multiple args function
  • Next by thread: Re: Re: Map[] and multiple args function