Say I have an expression test = 3 x1^2 - 2 x3^-3 and I would like to decompose the expression into {3 x1^2, -(2/x3^3)}, I can do that by using List @@ test on Mathematica.

However, the problem I face is that when the expression is test = x1^2, i get {x1, 2} and for test = (3 x1^2) i get {3, x1^2} instead of {x1^2} and {3x1^2} respectively.

The added information I have is the list of variables. For example, if test = x1^2 I have the list of variables, which is {x1}. And for test = 3 x1^2 - 2 x3^-3, I have {x1,x3}

Is there any other way to get what I want?


  • 1
    $\begingroup$ You should look at the FullForm representation to get an idea of the structure of your expressions if you want to use pattern matching (which can be a minefield for changing expressions). $\endgroup$
    – Yves Klett
    Mar 14, 2014 at 12:28

1 Answer 1


Perhaps what you want:

exprs = {3 x1^2 - 2 x3^-3, x1^2, (3 x1^2)};

f[HoldPattern[+z__]] := {z}

f[else_] := {else}

f /@ exprs
{{3 x1^2, -(2/x3^3)}, {x1^2}, {3 x1^2}}
  • $\begingroup$ thanks! Referring to your answer, what if the only information I have is exprs = 3 x1^2 - 2 x3^-3 and I'd wanna obtain {3 x1^2, -(2/x3^3)}? $\endgroup$ Mar 14, 2014 at 12:50
  • $\begingroup$ @arvindrajan92 Perhaps I misunderstand, but that would be produced by f[exprs] I believe. Is that not correct? $\endgroup$
    – Mr.Wizard
    Mar 14, 2014 at 13:37
  • $\begingroup$ I have gotten what I want and I have added it below as the answer to my question $\endgroup$ Mar 14, 2014 at 14:02
  • 1
    $\begingroup$ @arvindrajan92 Oh, I see your confusion now. Sorry! I just put all your test expressions in a list so that it was easy to Map my f function over them, but the function works independently on each one. That is, f[3 x1^2 - 2 x3^-3] should output {3 x1^2, -(2/x3^3)}. $\endgroup$
    – Mr.Wizard
    Mar 14, 2014 at 14:40
  • 1
    $\begingroup$ @arvindrajan92 Yes, but for the other expressions to yield the output requested you also need f[else_] := {else}. $\endgroup$
    – Mr.Wizard
    Mar 15, 2014 at 1:10

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.