Ask Your Question

Bad surface for `max`

asked 2018-01-12 04:51:28 -0500

ProfGra gravatar image

With the code:

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:

plot3d((x+y+abs(x-y))/2, (x, -5, 5), (y, -5, 5))
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-01-12 07:42:50 -0500

tmonteil gravatar image

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)
sage: bool(y<x)


sage: max(x,y)

Which explains your plot.

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)


sage: plot3d(f, (-5, 5), (-5, 5))
edit flag offensive delete link more


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

ProfGra gravatar imageProfGra ( 2018-01-13 00:56:33 -0500 )edit

Indeed, this is why i mentioned the case

sage: f = max
tmonteil gravatar imagetmonteil ( 2018-01-13 08:29:38 -0500 )edit

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

For the completeness of the answer, could you just add:

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

ProfGra gravatar imageProfGra ( 2018-01-14 04:42:49 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2018-01-12 04:51:28 -0500

Seen: 40 times

Last updated: Jan 12