# max_symbolic with assumptions?

I am in need of a symbolic engine for Python to calcultate max expressions. It is important that we can pass assumptions like "x is a positive symbol" and "y is greater than x" to the engine (the latter is why sympy does not suffice AFAIK).

I am currently testing sage and ran into some disappointing results... Am I doing something wrong here or is this sage's limit?

import sage.all as sage
x = sage.var('x')
y = sage.var('y')

myMax = sage.max_symbolic(x, 2*x)
print(sage.simplify(myMax)) # gives max(x, 2*x) as expected

sage.assume(x, 'integer')
sage.assume(x > 0)
print(sage.simplify(myMax)) # simplifies to 2*x (nice!)

sage.assume(y > 0)
sage.assume(y, 'integer')
myMax = sage.max_symbolic(x, y)
print(sage.simplify(myMax)) # gives max(x, y) as expected

sage.assume(y > x)
print(sage.simplify(myMax)) # now gives y (nice!)
print(sage.simplify(sage.max_symbolic(20*x, 20*y))) # gives 20*y (nice!)
print(sage.simplify(sage.max_symbolic(19*x, 20*y))) # gives max(19*x, 20*y)... why?
print(sage.simplify(sage.max_symbolic(19*x, 20*x))) # gives 20*x, so the above should be deducable


We tell sage that x and y are positive integers and that y > x. Sage can deduce that 20y > 20x (and also 20x > 19x), but it cannot deduce that 20y > 19x? :(

Do I need to do somehing differently or is this how things are in sage?

Thanks for the much appreciated help!

edit retag close merge delete

If constraints are linear it's worth employing Integer Linear Programming rather than symbolic machinery.

( 2023-08-16 16:41:54 +0200 )edit

It's not given that the symbols are integers in the actual usecase, but rather reals.

( 2023-08-16 17:51:02 +0200 )edit

That's fine - just Linear Programming then, which is computationally easier than integer one. In Sage, there is a common interface for them called Mixed Integer Linear Programming - eg., see this tutorial: https://doc.sagemath.org/html/en/them...

( 2023-08-16 18:26:00 +0200 )edit

Using giac:

giac('assume(x>0);assume(y>0);assume(y>x)');
giac('max(19*x,20*y)')

20*y

( 2023-08-17 10:12:37 +0200 )edit

Does giac exist as a Python module?

( 2023-08-18 15:39:44 +0200 )edit

Sort by ยป oldest newest most voted

Well...

sage: var("a, b")
(a, b)
sage: assume(a, b, "real", a>b)
sage: max_symbolic(a, b)
max(a, b)


Disappointing, indeed. But :

sage: max_symbolic(a, b).simplify()
a


Aaaahhhh...

HTH,

more

I am already calling simplify() on my expressions, but it does not work for the harder case

( 2023-08-18 15:39:20 +0200 )edit

## Stats

Seen: 114 times

Last updated: Aug 17 '23