Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find all terms in an expression in Sympy

Tags:

python

sympy

I have an expression as: 1/(x+1)+4*x/(x-1)+3-4*x**2+10*x**2

What I need is a list that contain the terms in this expression.
i.e. [1/(x+1), 4*x/(x-1), 3, -4*x**2 , 10*x**2]

update: It should not collect like terms. Therefore the list should have -4*x** 2 and 10*x** 2 separately and not 6*x**2 after collecting like terms.

like image 327
harsh Avatar asked Jun 01 '16 10:06

harsh


1 Answers

The right way to do this is Add.make_args. This is the same things as expr.args from Bjoern's answer, except if the expression is not an Add (a single term), it still gives that term, instead of traversing into that expression.

In [20]: expr = 1/(x+1)+4*x/(x-1)+3-4*x**2

In [21]: print(Add.make_args(expr))
(3, 1/(x + 1), -4*x**2, 4*x/(x - 1))
like image 133
asmeurer Avatar answered Oct 31 '22 16:10

asmeurer