Re: Grouping similar indices together in an expression
- To: mathgroup at smc.vnet.net
- Subject: [mg65246] Re: [mg65242] Grouping similar indices together in an expression
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Wed, 22 Mar 2006 06:13:36 -0500 (EST)
- References: <200603211246.HAA19581@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On 21 Mar 2006, at 13:46, David Sanders wrote: > 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. > As long as you do not mind the order of the terms being re-arranged, the following should do what you want: GroupTerms[m_Times, f_] := Times @@ Apply[f, Split[Sort[List @@ m, OrderedQ[{#1[[1]], #2[[1]]}] & ], #1[[1]] === #2[[1]] & ], {1}] For example: GroupTerms[a[1]*a[2]*b[1]*b[2]*c[1]*c[3], f] f[c[3]]*f[a[2], b[2]]*f[a[1], b[1], c[1]] which separates all terms according to the "index" . Andrzej Kozlowski
- References:
- Grouping similar indices together in an expression
- From: "David Sanders" <dpsanders@gmail.com>
- Grouping similar indices together in an expression