Re: How to simplify this code with variable number of
- To: mathgroup at smc.vnet.net
- Subject: [mg124292] Re: How to simplify this code with variable number of
- From: James Stein <mathgroup at stein.org>
- Date: Mon, 16 Jan 2012 17:06:59 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <201201150951.EAA19677@smc.vnet.net> <CAKStUKpK54q6uiUBLmMO+w3KJu5dEXCxveqDfrca2AGzS=i26w@mail.gmail.com>
Just noticed a typo in my code; second line should be a = Table[0, {k, 1, g+1}]; On Sun, Jan 15, 2012 at 6:14 PM, James Stein <mathgroup at stein.org> wrote: > 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. >> >> > </p><p><font face="'courier new', monospace">looper[g_] := Modu= le[{a, computations, f},<br> a = Table[0, {k, 1, g} + 1];<br> (*Note: your a[0] is Mathematica's a[[1]] *)<br> a[[1]] := initializeA0[];(* or all of a?? *)<br> computations = initializeComputations[];<br> f[i_] := If[i-1 <= g,<br> For[a[[i]] = a[[i - 1]] + 1, a[[i]] <= 2 i - 1, ++a[[i]],= <br> computations = f[i + 1]<br> ];<br> ];<br> Return[f[2]];<br> ];</font></p> <p></p></div><div><div class="h5"><div><br><br><div class="gmail_quote"= >On Sun, Jan 15, 2012 at 1:51 AM, Rex <span dir="ltr"><<a href="mail= to:aoirex at gmail.com" target="_blank">aoirex at gmail.com</a>></span> wrot= e:<br> <blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> Considering an array a[i], i=0,1,...,g, where g could be any given<br> number, and a[0]=1.<br> <br> for a[1]=a[0]+1 to 1 do<br> for a[2]=a[1]+1 to 3 do<br> for a[3]=a[2]+1 to 5 do<br> ......<br> for a[g]=a[g-1]+1 to 2g-1 do<br> ###some computations here###<br> <br> The problem is that everytime we change the value of g, we need to<br> modify the code, those loops above. So this is not a good code.<br> <br> Any advice would be greatly appreciated.<br> <br> </blockquote></div><br></div></div></div></div> </blockquote></div><br> --0015175cd5ce5edf0104b6a0ed7b--
- References:
- How to simplify this code with variable number of nesting loops?
- From: Rex <aoirex@gmail.com>
- How to simplify this code with variable number of nesting loops?