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


Groups > comp.soft-sys.math.mathematica > #7893 > unrolled thread

Re: How to simplify this code with variable number of

Started byBob Hanlon <hanlonr357@gmail.com>
First post2012-01-16 12:07 +0000
Last post2012-01-16 12:07 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.soft-sys.math.mathematica

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: How to simplify this code with variable number of Bob Hanlon <hanlonr357@gmail.com> - 2012-01-16 12:07 +0000

#7893 — Re: How to simplify this code with variable number of

FromBob Hanlon <hanlonr357@gmail.com>
Date2012-01-16 12:07 +0000
SubjectRe: How to simplify this code with variable number of
Message-ID<jf13tu$5nn$1@smc.vnet.net>
iter[0] = {a[0], 1, 1};

iter[n_Integer?Positive] = {a[n], a[n - 1] + 1, 2 n - 1};

g = 2;

Table[f @@ Table[a[k], {k, 0, g}],
  Evaluate[Sequence @@ Table[iter[k], {k, 0, g}]]] //
 Flatten[#, g] &

{}

The iterators upper bounds are lower than the corresponding lower
bounds, so the iterations are empty

With a different definition for the iterators

iter[n_Integer?Positive] = {a[n], a[n - 1], 2 n - 1};

Table[f @@ Table[a[k], {k, 0, g}],
  Evaluate[Sequence @@ Table[iter[k], {k, 0, g}]]] //
 Flatten[#, g] &

{f[1, 1, 1], f[1, 1, 2], f[1, 1, 3]}

Or

iter[n_Integer?Positive] = {a[n], a[n - 1] + 1, 2 n + 1};

Table[f @@ Table[a[k], {k, 0, g}],
  Evaluate[Sequence @@ Table[iter[k], {k, 0, g}]]] //
 Flatten[#, g] &

{f[1, 2, 3], f[1, 2, 4], f[1, 2, 5], f[1, 3, 4], f[1, 3, 5]}


Bob Hanlon

On Sun, Jan 15, 2012 at 4: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.
>

[toc] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web