# Decompose Expression into Segments

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?

Thanks

-
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). – Yves Klett Mar 14 '14 at 12:28

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}}

-
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)}? – arvindrajan92 Mar 14 '14 at 12:50
@arvindrajan92 Perhaps I misunderstand, but that would be produced by f[exprs] I believe. Is that not correct? – Mr.Wizard Mar 14 '14 at 13:37
I have gotten what I want and I have added it below as the answer to my question – arvindrajan92 Mar 14 '14 at 14:02
@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)}. – Mr.Wizard Mar 14 '14 at 14:40
@arvindrajan92 Yes, but for the other expressions to yield the output requested you also need f[else_] := {else}. – Mr.Wizard Mar 15 '14 at 1:10