Symbolic minimum and maximum¶
Sage provides a symbolic maximum and minimum due to the fact that the
Python builtin max()
and min()
are not able to deal with variables
as users might expect. These functions wait to evaluate if there are variables.
Here you can see some differences:
sage: max(x, x^2) # needs sage.symbolic
x
sage: max_symbolic(x, x^2) # needs sage.symbolic
max(x, x^2)
sage: f(x) = max_symbolic(x, x^2); f(1/2) # needs sage.symbolic
1/2
>>> from sage.all import *
>>> max(x, x**Integer(2)) # needs sage.symbolic
x
>>> max_symbolic(x, x**Integer(2)) # needs sage.symbolic
max(x, x^2)
>>> __tmp__=var("x"); f = symbolic_expression(max_symbolic(x, x**Integer(2))).function(x); f(Integer(1)/Integer(2)) # needs sage.symbolic
1/2
This works as expected for more than two entries:
sage: # needs sage.symbolic
sage: max(3, 5, x)
5
sage: min(3, 5, x)
3
sage: max_symbolic(3, 5, x)
max(x, 5)
sage: min_symbolic(3, 5, x)
min(x, 3)
>>> from sage.all import *
>>> # needs sage.symbolic
>>> max(Integer(3), Integer(5), x)
5
>>> min(Integer(3), Integer(5), x)
3
>>> max_symbolic(Integer(3), Integer(5), x)
max(x, 5)
>>> min_symbolic(Integer(3), Integer(5), x)
min(x, 3)
- class sage.functions.min_max.MaxSymbolic[source]¶
Bases:
MinMax_base
Symbolic \(\max\) function.
The Python builtin
max()
function does not work as expected when symbolic expressions are given as arguments. This function delays evaluation until all symbolic arguments are substituted with values.EXAMPLES:
sage: # needs sage.symbolic sage: max_symbolic(3, x) max(3, x) sage: max_symbolic(3, x).subs(x=5) 5 sage: max_symbolic(3, 5, x) max(x, 5) sage: max_symbolic([3, 5, x]) max(x, 5)
>>> from sage.all import * >>> # needs sage.symbolic >>> max_symbolic(Integer(3), x) max(3, x) >>> max_symbolic(Integer(3), x).subs(x=Integer(5)) 5 >>> max_symbolic(Integer(3), Integer(5), x) max(x, 5) >>> max_symbolic([Integer(3), Integer(5), x]) max(x, 5)
- class sage.functions.min_max.MinMax_base[source]¶
Bases:
BuiltinFunction
- eval_helper(this_f, builtin_f, initial_val, args)[source]¶
EXAMPLES:
sage: # needs sage.symbolic sage: max_symbolic(3, 5, x) # indirect doctest max(x, 5) sage: max_symbolic([5.0r]) # indirect doctest 5.0 sage: min_symbolic(3, 5, x) min(x, 3) sage: min_symbolic([5.0r]) # indirect doctest 5.0
>>> from sage.all import * >>> # needs sage.symbolic >>> max_symbolic(Integer(3), Integer(5), x) # indirect doctest max(x, 5) >>> max_symbolic([5.0]) # indirect doctest 5.0 >>> min_symbolic(Integer(3), Integer(5), x) min(x, 3) >>> min_symbolic([5.0]) # indirect doctest 5.0
- class sage.functions.min_max.MinSymbolic[source]¶
Bases:
MinMax_base
Symbolic \(\min\) function.
The Python builtin
min()
function does not work as expected when symbolic expressions are given as arguments. This function delays evaluation until all symbolic arguments are substituted with values.EXAMPLES:
sage: # needs sage.symbolic sage: min_symbolic(3, x) min(3, x) sage: min_symbolic(3, x).subs(x=5) 3 sage: min_symbolic(3, 5, x) min(x, 3) sage: min_symbolic([3, 5, x]) min(x, 3)
>>> from sage.all import * >>> # needs sage.symbolic >>> min_symbolic(Integer(3), x) min(3, x) >>> min_symbolic(Integer(3), x).subs(x=Integer(5)) 3 >>> min_symbolic(Integer(3), Integer(5), x) min(x, 3) >>> min_symbolic([Integer(3), Integer(5), x]) min(x, 3)