Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Symbolic min/max #21945

Open
pelegm opened this issue Nov 23, 2016 · 9 comments
Open

Symbolic min/max #21945

pelegm opened this issue Nov 23, 2016 · 9 comments

Comments

@pelegm
Copy link
Contributor

pelegm commented Nov 23, 2016

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)

Depends on #24838

CC: @mathzeta @slel

Component: symbolics

Keywords: min, max, days79

Issue created by migration from https://trac.sagemath.org/ticket/21945

@pelegm pelegm added this to the sage-7.5 milestone Nov 23, 2016
@mathzeta
Copy link

comment:2

The functions max_symbolic and min_symbolic exist. They can not make strong deductions, but at least they do know how to convert to sympy's Max and Min.

@pelegm
Copy link
Contributor Author

pelegm commented Nov 25, 2016

comment:3

Replying to @mathzeta:

The functions max_symbolic and min_symbolic exist. They can not make strong deductions, but at least they do know how to convert to sympy's Max and Min.

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 and y are positive. Then min_symbolic(x, y) and max_symbolic(x, y) should return True.
  • Similarly, for x and y which are both negative, nonnegative and nonpositive.
  • Similarly, if x is assumed to be negative and y is assumed to be positive, then min and max should behave respectively.
  • min and max of real symbols should be real. Similarly for algebraic, rational, integer...
  • Should min_symbolic(x, x) simplify to x?

@mathzeta
Copy link

comment:4

Replying to @pelegm:

  • Should min_symbolic(x, x) simplify to x?

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.

@jdemeyer

This comment has been minimized.

@jdemeyer jdemeyer modified the milestones: sage-7.5, sage-8.2 Apr 6, 2018
@slel

This comment has been minimized.

@slel

This comment has been minimized.

@rwst
Copy link

rwst commented Apr 7, 2018

comment:8

Replying to @pelegm:

  • Assume x and y are positive. Then min_symbolic(x, y) and max_symbolic(x, y) should return True.
  • Similarly, for x and y which are both negative, nonnegative and nonpositive.
  • Similarly, if x is assumed to be negative and y is assumed to be positive, then min and max should behave respectively.
  • min and max 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 GinacFunctions (like most elementary functions). Note also #18630, #24368, #24352, #22162 and pynac/pynac#293

@rwst
Copy link

rwst commented Apr 7, 2018

comment:9

However, it would help if min/max_symbolic would be made GinacFunctions (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.

@rwst
Copy link

rwst commented Apr 8, 2018

Dependencies: #24838

@mkoeppe mkoeppe removed this from the sage-8.2 milestone Dec 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants