Bad surface for max

Hi,
With the code:

var('x,y')
plot3d(max(x,y), (x, -5, 5), (y, -5, 5))


Why don't I get something symetric by the x=y plane?

This code gives me want I thought I'd get:

var('x,y')
plot3d((x+y+abs(x-y))/2, (x, -5, 5), (y, -5, 5))

edit retag close merge delete

Sort by ยป oldest newest most voted

The issue is the following: the Python builtin max does something like

if a < b:
return b
else return a


Here, x and y are symbols, hence neither x<y nor y<x is True:

sage: var('x,y')
sage: bool(x<y)
False
sage: bool(y<x)
False


Hence:

sage: max(x,y)
x


What you need is either to use max_symbolic function:

sage: max_symbolic(x,y)
max(x, y)

sage: plot3d(max_symbolic(x,y), (x, -5, 5), (y, -5, 5))


Or to define the max as a Python function (not a symbolic expression), with one of the three equivalent syntaxes:

sage: f = lambda a,b : max(a,b)

sage: f = max

sage: def f(a,b):
....:     return max(a,b)


Then,

sage: plot3d(f, (-5, 5), (-5, 5))

more

Please note that plot3d(max, (-5, 5), (-5, 5)) works directly.

( 2018-01-13 00:56:33 -0600 )edit

Indeed, this is why i mentioned the case

sage: f = max

( 2018-01-13 08:29:38 -0600 )edit

But as it was written I thought I had to somewhat circumvent using directly max. Thanks anyway.

Or directly plot3d(max, (-5, 5), (-5, 5))

( 2018-01-14 04:42:49 -0600 )edit