Opened 5 years ago
Last modified 4 years ago
#21945 new defect
Symbolic min/max
Reported by: | pelegm | Owned by: | |
---|---|---|---|
Priority: | major | Milestone: | sage-8.2 |
Component: | symbolics | Keywords: | min, max, days79 |
Cc: | mathzeta2, slelievre | Merged in: | |
Authors: | Reviewers: | ||
Report Upstream: | N/A | Work issues: | |
Branch: | Commit: | ||
Dependencies: | #24838 | Stopgaps: |
Description (last modified by )
The Python builtins max
and min
are not user-friendly because they give totally wrong results on symbolic input:
sage: max(1/(1-x), 1/x) -1/(x - 1)
There exist functions max_symbolic
and min_symbolic
but people generally don't know those functions. They expect max
and min
to "just work".
One way this puzzles users is when plotting eg the following:
sage: plot(max(1/(1-x), 1/x), (x, 0, 1), ymin=0, ymax=10))
... for which a workaround is to plot using a lambda function:
sage: plot(lambda x: max(1/(1-x), 1/x), (0, 1), ymin=0, ymax=10)
Change History (10)
comment:1 Changed 5 years ago by
- Cc mathzeta2 added
comment:2 follow-up: ↓ 3 Changed 5 years ago by
comment:3 in reply to: ↑ 2 ; follow-ups: ↓ 4 ↓ 8 Changed 5 years ago by
Replying to mathzeta2:
The functions
max_symbolic
andmin_symbolic
exist. They can not make strong deductions, but at least they do know how to convert to sympy'sMax
andMin
.
Thanks for the info. The names are not intuitive, but perhaps we can live with it. Here's a short list of what these creatures should be able to do:
- Assume
x
andy
are positive. Thenmin_symbolic(x, y)
andmax_symbolic(x, y)
should return True. - Similarly, for
x
andy
which are both negative, nonnegative and nonpositive. - Similarly, if
x
is assumed to be negative andy
is assumed to be positive, thenmin
andmax
should behave respectively. min
andmax
of real symbols should be real. Similarly for algebraic, rational, integer...- Should
min_symbolic(x, x)
simplify tox
?
comment:4 in reply to: ↑ 3 Changed 5 years ago by
Replying to pelegm:
- Should
min_symbolic(x, x)
simplify tox
?
It currently does. Regarding your other points, unfortunately, it seems those are not available. See also #17700 and #19162. A "non-trivial" thing that does work is that
var("x,y,z") assume(x>z) assume(z>y) print max_symbolic(x,y,z).simplify() print min_symbolic(x,y,z).simplify()
prints x
and y
.
comment:5 Changed 4 years ago by
- Description modified (diff)
- Milestone changed from sage-7.5 to sage-8.2
- Type changed from enhancement to defect
comment:6 Changed 4 years ago by
- Cc slelievre added
- Description modified (diff)
- Keywords changed from min,max,days79 to min, max, days79
comment:7 Changed 4 years ago by
- Description modified (diff)
comment:8 in reply to: ↑ 3 ; follow-up: ↓ 9 Changed 4 years ago by
Replying to pelegm:
- Assume
x
andy
are positive. Thenmin_symbolic(x, y)
andmax_symbolic(x, y)
should return True.- Similarly, for
x
andy
which are both negative, nonnegative and nonpositive.- Similarly, if
x
is assumed to be negative andy
is assumed to be positive, thenmin
andmax
should behave respectively.min
andmax
of real symbols should be real. Similarly for algebraic, rational, integer...
Actually this is quite achievable. Pynac already has such logic for several functions. However, it would help if min/max_symbolic
would be made GinacFunction
s (like most elementary functions). Note also #18630, #24368, #24352, #22162 and https://github.com/pynac/pynac/issues/293
comment:9 in reply to: ↑ 8 Changed 4 years ago by
However, it would help if
min/max_symbolic
would be madeGinacFunction
s (like most elementary functions).
Not necessary, after some thought. I'll put this in pynac-0.7.19. The various queries depend on existence of their interface, e.g. ex.is_rational
(#24368), ex.is_nonnegative
(simply is_trivial_zero() or is_positive()
Ticket?), ex.is_nonpositive
likewise. There is no is_algebraic logic in Pynac at the moment, this is part of the above issue.
comment:10 Changed 4 years ago by
- Dependencies set to #24838
The functions
max_symbolic
andmin_symbolic
exist. They can not make strong deductions, but at least they do know how to convert to sympy'sMax
andMin
.