简体   繁体   中英

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(ab,a+b)$.

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

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 eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM