Is there any command in Mathematica to expand terms like $x^2\, y^3$ to $x\,x\,y\,y\,y$?
I tried PowerExpansion, but it works for expressions like $(x\,y)^2$ which gives $x^2\,y^2$.
Is there any command in Mathematica to expand terms like $x^2\, y^3$ to $x\,x\,y\,y\,y$?
I tried PowerExpansion, but it works for expressions like $(x\,y)^2$ which gives $x^2\,y^2$.
This is a task for Inactivate/Inactive and HoldForm:
powerProductForm[expr_] :=
With[{expanded = Inactivate[
expr /. Power[u_, n_Integer?Positive] :>
Inactive[Times] @@ ConstantArray[u, n],
Times
] /.
prod : Inactive[Times][__] :> Flatten[prod]},
Activate@HoldForm[expanded]];
Then
In[2]:= powerProductForm[x^2*y^3 + x*y^2 + 2*z^3*w^4]
Out[2]= x y y + x x y y y + 2 w w w w z z z
Activate[Inactivate blah blah blah!!
– MEDVIS
Nov 02 '15 at 06:16
Inactivate was introduced in 10.0.
– Pillsy
Nov 02 '15 at 13:05
J. M. beat me to it with the comment, but here's some example code:
f=(x^2 y^3);
Row@
Flatten@
(ConstantArray @@@
Cases[#, Power[b_, e_] :> {b, e}] &@
f)
You should think about whether you need the expansion purely for display purposes though, or whether you need to do any further manipulation. xxyyy in the above example is something complete different to Mathematica than x^2 y^3.
With[{prod = Join @@ (x^2 y^3 /. {Times -> List, Power -> ConstantArray})}, Defer[prod] /. List -> Times].
– J. M.'s missing motivation
Nov 01 '15 at 11:38
If this is for display purposes you could use Format:
Format[fmt[{x_, n_}], TraditionalForm] := Row@Table[x, {n}]
fmt[{x_, 0}] := 1
fun[poly_, {var__}] := With[{cr = CoefficientRules[poly, {var}]},
Times @@ MapThread[fmt[{#2, #1}] &, {#, {var}}] & /@
cr[[All, 1]].cr[[All, 2]] // TraditionalForm
]
For example:
fun[3 x^2 z^6 + x^2 y^3 + z^5 x y^4 + 4 x y z, {x, y, z}]
yields:
Row[]andConstantArray[]… – J. M.'s missing motivation Nov 01 '15 at 10:16