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
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
<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
Outer[#2@#1 &, {a, b, c}, {f, g}]
hth,
albert
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]}}
>
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]}}
In[67]:= Through[{f,g}[#]]&/@{a,b,c}
Out[67]= {{f[a],g[a]},{f[b],g[b]},{f[c],g[c]}}
Adriano Pascoletti
Hello
One way of doint this would be:
Outer[#2[#1] & , {a, b, c}, {f, g}]
Best Regards
Norbert Marxer
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
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