Re: Grouping similar indices together in an expression

*To*: mathgroup at smc.vnet.net*Subject*: [mg65248] Re: Grouping similar indices together in an expression*From*: Peter Pein <petsie at dordos.net>*Date*: Wed, 22 Mar 2006 06:13:38 -0500 (EST)*References*: <dvosko$j5k$1@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

David Sanders schrieb: > Hi, > > I am a newbie in Mathematica, and am trying to do the following. > > I have terms which look like > a[i] a[j] b[i] b[j] > > I need to apply a function F, for which I need to group the different > indices (by which I mean i, j) together as follows: > > F(a[i] a[j] b[i] b[j]) = F(a[i] b[i]) F(a[j] b[j]) > > I do not in general know how many different indices there might be in a > term. E.g. I might have > a[i] a[j] a[k] b[j] > > Could somebody please give me a hint as to how to do this? > > Thanks, > David. > Hi David, the function combineCommonIndices[lst_,f_:Plus,g_:Times]:=Module[{hds=Union[Head/@lst]}, g@@f@@@(Through[hds[#]]&/@ (Intersection@@(Pick[First/@lst,lst,Blank[#]]&/@ hds)))] works at least for these examples: examples = {{a[i],a[j],b[i],b[j]}, {a[i],a[j],a[k],b[j]}, {a[i],a[j],a[k],b[g],b[h],b[i],b[j]}}; combineCommonIndices[#, F] & /@ examples --> {F[a[i],b[i]] F[a[j],b[j]], F[a[j],b[j]], F[a[i],b[i]] F[a[j],b[j]]} and even the slightly confusing example lst=Union@@Outer[#1[#2]&,#,#]&[{a,b,c}] --> {a[a],a[b],a[c],b[a],b[b],b[c],c[a],c[b],c[c]} combineCommonIndices[lst,F,List] --> {F[a[a],b[a],c[a]], F[a[b],b[b],c[b]], F[a[c],b[c],c[c]]} gives the expectes result. Greetings, Peter P.S.: If you've got a term t = a[i] a[j] b[i] b[j], then use combineCommonIndices[List @@ t, F] or modify this function according to your needs.