Ask Your Question
0

What is a sage object?

asked 2013-01-03 18:22:52 +0100

anonymous user

Anonymous

updated 2013-01-03 18:29:47 +0100

How do I convert a lambda function to a sage object?

I would like to use the plot command with my lambda function

from numpy import sign
H=lambda x: (sign(x)+1)/2
P = lambda x, a,b: H(x - a) - H(x - b)
plot(P(x,5,10), (x,-10,20)
# or 
BOX510(x) = lambda x: P(x,5,10)
plot(BOX510(x), (x,-10,20)

I get error messages. I had a glance at the documentation. It says the plot command expects a Sage Object, but unfortuntely I do not have the notion for its meaning.

Eureka, it works, when I use the sign function of sage instead of the one I imported from numpy. Yet, I would like to know what a sage object is?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2013-01-03 21:08:19 +0100

kcrisman gravatar image

In general, a Sage object foo.sobj is a (Python) pickle of a Sage/Python thing (nearly anything). However, in this instance, the documentation means that one can plot functions or certain other things, like circles, arcs, as well as elliptic curves or anything else that has a .plot() method.


So much for that. What is the problem with the errors? It turns out your first thing works in Sage without numpy

H=lambda x: (sign(x)+1)/2
P = lambda x, a,b: H(x - a) - H(x - b)
plot(P(x,5,10), (x,-10,20))

because P is a function of one variable the way you made it

sage: P(x,5,10)
-1/2*sgn(x - 10) + 1/2*sgn(x - 5)

but

sage: from numpy import sign
sage: P(x,5,10)
0

because symbolic expressions apparently always have sign 1 when compared with Numpy's sign.

Similarly,

sage: BOX510 = lambda x: P(x,5,10)
sage: plot(BOX510, (x,-10,20))

works great, but try to make it BOX510(x) and you are trying to do this.

sage: preparse('BOX510(x) = lambda x: P(x,5,10)')
'__tmp__=var("x"); BOX510 = symbolic_expression(lambda x: P(x,Integer(5),Integer(10))).function(x)'

and you can't make a symbolic expression from a lambda function. The moral of the story is to be careful when mixing Sage expressions with Python ones.

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

Stats

Asked: 2013-01-03 18:22:52 +0100

Seen: 394 times

Last updated: Jan 03 '13