Message Boards Message Boards

0
|
5073 Views
|
2 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Decompose Expression into Segments

Posted 11 years ago
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
POSTED BY: Arvind Rajan
2 Replies
Posted 11 years ago
I'm guessing you want to divide up "terms" and not divide up "factors", i.e, divide up + and - but not * and / and ^
 In[1]:= 3 x1^2 - 2 x3^-3 /. Plus -> List
 
 Out[1]= {3 x1^2, -(2/x3^3)}
 
 In[2]:= x1^2 /. Plus -> List
 
 Out[2]= x1^2
 
 In[3]:= 3 x1^2 /. Plus -> List

Out[3]= 3 x1^2

But if you start having even more complicated expressions then you might run into trouble with things like this

In[4]:= x^(2 a + b) /. Plus -> List


Out[4]= {x^(2 a), x^b}

and it isn't clear whether that is what you would want or not.

If that is not what you would want to happen then you might consider substituting for Plus only at the topmost level.
POSTED BY: Bill Simpson
Are you considering polynomials only? Then would MonomialList be enough?
MonomialList[a x^2 + b x y + c y^2, {x, y}]
Out[]= {a x^2, b x y, c y^2}
POSTED BY: Sam Carrettie
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract