David Armendariz
David Armendariz

Reputation: 1779

How to tell Sympy that one symbol is "greater than other"

Suppose I have two symbols

x,y=symbols('x y')

My objective is to tell Sympy that x is always greater than y (x>y). Is there any way to achieve this?

Upvotes: 9

Views: 5352

Answers (1)

user6655984
user6655984

Reputation:

There is no way to do this directly. The assumptions module does not support relations, and it's not (yet) integrated with the rest of SymPy anyway, so its assumptions won't help you, say, simplify an integral.

The workaround is to introduce a symbol expressing the difference of two symbols, and declare that to be positive. For example,

y = symbols('y')
p = symbols('p', positive=True)
x = y + p

Now SymPy knows that x > y:

>>> (x > y).simplify()
True

How useful this is in computations that involve x depends on whether doing .subs(x, y + p) will turn it into something that simplifies.

It's often easier to directly replace a condition with True or False, as I did here.

Upvotes: 9

Related Questions