[Date Index]
[Thread Index]
[Author Index]
Re: Loop programming; how do i do this ???
*To*: mathgroup at smc.vnet.net
*Subject*: [mg96102] Re: Loop programming; how do i do this ???
*From*: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
*Date*: Thu, 5 Feb 2009 04:38:25 -0500 (EST)
*Organization*: The Open University, Milton Keynes, UK
*References*: <gmbr29$igp$1@smc.vnet.net>
In article <gmbr29$igp$1 at smc.vnet.net>, plzhlp <naso at live.be> wrote:
> I need to implement a function into my notebook but am having great
> difficulties even getting started (i'm new to mathematica and programming in
> general)
>
> The function accepts a 1d array (of variable length) of integers
>
> which will refer to a 2d array of constants, of which all need to be tested
> against eachother.
>
> for example:
>
> 1DArrayVar = {2, 4, 5}
>
> 2DArrayCon = {
> {a1,b1,c1},
> {a2,b2,c2},
> {a3,b3,c3},
> {a4,b4,c4},
> {a5,b5,c5}
> }
>
> Function[1DArrayVar]
>
> in this scenario will test:
>
> Do[
> Do[
> Do[
>
> TEST[2DArrayCon[[2,x]],2DArrayCon[[4,y]],2DArrayCon[[5,z]]]
>
> , {x, Length[DArrayCon[[2]]]
> , {y, Length[DArrayCon[[4]]]
> , {z, Length[DArrayCon[[5]]]
>
>
> So basically the problem is that i need a variable amount of nested loops, as
> the length of 1DArrayVar will always be unknown.
"A variable amount of nested loops," just forget it, whatever
programming languages your are/might/will be using.
> >From some research on the internet i've learned a recursive algorithm might
> >be the solution, but i am completely lost on how to start --
Recursion can be use.
> any tips would be really appreciated !
Here is a functional/mathematical approach that is independent of the
size of the lists (so no need for indexes, nested loops, etc.) and is
computationally efficient.
My understanding is that you want the outer product of a variable number
of vectors (vectors that are the rows of a matrix ---'cons' in the
example below--- and are picked up accordingly to a vector of indices
---'vars' in the example---).
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]}}}
Regards,
--Jean-Marc
Prev by Date:
**Re: Implementation of Minimize for integer programming in Mathematica 7**
Next by Date:
**Re: Loop programming; how do i do this ???**
Previous by thread:
**Re: Loop programming; how do i do this ???**
Next by thread:
**Re: Loop programming; how do i do this ???**
| |