Re: multiple variables in pure function used in map.

• To: mathgroup at smc.vnet.net
• Subject: [mg107634] Re: [mg107632] multiple variables in pure function used in map.
• From: Leonid Shifrin <lshifr at gmail.com>
• Date: Sun, 21 Feb 2010 04:22:55 -0500 (EST)
• References: <201002201138.GAA13609@smc.vnet.net>

```Hi,

what you need is to Apply your function (Plus here) to level 1 of your
expression:

In[1]:= ab = {{1, 2}, {2, 3}, {3, 4}};

The  shorthand is @@@:

In[2]:= (#1 + #2) & @@@ ab

Out[2]= {3, 5, 7}

You can also use Plus, which has the advantage that you don't need to know
the size of your sublists - they may even have different sizes.

In[3]:= Plus @@@ ab

Out[3]= {3, 5, 7}

Without the shorthand, it looks like:

In[4]:= Apply[(#1 + #2) &, ab, 1]

Out[4]= {3, 5, 7}

In[5]:= Apply[Plus, ab, 1]

Out[5]= {3, 5, 7}

You can also use Map:

In[6]:= Map[Apply[(#1 + #2) &, #] &, ab]

Out[6]= {3, 5, 7}

In[7]:= Map[Apply[Plus, #] &, ab]

Out[7]= {3, 5, 7}

In your particular case, it  will probably most efficient  to  use Total
with level specification:

In[8]:= Total[ab, {2}]

Out[8]= {3, 5, 7}

Hope this helps.

Regards,
Leonid

On Sat, Feb 20, 2010 at 3:38 AM, pipehappy <pipehappy at gmail.com> wrote:

> Hi, All
>
> Is there a way to use multiple variables in pure function with map.
>
> What I want to do is like this:
>
> ab = {{1, 2}, {2, 3}, {3, 4}};
> (#[[1]] + #[[2]]) & /@ ab
> {3, 5, 7}
>
> Instead of refer to elements in list I want to use multiple variables
> in pure function.
>
> something like this:
> ab = {{1, 2}, {2, 3}, {3, 4}};
> (#1 + #2) & /@ ab
> To do the same thing as the above example.
>
> Best
>
> -- pipehappy
>
>

```

• Prev by Date: Re: multiple variables in pure function used in map.
• Next by Date: Re: Matching FillingStyle to PlotStyle
• Previous by thread: Re: multiple variables in pure function used in map.
• Next by thread: Re: multiple variables in pure function used in map.