8

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

rcollyer
  • 10,475
  • 4
  • 48
  • 75
Tom Dickens
  • 192
  • 1
  • 8
  • 2
    Unfortunately, SO does not support LaTeX markup like some of the other stackexchange sites. So, I moved the equations to code blocks. – rcollyer Aug 17 '11 at 18:13

2 Answers2

11

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
Heike
  • 24,102
  • 2
  • 31
  • 45
  • Thanks, a very nice answer! And I will learn something from understanding the code... – Tom Dickens Aug 17 '11 at 18:56
  • 1
    @Tom, doing this problem in reverse pays, i.e. try to rewrite it in [postfix form](http://stackoverflow.com/questions/7095557/may-i-write-x-a-b-do-instead-of-do-x-a-b) and what each step does will become clearer. Watch out for the function `Exp[#[[2]]]^(Times @@ ({n, m}^#[[1]])) &` it's trickier than it looks. – rcollyer Aug 23 '11 at 20:46
7

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
Leonid Shifrin
  • 22,449
  • 4
  • 68
  • 100
  • Thank you very much! I had wondered about uisng logarithms, but didn't see how to do it. (I also have enjoyed reading your Mathematica programming book...) – Tom Dickens Aug 17 '11 at 18:54
  • @Tom Dickens These are not true logarithms - these one cheat, since the first rule, for example, is not always correct. Mathematica does many auto-simplifications, a behavior which is problematic for the case at hand - these auxiliary functions prevent them from happening. – Leonid Shifrin Aug 17 '11 at 18:57
  • @Tom Dickens Glad you liked the book. Responses like yours do reassure me that writing it was not such a bad idea. – Leonid Shifrin Aug 17 '11 at 19:38
  • Good - it's very well done, and more insightful than most of the Mathematica teaching materials. I just need to find more time to work with it! – Tom Dickens Aug 17 '11 at 20:25
  • Congratulations on 10K reputation, Leonid! It was my honor to put you over the mark. :-) – Mr.Wizard Aug 20 '11 at 23:06
  • @Mr.Wizard Thanks! Hope to return the honor soon. – Leonid Shifrin Aug 22 '11 at 18:07
  • I will appreciate it. By the way, I notice that you have *never* asked a question. Is that a personal policy, or do you truly not have any questions about *Mathematica*? – Mr.Wizard Aug 22 '11 at 19:59
  • @Mr.Wizard I try to find the answers to all my questions myself, at least ar first. Not very efficient sometimes, but it worked for me so far. I learned a lot from others though - first from books, then MathGroup and now SO - I just found that I learn most effectively when I have a solution or two to some problem myself, before looking at how others solve it. Besides, in this way one can often find something new and unexpected, which often can be used in interesting ways. – Leonid Shifrin Aug 23 '11 at 09:07