Opened 6 years ago

Last modified 5 years ago

# Symbolic min/max

Reported by: Owned by: Peleg Michaeli major sage-8.2 symbolics min, max, days79 Tomer Bauer, Samuel Lelièvre N/A #24838

### Description (last modified by Samuel Lelièvre)

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)
```

### comment:1 Changed 6 years ago by Peleg Michaeli

Cc: Tomer Bauer added

### comment:2 follow-up:  3 Changed 6 years ago by Tomer Bauer

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:  4  8 Changed 6 years ago by Peleg Michaeli

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 6 years ago by Tomer Bauer

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 5 years ago by Jeroen Demeyer

Description: modified (diff) sage-7.5 → sage-8.2 enhancement → defect

### comment:6 Changed 5 years ago by Samuel Lelièvre

Cc: Samuel Lelièvre added modified (diff) min,max,days79 → min, max, days79

### comment:7 Changed 5 years ago by Samuel Lelièvre

Description: modified (diff)

### comment:8 in reply to:  3 ; follow-up:  9 Changed 5 years ago by Ralf Stephan

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 `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 5 years ago by Ralf Stephan

However, it would help if `min/max_symbolic` would be made `GinacFunction`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 5 years ago by Ralf Stephan

Dependencies: → #24838
Note: See TracTickets for help on using tickets.