Ask Your Question
0

contour and function definition

asked 2012-03-11 13:18:22 +0100

lg gravatar image

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

1 Answer

Sort by » oldest newest most voted
1

answered 2012-03-11 13:50:46 +0100

DSM gravatar image

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):

image description

edit flag offensive delete link more

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: 2012-03-11 13:18:22 +0100

Seen: 489 times

Last updated: Mar 11 '12