Re: Applying a list of 2D paramters to a mathematica function
- To: mathgroup at smc.vnet.net
- Subject: [mg68066] Re: Applying a list of 2D paramters to a mathematica function
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Fri, 21 Jul 2006 17:36:03 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <e9q9h8$829$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Curry Taylor wrote:
> I have (for simplicity) something like this:
>
> f[x_, y_] := x+y;
>
> I want to use this function for a variety of parameters in a list:
>
> indices = {{a,b}, {c,d}, {e,f}, ..}
>
> so that I get
>
> {a+b, c+d, e+f, ..}
>
> Doing the intuitive thing (for me)
>
> f /@ indices
>
> results in
>
> {f[{a, b}], f[{c, d}], f[{e, f}], ..}
>
> but what I want is
>
> {f[a, b], f[c, d], f[e, f], ..}
>
> and that's not what I'm getting. I have tried using Flatten, pure
> functions (Slot # and &), different Map functions, nesting at parameters
> (like {1} and {2}), and other things but I just can't seem to get what I'm
> after. Any help please?
>
> Thank you,
>
> Curry
>
What about the following?
Let
indices = {{a, b}, {c, d}, {e, f}}
Then we define f as you did previously
f[x_, y_] := x + y;
Now, the following pure function
(f[#1[[1]], #1[[2]]] & ) /@ indices
returns what you wanted
{a + b, c + d, e + f}
We could also defined f as taking a list of two arguments
f[{x_, y_}] := x + y;
Then
f /@ indices
works as expected
{a + b, c + d, e + f}
as well as Thread
Thread[f[Transpose[indices]]]
which returns
{a + b, c + d, e + f}
HTH,
Jean-Marc