Lambda functions vs. defining functions

449 views
Skip to first unread message

Todd Zimmerman

unread,
Aug 10, 2016, 2:37:18 PM8/10/16
to sage-support
Is there any significant difference in SageMath between defining a function using lambda vs. defining it using 'def ...:'?  Both situations result in functions that can be differentiated, integrated, etc so I'm not sure if there is any functional difference between the two methods in SageMath.

-Todd


Harald Schilly

unread,
Aug 10, 2016, 2:51:38 PM8/10/16
to sage-support


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


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

William Stein

unread,
Aug 10, 2016, 2:52:32 PM8/10/16
to sage-support
Both def and lambda are from Python (unlike f(x,y) = sin(x+y), say,
which is Sage specific), so this question is heavily discussed online
already:

https://www.google.com/search?q=python+lambda+versus+def&rlz=1C5CHFA_enUS691US691&oq=python+lambda+versus+def&aqs=chrome..69i57j0.4431j0j7&sourceid=chrome&ie=UTF-8

> -Todd
>
>
> --
> 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...@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.



--
William (http://wstein.org)

Todd Zimmerman

unread,
Aug 10, 2016, 4:46:36 PM8/10/16
to sage-support
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

William Stein

unread,
Aug 10, 2016, 4:48:59 PM8/10/16
to sage-s...@googlegroups.com


On Wednesday, August 10, 2016, Todd Zimmerman <todd.zimm...@gmail.com> wrote:
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. 


No it doesn't. 

 

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


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.


--
Sent from my massive iPhone 6 plus.

Harald Schilly

unread,
Aug 10, 2016, 5:01:09 PM8/10/16
to sage-s...@googlegroups.com
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

Todd Zimmerman

unread,
Aug 10, 2016, 5:12:14 PM8/10/16
to sage-support
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)

William Stein

unread,
Aug 10, 2016, 5:14:06 PM8/10/16
to sage-s...@googlegroups.com


On Wednesday, August 10, 2016, Todd Zimmerman <todd.zimm...@gmail.com> wrote:
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)

f is a python function 

f(x) is a symbolic expression - the result of calling f with input 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.

leif

unread,
Aug 11, 2016, 3:59:39 AM8/11/16
to sage-s...@googlegroups.com
William Stein wrote:
> On Wednesday, August 10, 2016, Todd Zimmerman
> <todd.zimm...@gmail.com <mailto:todd.zimm...@gmail.com>> wrote:
>
> 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)
>
>
> f is a python function
>
> f(x) is a symbolic expression - the result of calling f with input x.

And that only works "out of the box" because x (in the global
environment) is already (pre-)defined to be a symbolic variable, hence
from Python's point of view has a value assigned such that f(x) can be
evaluated before calling diff().

I.e.,

diff(g(y),y)

won't work unless you also define y to be a symbolic variable (no matter
what formal parameter name you use in the Python function g).


-leif

Harald Schilly

unread,
Aug 11, 2016, 6:13:45 AM8/11/16
to sage-s...@googlegroups.com
I think you got it, but I'm just adding this below in case someone else is also interested:

Here, this sequence defines a symbolic x, and that function f, and then checks the types

x = var('x')
f=lambda x: x*sin(x)
type(f)
type(f(x))


and f(x) is still a symbolic expression.

Now we change x to be a floating point number

x = 9.81
type(f(x))


which gives us a *number* as a result.

And adding on top of your "conversion into symbolic expression", another POV is that you can use python functions to construct
​ ​
an
​ ​
 expression
programmatically. E.g. less trivial:

def builder(v1, n):
    ex = 1 + v1
    for i in range(n):
        ex = (i + v1) * ex^2
    return ex

x = var('x')
builder(x, 5)

gives (x + 4)*(x + 3)^2*(x + 2)^4*(x + 1)^40*x^16

-- harald



Dima Pasechnik

unread,
Aug 11, 2016, 9:19:04 AM8/11/16
to sage-support
In addition to what others have written in this thread, there is yet another potential confusion, stemming from such data types as elements of  polynomial rings. E.g.

sage: R.<x,y>=QQ[]
sage: f=2*x*y-5
sage: type(f)
<type 'sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular'>

shows that f here is neither a python function nor a symbolic expression.
Although you can convert it to the latter, as follows:

sage: SR(f)
2*x*y - 5
sage: type(SR(f))
<type 'sage.symbolic.expression.Expression'>
 
And there are more cases like this in Sage's interfaces to optimisation software.

HTH,
Dima
Reply all
Reply to author
Forward
0 new messages