Nikos Pap
Nikos Pap

Reputation: 159

Sympy: Is there a function that gives all the factors of an expression but does not work sums?

I have an expression which is composed only of factors (e.g. (x**2+1)*(x**2)*(x+4). I want to delete the factor x**2 from it using the function .args with an if condition. However, if I have the following equation x**2+1+x+4, the .args thinks I have x**2 in the expression which is not true (I only have one factor). I have the code below.:

if q**2 in expr.args:        
   expr = expr.func(*[term for term in expr.args if term != q**2])
else:
   expr = expr*2

Upvotes: 0

Views: 25

Answers (1)

smichr
smichr

Reputation: 19063

By using Mul.make_args(expr) you will get a singleton if the expression is not a product, otherwise a tuple of the factors:

>>> from sympy.abc import x, y
>>> from sympy import Mul
>>> Mul.make_args(x + y)
(x + y,)
>>> Mul.make_args(x*y)
(x, y)

Upvotes: 2

Related Questions