Ask Your Question
1

max_symbolic with assumptions?

asked 2023-08-16 16:02:02 +0100

new_to_sage gravatar image

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 flag offensive close merge delete

Comments

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

Max Alekseyev gravatar imageMax Alekseyev ( 2023-08-16 16:41:54 +0100 )edit

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

new_to_sage gravatar imagenew_to_sage ( 2023-08-16 17:51:02 +0100 )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...

Max Alekseyev gravatar imageMax Alekseyev ( 2023-08-16 18:26:00 +0100 )edit

Using giac:

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

20*y
achrzesz gravatar imageachrzesz ( 2023-08-17 10:12:37 +0100 )edit

Does giac exist as a Python module?

new_to_sage gravatar imagenew_to_sage ( 2023-08-18 15:39:44 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-08-17 12:07:26 +0100

Emmanuel Charpentier gravatar image

updated 2023-08-17 12:07:50 +0100

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,

edit flag offensive delete link more

Comments

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

new_to_sage gravatar imagenew_to_sage ( 2023-08-18 15:39:20 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2023-08-16 15:58:20 +0100

Seen: 184 times

Last updated: Aug 17 '23