Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Multiple Sums in Mathematica

1,006 views
Skip to first unread message

Sebastian Brandt

unread,
Jan 23, 2005, 2:13:38 AM1/23/05
to
Hello,

I want to perform a nested sum, where the number of summations is not
specified. I.e., I am looking for a way to implement the sum

sum[ f, {i1, 1, M}, {i2, 1, M}, ..., {in, 1, M} ]

Does anbody know how to do this for arbitrary n?

Regards,

Sebastian Brandt

David Bailey

unread,
Jan 24, 2005, 11:07:23 PM1/24/05
to
Hi,

I think you want something like this:

xx = {{i, 1, 2}, {j, 1, 2}, {k, 1, 2}};


Sum[f[i, j, k], Evaluate[Sequence @@ xx]]

David Bailey
dbaileyconsultancy.co.uk

Steve Luttrell

unread,
Jan 24, 2005, 11:09:25 PM1/24/05
to
Here is an example of what you could do:

With[{m=2,n=4},
Apply[
Sum,
Prepend[
Table[{i[k],m},{k,n}],
Apply[f,Table[i[k],{k,n}]]
]
]
]

f[1,1,1,1]+f[1,1,1,2]+f[1,1,2,1]+f[1,1,2,2]+f[1,2,1,1]+f[1,2,1,2]+f[1,2,2,1]+
f[1,2,2,2]+f[2,1,1,1]+f[2,1,1,2]+f[2,1,2,1]+f[2,1,2,2]+f[2,2,1,1]+
f[2,2,1,2]+f[2,2,2,1]+f[2,2,2,2]

Steve Luttrell

"Sebastian Brandt" <sbr...@physics.wustl.edu> wrote in message
news:csvir2$avi$1...@smc.vnet.net...

Bob Hanlon

unread,
Jan 24, 2005, 11:11:26 PM1/24/05
to
Presumably, you intend f to be a function of the indices.

multiSum[f_, n_Integer, maxIndex_:M, index_String:"i"] :=
Module[{var,k},
var=ToExpression[Table[index<>ToString[k],{k,n}]];
Fold[Sum[#1,{#2,1,maxIndex}]&,
f[Sequence@@var],Reverse[var]]];

multiSum[f,3]

Sum[f[i1, i2, i3], {i1, 1, M}, {i2, 1, M}, {i3, 1, M}]

multiSum[f,3,r]

Sum[f[i1, i2, i3], {i1, 1, r}, {i2, 1, r}, {i3, 1, r}]

multiSum[f,3,M,"k"]

Sum[f[k1, k2, k3], {k1, 1, M}, {k2, 1, M}, {k3, 1, M}]


Bob Hanlon

highegg

unread,
Jan 24, 2005, 11:17:32 PM1/24/05
to
On 22 Jan 05 16:04:14 -0500 (EST), Sebastian Brandt wrote:
>Hello,
>
>I want to perform a nested sum, where the number of summations is not
>specified. I.e., I am looking for a way to implement the sum
>
>sum[ f, {i1, 1, M}, {i2, 1, M}, ..., {in, 1, M} ]
>
>Does anbody know how to do this for arbitrary n?
>
>Regards,
>
>Sebastian Brandt


hi,

for example

brutalSum[n_Integer]:=
Plus@@(Array[f,Table[M,{n}]])

Bill Rowe

unread,
Jan 24, 2005, 11:20:36 PM1/24/05
to
On 1/23/05 at 2:02 AM, sbr...@physics.wustl.edu (Sebastian Brandt)
wrote:

>I want to perform a nested sum, where the number of summations is
>not specified. I.e., I am looking for a way to implement the sum

>sum[ f, {i1, 1, M}, {i2, 1, M}, ..., {in, 1, M} ]

>Does anbody know how to do this for arbitrary n?

Put the iterators in a list and use Sequence. For example,

list = {{n, 1, 4}, {m, 2, 5}};
Sum[n + m, Evaluate[Sequence@@list]]
--
To reply via email subtract one hundred and four

Astanoff

unread,
Jan 25, 2005, 5:07:08 AM1/25/05
to
Maybe this example is what you're looking for :

In[1]:=
n=5;
indices={#,1,m}& /@Array[i,n];
f:=Plus@@Array[i,n];(*for instance*)

In[4]:=
Sum[Evaluate[f], Evaluate[Sequence@@indices]]
Out[4]=
m^4*(2*m + 2*m^2 + (1/2)*m*(1 + m))


V.A.

Roland Franzius

unread,
Jan 25, 2005, 5:06:07 AM1/25/05
to

s[n_] := sum[f, Sequence[
ToExpression["{i" <> # <> ",1,M}" & /@ (ToString /@ Range[n])]]]

--

Roland Franzius

János

unread,
Jan 25, 2005, 5:16:20 AM1/25/05
to
Will

In[75]:=
Fold[Sum[f[#1, k],
{k, 1, m}] & , x,
Table[i[j], {j, 1, n}]]

do what you want ?

János


On Jan 23, 2005, at 2:02 AM, Sebastian Brandt wrote:

> Hello,
>
> I want to perform a nested sum, where the number of summations is not
> specified. I.e., I am looking for a way to implement the sum
>
> sum[ f, {i1, 1, M}, {i2, 1, M}, ..., {in, 1, M} ]
>
> Does anbody know how to do this for arbitrary n?
>

> Regards,
>
> Sebastian Brandt
>
----------------------------------------------
Trying to argue with a politician is like lifting up the head of a
corpse.
(S. Lem: His Master Voice)

Bob Hanlon

unread,
Jan 25, 2005, 5:20:24 AM1/25/05
to
Clear[f];

f[x__] := Module[{xl=Flatten[{x}]},
Times@@(Tr /@
({#,Range[Length[xl]]*#,#!,Table[C[k],{k,Length[xl]}]^#}&[xl]))];

f[i1,i2,i3]

(i1 + i2 + i3)*(i1 + 2*i2 + 3*i3)*(C[1]^i1 + C[2]^i2 +
C[3]^i3)*(i1! + i2! + i3!)

It also works when the argument is a list instead of a sequence

f[{i1,i2,i3,i4}]

(i1 + i2 + i3 + i4)*(i1 + 2*i2 + 3*i3 + 4*i4)*
(C[1]^i1 + C[2]^i2 + C[3]^i3 + C[4]^i4)*
(i1! + i2! + i3! + i4!)


Bob Hanlon

>
> From: Sebastian Brandt <sbr...@physics.wustl.edu>
> Date: 2005/01/24 Mon PM 09:19:09 EST
> To: han...@cox.net
> Subject: Re: Multiple Sums in Mathematica
>
> Dear Bob Hanlon,
>
> Thank you very much for your help! Your code works fine, but now I have
the (for you probably trivial) problem of how to define f as a function of an
arbitrary number of arguments.
>
> For example, f is supposed to be something like this:
>
> f[i1_, i2_, i3_, ..., iM_] =
>
> (i1 + i2 + .... + iM)
> * (1*i1 + 2*i2 + ... + M*iM)
> * (i1! + i2! + i3! + ... + iM!)
> * (C[1]^i1 * C[2]^i2 * ... * C[M]^iM)
>
> In the last part, the C[1], C[2], ..., C[M] are some constants.
>
> Sorry for bothering you with this question, but I would really appreciate it if
you could help me once more.
>
> Regards,
>
> Sebastian
>
> -------------------------------------------------
> Sebastian Brandt
> -------------------------------------------------
> Physics Department, CB 1105
> Washington University
> 1 Brookings Drive
> St. Louis, MO 63130-4899, USA
> -------------------------------------------------
> +1-314-935-7507 (phone)
> +1-314-935-6219 (fax)
> sbr...@physics.wustl.edu
> -------------------------------------------------


>
> ----- Original Message -----
> From: Bob Hanlon <han...@cox.net>
> Date: Sunday, January 23, 2005 8:20 am
> Subject: Re: Multiple Sums in Mathematica
>
> > Presumably, you intend f to be a function of the indices.
> >
> > multiSum[f_, n_Integer, maxIndex_:M, index_String:"i"] :=
> > Module[{var,k},
> > var=ToExpression[Table[index<>ToString[k],{k,n}]];
> > Fold[Sum[#1,{#2,1,maxIndex}]&,
> > f[Sequence@@var],Reverse[var]]];
> >
> > multiSum[f,3]
> >
> > Sum[f[i1, i2, i3], {i1, 1, M}, {i2, 1, M}, {i3, 1, M}]
> >
> > multiSum[f,3,r]
> >
> > Sum[f[i1, i2, i3], {i1, 1, r}, {i2, 1, r}, {i3, 1, r}]
> >
> > multiSum[f,3,M,"k"]
> >
> > Sum[f[k1, k2, k3], {k1, 1, M}, {k2, 1, M}, {k3, 1, M}]
> >
> >
> > Bob Hanlon
> >
> > >
> > > From: sbr...@physics.wustl.edu (Sebastian Brandt)
> > > Date: 2005/01/23 Sun AM 02:02:26 EST
> > > To: math...@smc.vnet.net
> > > Subject: Multiple Sums in Mathematica
> > >

Roland Franzius

unread,
Jan 26, 2005, 4:50:58 AM1/26/05
to

Sorry error, must be

s[n_] := sum[f, Sequence @@ ToExpression["{i" <> # <> ",

0 new messages