6

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$.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
MEDVIS
  • 173
  • 4

3 Answers3

10

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
Pillsy
  • 18,498
  • 2
  • 46
  • 92
8

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.

Graumagier
  • 1,130
  • 5
  • 11
5

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:

enter image description here

ubpdqn
  • 60,617
  • 3
  • 59
  • 148