# contour and function definition

Hello

Let us consider this piece of code

var('x,y')
px=(x,-1,1)
py=(y,-1,1)

def V(x,y) :
if (x2+y2 <1) :
return(x+y+(abs(1-x2-y2))(1/2))
else:
return(20)
W(x,y)=x+y+(abs(1-x
2-y2))(1/2)

r=Graphics()
r+=contour_plot(W(x,y),px,py,contours=[1.70],fill=false,cmap=["red"])
r+=contour_plot(V(x,y),px,py,contours=[1.50],fill=false,cmap=["magenta"])
plot(r)

In 4.7, the level lines in magenta and red are displayed, but in 4.8 only the red one appears.

• is this change expected ?
• how to let sage knowing the definition of the V function ?
edit retag close merge delete

Sort by » oldest newest most voted

I don't have Sage 4.7 around but I'm a little surprised that it works there. The issue is that V and W aren't two different ways of expressing the same kind of thing, they're two different things.

V is a Python function, W is a Sage function.

sage: parent(V)
<type 'function'>
sage: type(V)
<type 'function'>
sage: V
<function V at 0x10f423500>
sage: parent(W)
Callable function ring with arguments (x, y)
sage: type(W)
<type 'sage.symbolic.expression.Expression'>
sage: W
(x, y) |--> x + y + sqrt(abs(-x^2 - y^2 + 1))


And when you call them with x and y as arguments:

sage: W(x,y)
x + y + sqrt(abs(-x^2 - y^2 + 1))
sage: V(x,y)
20


W returns an Expression and V returns a number. This happens because V(x,y) is evaluated immediately in the contour_plot line, it decides that it can't prove (x**2+y**2 <1) is true (which it isn't: x and y could be anything, as far as it knows) and so it returns 20 on the other branch. You're basically asking for

r+=contour_plot(20,px,py,contours=[1.50],fill=false,cmap=["magenta"])


If you want to pass the Python function itself, you can use

r+=contour_plot(V,px,py,contours=[1.50],fill=false,cmap=["magenta"])


which gave me (in 4.8):

more