Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to group terms by exponent in Mathematica expression

I need to find a method to transform an expression like

a^(1+m+n) b^(2+2m - 2n)

into

(a b^2)^m (a/b^2)^n (a b^2),

that is, to group terms with the same exponent. I tried using Collect[], etc, but can't get anything to work.

Any suggestions?

Thanks, Tom

like image 897
Tom Dickens Avatar asked Aug 17 '11 17:08

Tom Dickens


2 Answers

Using Log in combination with CoefficientRules:

exp = a^(1 + m + n) b^(2 + 2 m - 2 n);

Times @@ (Exp[#[[2]]]^(Times @@ ({n, m}^#[[1]])) & /@ 
   CoefficientRules[PowerExpand[Log[exp]], {n, m}])

output:

a (a/b^2)^n b^2 (a b^2)^m
like image 156
Heike Avatar answered Oct 01 '22 07:10

Heike


You can do this, for example:

log[x_*y_] := log[x] + log[y];
log[x_^y_] := y*log[x];
log1 /: a_*log1[b_] := log1[b^a];
log1 /: Plus[x__log1] := log1[Times @@ Map[First, {x}]];
exp[HoldPattern[Plus[x__]]] := Times @@ Map[exp, {x}];
exp[log1[x_]] := x

and then:

In[58]:= exp[Collect[Expand[log[a^(1+m+n) b^(2+2m-2n)]],{m,n}]]/.log->log1

Out[58]= a (a/b^2)^n b^2 (a b^2)^m
like image 33
Leonid Shifrin Avatar answered Oct 01 '22 08:10

Leonid Shifrin