Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

share|improve this question
1  
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 at 12:28

1 Answer 1

up vote 3 down vote accepted

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}}
share|improve this answer
    
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 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 at 13:37
    
I have gotten what I want and I have added it below as the answer to my question –  arvindrajan92 Mar 14 at 14:02
1  
@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 at 14:40
1  
@arvindrajan92 Yes, but for the other expressions to yield the output requested you also need f[else_] := {else}. –  Mr.Wizard Mar 15 at 1:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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