Vaibhav Karve
Vaibhav Karve

Reputation: 45

Check if a Sympy expression simplifies to a constant

Is there a way (within Sympy) to check if two expressions differ by a mere constant? In other words, is there something like a is_constant() function?

My minimum working example:

from sympy import symbols, simplify
x,y = symbols('x y')
expr1 = x+y+1
expr2 = x+y+3
if is_constant(simplify(expr1 - expr2)):
    print('expr2 is just expr1 added to a constant!')

Upvotes: 3

Views: 1016

Answers (1)

Stelios
Stelios

Reputation: 5521

You could use the is_constant() method of Sympy objects. For example,

(expr1 - expr2).is_constant()

True

Upvotes: 4

Related Questions