Grupos de Google ya no admite nuevas publicaciones ni suscripciones de Usenet. El contenido anterior sigue siendo visible.

"vector" Map[] / functional outer product?

Visto 7 veces
Saltar al primer mensaje no leído

Mitch Murphy

no leída,
29 nov 2007, 6:48:1029/11/07
a

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

no leída,
30 nov 2007, 5:38:5730/11/07
a
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

no leída,
30 nov 2007, 5:41:0030/11/07
a
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

no leída,
30 nov 2007, 5:42:0230/11/07
a
Hi,

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

hth,

albert

Mitch Murphy

no leída,
30 nov 2007, 5:52:1230/11/07
a

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

no leída,
30 nov 2007, 5:59:2130/11/07
a
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

no leída,
30 nov 2007, 6:01:2330/11/07
a

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

no leída,
30 nov 2007, 6:06:2830/11/07
a

Hello

One way of doint this would be:

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

Best Regards
Norbert Marxer

Szabolcs Horvát

no leída,
30 nov 2007, 6:11:3330/11/07
a
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

no leída,
1 dic 2007, 5:41:201/12/07
a
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 mensajes nuevos