MathGroup Archive 2011

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

Search the Archive

Re: dynamic nested loops

  • To: mathgroup at smc.vnet.net
  • Subject: [mg122851] Re: dynamic nested loops
  • From: Bob Hanlon <hanlonr357 at gmail.com>
  • Date: Sat, 12 Nov 2011 07:35:17 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <201111110956.EAA08550@smc.vnet.net>

The form is Sum[f, Evaluate[Sequence@@iterators]]

n = 3;

iterators = Table[{i[m], 0, n - Sum[i[k], {k, m - 1}]}, {m, n}]

{{i[1], 0, 3}, {i[2], 0, 3 - i[1]}, {i[3], 0, 3 - i[1] - i[2]}}

Sum[f @@ Array[i, n], Evaluate[Sequence @@ iterators]]

f[0, 0, 0] + f[0, 0, 1] + f[0, 0, 2] + f[0, 0, 3] + f[0, 1, 0] +
 f[0, 1, 1] + f[0, 1, 2] + f[0, 2, 0] + f[0, 2, 1] + f[0, 3, 0] +
 f[1, 0, 0] + f[1, 0, 1] + f[1, 0, 2] + f[1, 1, 0] + f[1, 1, 1] +
 f[1, 2, 0] + f[2, 0, 0] + f[2, 0, 1] + f[2, 1, 0] + f[3, 0, 0]

% == Total[f @@@ Select[Tuples[Range[0, n], n], Total[#] <= n &]]

True


Bob Hanlon


On Fri, Nov 11, 2011 at 4:56 AM, Ron <ragove at verizon.net> wrote:
> i need to perform nested sums such as Sum[f, {i,imin,imax},{j, jmin, jmax}, ...{}].  In each nest the new index is a function of the previous and the max values vary.  Also at the beginning I do not know how long a nest I need.  I can't come up with anything that works.  I could construct a list {{i,imin,imax},{j, jmin, jmax}, ...{}} but "Sum" doen't know what to do with the outside braces{...}.
> ANy suggestions would be much appreciated.
>



  • Prev by Date: Re: Pattern to match element in nested list
  • Next by Date: Re: Converting a list of strings to a single space-delimited string
  • Previous by thread: Re: dynamic nested loops
  • Next by thread: Re: dynamic nested loops