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
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
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...
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
hi,
for example
brutalSum[n_Integer]:=
Plus@@(Array[f,Table[M,{n}]])
>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
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.
s[n_] := sum[f, Sequence[
ToExpression["{i" <> # <> ",1,M}" & /@ (ToString /@ Range[n])]]]
--
Roland Franzius
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)
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
> > >
Sorry error, must be
s[n_] := sum[f, Sequence @@ ToExpression["{i" <> # <> ",