MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

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

  • To: mathgroup at smc.vnet.net
  • Subject: [mg124289] Re: How to simplify this code with variable number of nesting loops?
  • From: James Stein <mathgroup at stein.org>
  • Date: Mon, 16 Jan 2012 17:05:55 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com

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 at 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.
>
>


  • Prev by Date: Re: How to check whether an infinite set is closed under addition?
  • Next by Date: Re: NDSolve output without interpolating function
  • Previous by thread: Re: How to simplify this code with variable number of
  • Next by thread: Re: How to simplify this code with variable number of nesting loops?