Re: Grouping similar indices together in an expression
- To: mathgroup at smc.vnet.net
- Subject: [mg65257] Re: [mg65242] Grouping similar indices together in an expression
- From: "David Park" <djmp at earthlink.net>
- Date: Wed, 22 Mar 2006 06:13:48 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
David,
Your question isn't totally clear, but I'll take the Subject heading as the
definition.
expr = a[i] a[j] b[i] b[j]
As a newbie you will probably not like the following solution.
Times @@ F /@ Times @@@ MapThread[Cases[expr, _[#]] &, {{i, j}}]
F[a[i] b[i]] F[a[j] b[j]]
So I will take it apart. If we want all the factors in the term that contain
the index i we could use.
Cases[expr, _[i]]
{a[i], b[i]}
where _[i] is a pattern for an expression with any head and the single
argument i.
We want separate lists for each index. To obtain that we use MapThread that
evaluates a Cases statement for each of a list of indices. (With this
approach you have to know what the list of indices is.) The Cases statement
is turned into a pure function where # stands for the particular index that
will be used. Look up Function in Help.
MapThread[Cases[expr, _[#]] &, {{i, j}}]
{{a[i], b[i]}, {a[j], b[j]}}
Now copy and paste and then evaluate the following code in a Mathematica
notebook to see what each step in the expression does.
Print["Group factors with similar indices in following expression"]
expr = a[i] a[j] b[i] b[j]
Print["Generate lists of common index factors using Cases and MapThread"]
step1 = MapThread[Cases[expr, _[#]] &, {{i, j}}]
Print["Apply Times to each of the inner lists - 4th note in Apply Help"]
step2 = Times @@@ step1
Print["Map F onto the grouped factors - check Map Help"]
step3 = F /@ step2
Print["Convert the list to a product - check Apply again"]
Times @@ step3
I wouldn't be shocked if someone comes up with a much simpler method;
nevertheless, welcome to Mathematica's functional programming.
David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/
From: David Sanders [mailto:dpsanders at gmail.com]
To: mathgroup at smc.vnet.net
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.