Opened 3 years ago

Last modified 20 months 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 slelievre)

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 3 years ago by pelegm

  • Cc mathzeta2 added

comment:2 follow-up: Changed 3 years ago by mathzeta2

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.

comment:3 in reply to: ↑ 2 ; follow-ups: Changed 3 years ago by pelegm

Replying to mathzeta2:

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?

comment:4 in reply to: ↑ 3 Changed 3 years ago by mathzeta2

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.

comment:5 Changed 20 months ago by jdemeyer

  • Description modified (diff)
  • Milestone changed from sage-7.5 to sage-8.2
  • Type changed from enhancement to defect

comment:6 Changed 20 months ago by slelievre

  • Cc slelievre added
  • Description modified (diff)
  • Keywords changed from min,max,days79 to min, max, days79

comment:7 Changed 20 months ago by slelievre

  • Description modified (diff)

comment:8 in reply to: ↑ 3 ; follow-up: Changed 20 months ago by rws

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 https://github.com/pynac/pynac/issues/293

comment:9 in reply to: ↑ 8 Changed 20 months ago by rws

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.

comment:10 Changed 20 months ago by rws

  • Dependencies set to #24838
Note: See TracTickets for help on using tickets.