Re: How to pick all terms which have are multiplied with a factor, e.g., 2
- To: mathgroup at smc.vnet.net
- Subject: [mg35467] Re: [mg35463] How to pick all terms which have are multiplied with a factor, e.g., 2
- From: BobHanlon at aol.com
- Date: Sun, 14 Jul 2002 06:19:39 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
In a message dated 7/13/02 4:26:31 AM, mgi at vt.edu writes:
>I would like to simplify a large symbolic summation. I know that terms
>which
>are multiplied with a common integer factor can ultimately be combined
>and
>simplified.
>
>As an example: from the sum
>
> 2 a b^2 c^3 + 13 x y a b + 2 d^-1
>
>I would like to pick 2 a b^2 c^3 + 2 d^-1 to combine them; and similarly
>for
>other common integer factors. I suspect that patterns would help, but I
>did
>not manage to devise a correct one.
intFactor[expr_] := Module[{u},
Fold[(Collect[#1 /. #2*se_ -> u*se, u] /. u->#2)&, expr,
Union[Cases[expr, n_Integer*_ -> n]]]];
expr = 2 a b^2 c^3+13 x y a b+2 d^-1;
{intFactor[expr], Simplify[expr]}
{2*(a*b^2*c^3 + 1/d) + 13*a*b*x*y,
2/d + a*b*(2*b*c^3 + 13*x*y)}
LeafCount /@ %
{21, 20}
expr = 2 a b^2 c^3+13 x y a b+2 d^-1+13z;
{intFactor[expr], Simplify[expr]}
{2*(a*b^2*c^3 + 1/d) + 13*(a*b*x*y + z),
2/d + a*b*(2*b*c^3 + 13*x*y) + 13*z}
LeafCount /@ %
{24, 23}
As measured by LeafCount, Simplify works better.
Bob Hanlon
Chantilly, VA USA