Is there any significant difference in SageMath between defining a function using lambda vs. defining it using 'def ...:'?
def f1(x): return x
f2 = lambda x : x
import dis # disassembler
dis.dis(f1)
2 0 LOAD_FAST 0 (x) 3 RETURN_VALUE
dis.dis(f2)
1 0 LOAD_FAST 0 (x) 3 RETURN_VALUE
x = var("x")
f = symbolic_expression(x).function(x)
I'm aware of the difference between the two approaches in vanilla Python, I was just trying to figure out if SageMath treats the two differently.
You can integrate and differentiate both types of functions in SageMath as well as use them for solving differential equations.-Todd
On Wednesday, August 10, 2016 at 1:51:38 PM UTC-5, Harald Schilly wrote:
On Wednesday, August 10, 2016 at 8:37:18 PM UTC+2, Todd Zimmerman wrote:Is there any significant difference in SageMath between defining a function using lambda vs. defining it using 'def ...:'?This is actually a pure Python question, and the answer is yes. Technically:
def f1(x):return xf2 = lambda x : x
import dis # disassembler
dis.dis(f1)
2 0 LOAD_FAST 0 (x) 3 RETURN_VALUE
dis.dis(f2)
1 0 LOAD_FAST 0 (x) 3 RETURN_VALUE
But you cannot integrate or differentiate them! For that, you need a function/symbolic expression, which is a Sage-specific object!Under the hood, f(x) = x is constructed like:
x = var("x")
f = symbolic_expression(x).function(x)
-- harald
--
You received this message because you are subscribed to the Google Groups "sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sage-support+unsubscribe@googlegroups.com.
To post to this group, send email to sage-s...@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.
I assumed that SageMath converts the functions into symbolic expressions.If I enter the following it will work:f=lambda x: x*sin(x)diff(f(x),x)
def g(x):return x*sin(x)diff(g(x),x)
On Wednesday, August 10, 2016 at 4:01:09 PM UTC-5, Harald Schilly wrote:On Wed, Aug 10, 2016 at 10:46 PM, Todd Zimmerman
<todd.zimm...@gmail.com> wrote:
> You can integrate and differentiate both types of functions in SageMath as
> well as use them for solving differential equations.
So, can you copy/paste us an example? It does work, if that small
python-function is evaluated and returns a symbolic expression. That
will work, I don't doubt that, but the python-function in itself is
then no longer part of this. Key for understanding this is, that
nested functions are evaluated from the inside out and there is no
direct concept of lazyness in Python. In some situations, it might
look like that, so I fully understand that this is confusing.
-- h
--
You received this message because you are subscribed to the Google Groups "sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sage-support+unsubscribe@googlegroups.com.
To post to this group, send email to sage-s...@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.