Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

"vector" Map[] / functional outer product?

7 views
Skip to first unread message

Mitch Murphy

unread,
Nov 29, 2007, 6:48:10 AM11/29/07
to

greetings,

is there a simpler way to express

{f@#,g@#}& /@ {a,b,c}

-> {{f[a],g[a]},{f[b],g[b]},{f[c],g[c]}}

ie. is there some mathematica function ??? such that

{f,g} ??? {a,b,c}

-> {{f[a],g[a]},{f[b],g[b]},{f[c],g[c]}}

you might be asking what's so difficult about the "@#,@#,... & /@"
syntax, but what about when you don't have two functions f,g but 8-10
functions... the syntax gets ugly fast and hard to read.

note that

Outer[{f, g}, {a, b, c}]

-> {{f, g}[a], {f, g}[b], {f, g}[c]}

doesn't work, neither does

Outer[Apply, {f, g}, {a, b, c}]

->{{a, b, c},{a, b, c}}


thanks,
Mitch

wye...@gmail.com

unread,
Nov 30, 2007, 5:38:57 AM11/30/07
to
On 11=D4=C229=C8=D5, =CF=C2=CE=E77=CA=B148=B7=D6, Mitch Murphy <mi...@lemma.=

Hi Mitch,
is this what you are looking for?

In[1]:=
Transpose[ ( #1 /@ { x1, x2, x3 } &) /@ { func1, func2, func3 } ]
Out[1]=
{
{ func1[x1], func2[x1], func3[x1] },
{ func1[x2], func2[x2], func3[x2] },
{ func1[x3], func2[x3], func3[x3] }
}

hope this helps, wyelen

Jean-Marc Gulliet

unread,
Nov 30, 2007, 5:41:00 AM11/30/07
to
Mitch Murphy wrote:
> greetings,
>
> is there a simpler way to express
>
> {f@#,g@#}& /@ {a,b,c}
>
> -> {{f[a],g[a]},{f[b],g[b]},{f[c],g[c]}}
>
> ie. is there some mathematica function ??? such that
>
> {f,g} ??? {a,b,c}
>
> -> {{f[a],g[a]},{f[b],g[b]},{f[c],g[c]}}

<snip>

The following expression (In[1]) will return the desired list without
the burden of passing explicitly any arguments to your functions.

In[1]:= Outer[Compose, {f, g}, {a, b, c}] // Transpose

Out[1]= {{f[a], g[a]}, {f[b], g[b]}, {f[c], g[c]}}

Note that if you want a nice infix notation, you could add some meaning
to a symbol without any built-in meaning, such as CirclePlus (On the
keyboard, you can enter the symbol CirclePlus as Esc c+ Esc --- i.e.
escape key followed by c followed by + followed by the escape key.)

In[2]:= CirclePlus[f_, a_] := Transpose[Outer[Compose, f, a]]

In[3]:= {f, g}\[CirclePlus]{a, b, c}

Out[3]= {{f[a], g[a]}, {f[b], g[b]}, {f[c], g[c]}}

Regards,
--
Jean-Marc

Albert Retey

unread,
Nov 30, 2007, 5:42:02 AM11/30/07
to
Hi,

Outer[#2@#1 &, {a, b, c}, {f, g}]

hth,

albert

Mitch Murphy

unread,
Nov 30, 2007, 5:52:12 AM11/30/07
to

based on the solution from Jean-Marc Gulliet, here's a slight
improvement based on overloading Map[] for List arguments. this solves
my issue perfectly without having to type in esc sequences for infix
operators

Unprotect[Map];
Map[f_List, a_List] := Transpose@Outer[Compose, f, a]
Protect[Map];

{f, g} /@ {a, b, c}

-> {{f[a],g[a]},{f[b],g[b]},{f[c],g[c]}}


cheers,
Mitch

On Nov 29, 2007, at 09:46, Jean-Marc Gulliet wrote:

> Mitch Murphy wrote:
>> greetings,
>> is there a simpler way to express
>> {f@#,g@#}& /@ {a,b,c}
>> -> {{f[a],g[a]},{f[b],g[b]},{f[c],g[c]}}
>> ie. is there some mathematica function ??? such that
>> {f,g} ??? {a,b,c}
>> -> {{f[a],g[a]},{f[b],g[b]},{f[c],g[c]}}
>

Ray Koopman

unread,
Nov 30, 2007, 5:59:21 AM11/30/07
to
In[1]:= Outer[#2[#1]&,{a,b,c},{f,g}]

Out[1]= {{f[a],g[a]},{f[b],g[b]},{f[c],g[c]}}

In[2]:= Transpose@Outer[#1[#2]&,{f,g},{a,b,c}]

Out[2]= {{f[a],g[a]},{f[b],g[b]},{f[c],g[c]}}

Adriano Pascoletti

unread,
Nov 30, 2007, 6:01:23 AM11/30/07
to

In[67]:= Through[{f,g}[#]]&/@{a,b,c}
Out[67]= {{f[a],g[a]},{f[b],g[b]},{f[c],g[c]}}

Adriano Pascoletti

Norbert Marxer

unread,
Nov 30, 2007, 6:06:28 AM11/30/07
to

Hello

One way of doint this would be:

Outer[#2[#1] & , {a, b, c}, {f, g}]

Best Regards
Norbert Marxer

Szabolcs Horvát

unread,
Nov 30, 2007, 6:11:33 AM11/30/07
to
Mitch Murphy wrote:
> greetings,
>
> is there a simpler way to express
>
> {f@#,g@#}& /@ {a,b,c}
>
> -> {{f[a],g[a]},{f[b],g[b]},{f[c],g[c]}}
>
> ie. is there some mathematica function ??? such that
>
> {f,g} ??? {a,b,c}
>
> -> {{f[a],g[a]},{f[b],g[b]},{f[c],g[c]}}


There is no single function, but there are many ways to do it.

In[6]:= Through /@ Thread[{f, g}[{a, b, c}]]
Out[6]= {{f[a], g[a]}, {f[b], g[b]}, {f[c], g[c]}}

>
> you might be asking what's so difficult about the "@#,@#,... & /@"
> syntax, but what about when you don't have two functions f,g but 8-10
> functions... the syntax gets ugly fast and hard to read.
>
> note that
>
> Outer[{f, g}, {a, b, c}]
>
> -> {{f, g}[a], {f, g}[b], {f, g}[c]}


In[2]:= Outer[{f, g}, {a, b, c}]
Out[2]= {{f, g}[a], {f, g}[b], {f, g}[c]}

In[3]:= Through /@ %
Out[3]= {{f[a], g[a]}, {f[b], g[b]}, {f[c], g[c]}}

>
> doesn't work, neither does
>
> Outer[Apply, {f, g}, {a, b, c}]
>
> ->{{a, b, c},{a, b, c}}
>

In[4]:= Outer[#2[#1] &, {a, b, c}, {f, g}]
Out[4]= {{f[a], g[a]}, {f[b], g[b]}, {f[c], g[c]}}

Apply[] and #1[#2] are not the same thing.

--
Szabolcs

Daniel Lichtblau

unread,
Dec 1, 2007, 5:41:20 AM12/1/07
to
Mitch Murphy wrote:
> based on the solution from Jean-Marc Gulliet, here's a slight
> improvement based on overloading Map[] for List arguments. this solves
> my issue perfectly without having to type in esc sequences for infix
> operators
>
> Unprotect[Map];
> Map[f_List, a_List] := Transpose@Outer[Compose, f, a]
> Protect[Map];
>
> {f, g} /@ {a, b, c}
>
> -> {{f[a],g[a]},{f[b],g[b]},{f[c],g[c]}}
>
>
> cheers,
> Mitch

Map is a heavily used function, in the Mathematica kernel as well as by
users. I would not recommend redefining it unless you have convinced a
BRPOE (Blue Ribbon Panel Of Experts) that your results will improve the
world for generations to come. Generally such redefinition will bite,
and hard, when doing some unrelated computations.

Daniel Lichtblau
Wolfram Research

0 new messages