ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 03 Jan 2013 21:08:19 +0100What is a sage object?https://ask.sagemath.org/question/9680/what-is-a-sage-object/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?Thu, 03 Jan 2013 18:22:52 +0100https://ask.sagemath.org/question/9680/what-is-a-sage-object/Answer by kcrisman for <p>How do I convert a lambda function to a sage object?</p>
<p>I would like to use the plot command with my lambda function </p>
<pre><code>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)
</code></pre>
<p>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.</p>
<p>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?</p>
https://ask.sagemath.org/question/9680/what-is-a-sage-object/?answer=14353#post-id-14353In 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.Thu, 03 Jan 2013 21:08:19 +0100https://ask.sagemath.org/question/9680/what-is-a-sage-object/?answer=14353#post-id-14353