Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #7861

Re: How to simplify this code with variable number of nesting loops?

From James Stein <mathgroup@stein.org>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: How to simplify this code with variable number of nesting loops?
Date 2012-01-16 11:53 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jf1345$5hb$1@smc.vnet.net> (permalink)

Show all headers | View raw


As I understand your pseudo-code, your problem seems malformed (and
therefore incorrect);-- it seems to me that loop variables are being
conflated with initial values of loop variables. For example, by the time
your inner computation is performed for the first time, all elements of the
array a, except the first, will have been "initialized" based on the
 initial value of a[0].  This is overly complex: you should initialize ALL
the values of a before entering the outer loop, and use simple variables,
not members of vectors, for you loop variables.

Nevertheless, I think something like the following replicates in
Mathematica approximately what you have written in pseudo code, and may be
of some help to you.

 looper[g_] := Module[{a, computations, f},
   a = Table[0, {k, 1, g} + 1];
   (*Note: your a[0] is Mathematica's a[[1]] *)
   a[[1]] := initializeA0[];(* or all of a?? *)
   computations = initializeComputations[];
   f[i_] := If[i-1 <= g,
     For[a[[i]] = a[[i - 1]] + 1, a[[i]] <= 2 i - 1, ++a[[i]],
       computations = f[i + 1]
       ];
     ];
   Return[f[2]];
   ];



On Sun, Jan 15, 2012 at 1:51 AM, Rex <aoirex@gmail.com> wrote:

> Considering an array a[i], i=0,1,...,g, where g could be any given
> number, and a[0]=1.
>
>    for a[1]=a[0]+1 to 1 do
>        for a[2]=a[1]+1 to 3 do
>           for a[3]=a[2]+1 to 5 do
>             ......
>              for a[g]=a[g-1]+1 to 2g-1 do
>                ###some computations here###
>
> The problem is that everytime we change the value of g, we need to
> modify the code, those loops above. So this is not a good code.
>
> Any advice would be greatly appreciated.
>
>

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: How to simplify this code with variable number of nesting loops? James Stein <mathgroup@stein.org> - 2012-01-16 11:53 +0000

csiph-web