Ask Your Question
1

Bad surface for `max`

asked 2018-01-12 11:51:28 +0200

ProfGra gravatar image

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 flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2018-01-12 14:42:50 +0200

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

Hence:

sage: max(x,y)
x

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)

Then,

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

Comments

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

ProfGra gravatar imageProfGra ( 2018-01-13 07:56:33 +0200 )edit

Indeed, this is why i mentioned the case

sage: f = max
tmonteil gravatar imagetmonteil ( 2018-01-13 15:29:38 +0200 )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 11:42:49 +0200 )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

Stats

Asked: 2018-01-12 11:51:28 +0200

Seen: 386 times

Last updated: Jan 12 '18