Re: Iterator sequences
- To: mathgroup at yoda.physics.unc.edu
- Subject: Re: Iterator sequences
- From: TODD GAYLEY <TGAYLEY at ccit.arizona.edu>
- Date: 03 Oct 1992 20:41:20 -0700 (MST)
Hans Kunzle (hkunzle at galilei.math.ualberta.ca) asks:
> Is there a way in Mathematica to generate a list of
> iterators dynamically?
>
> What I have in mind is something like
>
> Sum[ Product[v[i]^j[i],{i,3}],Table[{j[i],nn[[i]]},{i,3}]]
>
> where nn is a given list of integers.
> This won't work since Table[..] is a list of lists, not
> just a sequence. I tried to fool Mathematica into
> accepting it by using
>
> Sum[ Product[v[i]^j[i],{i,3}],
> ToExpression[StringDrop[StringDrop[
> ToString[Table[{j[i],nn[[i]]},{i,3}]],1],-1]] ]
>
> but also get just an error message
A general method to capture the contents of a list without the enclosing
braces (i.e., as a sequence of items separated by commas) is to Apply Sequence
to it:
Sequence@@{x,y,z} -------> Sequence[x,y,z]
The first function that gets its hands on a Sequence object will strip off the
Sequence head, effectively splicing in a series of arguments separated by
commas. Thus you can use Sequence to "package" a series of arguments to a
function.
Your example becomes:
Sum[Product[v[i]^j[i],{i,3}],Evaluate[Sequence@@Table[{j[i],nn[[i]]},{i,3}]]]
The Evaluate is necessary to overcome the HoldAll attribute of Sum.
--Todd
-----------------------------------------------------------------
Todd Gayley
Department of Ecology and Evolutionary Biology
University of Arizona
tgayley at ccit.arizona.edu -or- tgayley at cs.arizona.edu