First time here? Check out the FAQ!

Ask Your Question
1

max_symbolic with assumptions?

asked 1 year ago

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!

Preview: (hide)

Comments

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

Max Alekseyev gravatar imageMax Alekseyev ( 1 year ago )

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

new_to_sage gravatar imagenew_to_sage ( 1 year ago )

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 ( 1 year ago )

Using giac:

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

20*y
achrzesz gravatar imageachrzesz ( 1 year ago )

Does giac exist as a Python module?

new_to_sage gravatar imagenew_to_sage ( 1 year ago )

1 Answer

Sort by » oldest newest most voted
0

answered 1 year ago

Emmanuel Charpentier gravatar image

updated 1 year ago

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,

Preview: (hide)
link

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 ( 1 year ago )

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: 1 year ago

Seen: 216 times

Last updated: Aug 17 '23