2
$\begingroup$

I have the following nested list

{{yy[1, 4] yy[2, 7]}, {yy[2, 5]^2}, {1}, {yy[1, 5]^3 yy[2, 3]^2}}

with objects yy[#1,#2] and I wish to get rid of the powers ^2 and ^3; writing them as an explicit product, i.e. I want the result to be

{{yy[1, 4] yy[2, 7]}, {yy[2, 5] yy[2, 5]}, {1},
 {yy[1, 5] yy[1, 5] yy[1, 5] yy[2, 3] yy[2, 3]}}
$\endgroup$
  • 1
    $\begingroup$ A space in Mathematica usually implies multiplication. If you are just wanting it for looks purposes, you can use /. Power[f_, e_] :> Row[ConstantArray[f, e]] to replicate what you have. $\endgroup$ – kale Jun 25 '15 at 13:53
  • 1
    $\begingroup$ Thanks for the reply, it isn't just for looks, I would still need the space to represent multiplication because I will end up using /. Times -> List. (I'd like a comma in between each product) $\endgroup$ – user1523500 Jun 25 '15 at 13:58
2
$\begingroup$

Identical multiplicands are automatically converted into powers:

foo*foo*bar
bar foo^2

To prevent this you either need to prevent evaluation as with HoldForm:

HoldForm[foo*foo*bar]
foo foo bar      (* HoldForm still present but not displayed *)

Or you need to define your own display method for Power which is essentially what kale proposes in a comment:

foo*foo*bar /. Power[m_, x_] :> Row[ConstantArray[m, x], " "]
bar foo foo

In a comment you write:

I would still need the space to represent multiplication because I will end up using /. Times -> List. (I'd like a comma in between each product)

Please tell me if this meets your goal:

expr = {{yy[1, 4] yy[2, 7]}, {yy[1, 3] yy[2, 3] yy[2, 5]^2}, {1},
        {yy[1, 5] yy[2, 5]^3 yy[2, 3]^2}};

expr /. {Times -> Sequence, Power -> Apply[Sequence]@*ConstantArray}
{{yy[1, 4], yy[2, 7]}, {yy[1, 3], yy[2, 3], yy[2, 5], yy[2, 5]}, {1},
 {yy[1, 5], yy[2, 3], yy[2, 3], yy[2, 5], yy[2, 5], yy[2, 5]}}
| improve this answer | |
$\endgroup$
  • $\begingroup$ Hi, thanks for the answer. I have a slight issue using this code. The problem with doing Times -> List before your code is it puts an extra set of curly brackets around elements which have a multplication with a space.{{yy[1, 4] yy[2, 7]}, {yy[2, 5]^2}, {1}, {yy[1, 5]^3 yy[2, 3]^2}} /. Times -> List gives extra curly brackets for the first and last element of this list which I don't really want. So after applying your code, I get different levels of nested lists. $\endgroup$ – user1523500 Jun 25 '15 at 14:35
  • $\begingroup$ {{yy[1, 4] yy[2, 7]}, {yy[2, 5]^2}, {1}, {yy[1, 5]^3 yy[2, 3]^2}} /. Times -> List /. Power -> Apply[Sequence]@*ConstantArray } give too many curly brackets in the first and last elements. My end result should be {{yy[1, 4], yy[2, 7]}, {yy[2, 5], yy[2, 5]}, {1}, {yy[1, 5],yy[1, 5],yy[1, 5], yy[2, 3], yy[2, 3]}} $\endgroup$ – user1523500 Jun 25 '15 at 14:37
  • $\begingroup$ This wasn't an issue when I had elements lists with no powers, because I would just do /. Times -> List and Flatten as necessary, usually Flatten[%,1] $\endgroup$ – user1523500 Jun 25 '15 at 14:44
  • $\begingroup$ @user1523500 Rather convoluted but does this do what you want (in other cases as well)?: {{yy[1, 4] yy[2, 7]}, {yy[2, 5]^2}, {1}, {yy[1, 5]^3 yy[2, 3]^2}} /. Times -> AngleBracket /. Power -> ConstantArray /. AngleBracket -> Join //. {{x__}} :> {x} If so I can try to refine it. $\endgroup$ – Mr.Wizard Jun 25 '15 at 15:44
  • $\begingroup$ Not quite, that merged the numbers in brackets of the first expression for some reason, giving yy[1,4,2,7]. I think I found something that works, although it probably isn't that efficient, it uses your code, strings and I made the list more complicated to make sure it covers all cases I need: { {{yy[1, 4] yy[2, 7]}, {yy[1, 3] yy[2, 3] yy[2, 5]^2}, {1}, {yy[1, 5] yy[2, 5]^3 yy[2, 3]^2}} /. Times -> AngleBracket /. Power -> Apply[Sequence]@*ConstantArray; StringReplace[ StringReplace[ToString[%], "[LeftAngleBracket]" -> ""], "[RightAngleBracket]" -> ""]; gg = ToExpression[%] $\endgroup$ – user1523500 Jun 25 '15 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.