Re: Loop programming; how do i do this ???
- To: mathgroup at smc.vnet.net
- Subject: [mg96117] Re: Loop programming; how do i do this ???
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Thu, 5 Feb 2009 04:41:14 -0500 (EST)
- References: <gmbr29$igp$1@smc.vnet.net>
On Thu, Feb 5, 2009 at 4:14 AM, naso e-o <naso at live.be> wrote:
> Thankyou very much for your response this does exactly what i want,
>
> though is it possible the test function can receive its arguments as 1 list
> rather than individual elements (as i dont know how i can define a function
> which accepts a variable amount of arguments)
[snip]
Sure: we write test[{##}] & rather than just test as first argument of
Outer. This is a pure function that takes a sequence of arguments ##,
changes this sequence into a list {##}, and passes this list to the
actual function test. So the full expression is
Outer[test[{##}] &, Sequence @@ (cons[[#]] & /@ vars)]
For instance,
In[1]:= vars = {2, 4, 5};
cons = {{a1, b1, c1}, {a2, b2, c2}, {a3, b3, c3}, {a4, b4, c4}, {a5,
b5, c5}};
Outer[test[{##}] &, Sequence @@ (cons[[#]] & /@ vars)]
Out[3]= {{{test[{a2, a4, a5}], test[{a2, a4, b5}],
test[{a2, a4, c5}]}, {test[{a2, b4, a5}], test[{a2, b4, b5}],
test[{a2, b4, c5}]}, {test[{a2, c4, a5}], test[{a2, c4, b5}],
test[{a2, c4, c5}]}}, {{test[{b2, a4, a5}], test[{b2, a4, b5}],
test[{b2, a4, c5}]}, {test[{b2, b4, a5}], test[{b2, b4, b5}],
test[{b2, b4, c5}]}, {test[{b2, c4, a5}], test[{b2, c4, b5}],
test[{b2, c4, c5}]}}, {{test[{c2, a4, a5}], test[{c2, a4, b5}],
test[{c2, a4, c5}]}, {test[{c2, b4, a5}], test[{c2, b4, b5}],
test[{c2, b4, c5}]}, {test[{c2, c4, a5}], test[{c2, c4, b5}],
test[{c2, c4, c5}]}}}
HTH,
--Jean-Marc