Opened 4 years ago

# Symbolic min/max

Reported by: Owned by: pelegm major sage-8.2 symbolics min, max, days79 mathzeta2, slelievre N/A #24838

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:2 follow-up: ↓ 3 Changed 4 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: ↓ 4 ↓ 8 Changed 4 years ago by pelegm

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 4 years ago by mathzeta2

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

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

### comment:7 Changed 3 years ago by slelievre

• Description modified (diff)

### comment:8 in reply to: ↑ 3 ; follow-up: ↓ 9 Changed 3 years ago by rws

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

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

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