user191919
user191919

Reputation: 754

Sympy simplification of maximum

I don't understand why Sympy won't return to me the expression below simplified (not sure its a bug in my code or a feature of Sympy).

import sympy as sp

a = sp.Symbol('a',finite = True, real = True)
b = sp.Symbol('b',finite = True, real = True)
sp.assumptions.assume.global_assumptions.add(sp.Q.positive(b))
sp.assumptions.assume.global_assumptions.add(sp.Q.negative(a))
sp.simplify(sp.Max(a-b,a+b))

I would expect the output to be $a+b$, but Sympy still gives me $Max(a-b,a+b)$.

Thanks; as you can see I am a beginner in Sympy so any hints/help are appreciated.

Upvotes: 1

Views: 463

Answers (1)

Oscar Benjamin
Oscar Benjamin

Reputation: 14480

Surely the result should be a + b...

You can do this by setting the assumptions on the symbol as in:

In [2]: a = Symbol('a', negative=True)                                                                                                         

In [3]: b = Symbol('b', positive=True)                                                                                                         

In [4]: Max(a - b, a + b)                                                                                                                      
Out[4]: a + b

You are trying to use the new assumptions system but that system is still experimental and is not widely used within sympy. The new assumptions are not used in core evaluation so e.g. the Max function has no idea that you have declared global assumptions on a and b unless those assumptions are declared on the symbols as I show above.

Upvotes: 2

Related Questions