# How can I convert x^2 to x*x?

When I try the following code:

a b^2 c /. b c -> e


Mathematica gives me:

a b^2 c


but what I want is:

a b e


The FullForm of a b^2 c is Times[a, Power[b, 2], c], so it can not match the pattern b c.

the problem above is a simplified version of my practical one as below:

The practical problem which comes to me is:

dat = {Subscript[n, c] Subscript[n, d] Subscript[v, c] Subscript[v, d],Subscript[n, d] Subscript[p, c] Subscript[v, c] Subscript[v, d],I Subscript[h, d] Subscript[n, d] Subscript[p, d] Subscript[v, d],I Subscript[h, d] Subsuperscript[n, d, 2] Subscript[v, d],Subscript[n, c] Subscript[p, d] Subscript[v, c] Subscript[v, d],Subscript[p, c] Subscript[p, d] Subscript[v, c] Subscript[v, d],I Subscript[h, d] Subsuperscript[p, d, 2] Subscript[v, d],I Subscript[h, d] Subscript[n, d] Subscript[p, d] Subscript[v, d],I Subscript[h, c] Subscript[n, c] Subscript[p, c] Subscript[v, c],I Subscript[h, c] Subsuperscript[p, c, 2] Subscript[v, c],-Subscript[h, c] Subscript[h, d] Subscript[p, c] Subscript[p, d],-Subscript[h, c] Subscript[h, d] Subscript[n, d] Subscript[p, c],I Subscript[h, c] Subsuperscript[n, c, 2] Subscript[v, c],I Subscript[h, c] Subscript[n, c] Subscript[p, c] Subscript[v, c],-Subscript[h, c] Subscript[h, d] Subscript[n, c] Subscript[p, d],-Subscript[h, c] Subscript[h, d] Subscript[n, c] Subscript[n, d]}

rules = {Subscript[h, c] Subscript[p, c]->I Subscript[h, e] Subscript[n, e]+Subscript[h, f] Subscript[p, f],Subscript[h, c] Subscript[n, c]->Subscript[h, f] Subscript[n, f]+I Subscript[h, e] Subscript[p, e],Subscript[p, c] Subscript[v, c]->I Subscript[n, e] Subscript[v, e]+Subscript[p, f] Subscript[v, f],Subscript[n, c] Subscript[v, c]->I Subscript[p, e] Subscript[v, e]+Subscript[n, f] Subscript[v, f],Subscript[h, d] Subscript[p, d]->I Subscript[h, f] Subscript[n, f]+Subscript[h, e] Subscript[p, e],Subscript[h, d] Subscript[n, d]->Subscript[h, e] Subscript[n, e]+I Subscript[h, f] Subscript[p, f],Subscript[p, d] Subscript[v, d]->Subscript[p, e] Subscript[v, e]+I Subscript[n, f] Subscript[v, f],Subscript[n, d] Subscript[v, d]->Subscript[n, e] Subscript[v, e]+I Subscript[p, f] Subscript[v, f]}

dat//.rules


some elements of the dat can't be matched by rules and ReplaceRepeated.

-

Are you sure you need to transform x^2 into x*x to achieve your goal? A simpler thing will work with the same result:

a b^2 c /. b^2 c -> b e


a b e

In cases like this you can always find correct algebraic form to replace, so the substitution will just work without any tricks. Of course you always can do something like

x^2 /. x^2 -> Defer[x x]


x x

but you maybe over-thinking it.

-
Thanks for the concise answer. – cmc Oct 26 '12 at 6:55
But I have a similar question that maybe not solved by this method suitably. The question is pasted following the first one. – cmc Oct 26 '12 at 7:02

In the example you gave, Simplify works

In[1]:=  Simplify[a b ^ 2 c, b c == e]
Out[1]:= a b e


Recall that Simplify[expr,assum] "does simplification using assumptions".

-
if I try Simplify[a b^2 c, b c == e f g],I get a b^2 c. – cmc Oct 26 '12 at 7:09
the Simplify can't instead of -> in some cases. – cmc Oct 26 '12 at 7:10
I reckon Simplify[a b^2 c, b c == e f g] gives a b^2 c because it's considered shorter than a b e f g – beb0s Dec 17 '12 at 23:44

Try

a b^2 c /. c -> e/b
(*
==> a b e
*)


Generally it is a good idea to try to replace only a single symbol and let algebra do the rest.

Another possibility is to use separate rules for powers, like this:

powerreplace[x_ y_, z_] :=
Sequence[x y -> z,
x^n_ y -> x^(n-1) z,
x y^m_ -> y^(m-1) z,
x^n_ y^m_ -> x^(n-1) y^(m-1) z]

a b^2 c /. { powerreplace[b c, e] }
(*
==> a b e
*)


Note that here the list around powerreplace is mandatory (I didn't put it right into powerreplace to allow more easy use in combination to other rules, like

a b^2 c d //. { powerreplace[a b, x], powerreplace[b c, y], d->z }
(*
==> x y z
*)

-