Understanding Python lambdas

2019-05-07 15:36发布

问题:

I am trying to understand a Python program that solves differential equations numerically using the Runge-Kutta method. I have developed my own solution but was looking for other implementations. I found several but this one intrigued me as I am having a difficult time understanding how lambda works.

Here is the code:

def RK4(f):
    return lambda t, y, dt: (
        lambda dy1: (
        lambda dy2: (
        lambda dy3: (
        lambda dy4: (dy1 + 2*dy2 + 2*dy3 + dy4)/6
        )( dt * f( t + dt  , y + dy3   ) )
        )( dt * f( t + dt/2, y + dy2/2 ) )
        )( dt * f( t + dt/2, y + dy1/2 ) )
        )( dt * f( t       , y         ) )

def theory(t): return (t**2 + 4)**2 /16

from math import sqrt
dy = RK4(lambda t, y: t*sqrt(y))

t, y, dt = 0., 1., .1
while t <= 10:
    if abs(round(t) - t) < 1e-5:
        print("y(%2.1f)\t= %4.6f \t error: %4.6g" % ( t, y, abs(y - theory(t))))
    t, y = t + dt, y + dy( t, y, dt )

The long strings of lambdas and the dy function is confusing me.

First: How is the function RK4 receiving (t, y, dt) when dy is called? It looks appears that the lambda in dy = RK4(..) is only taking two parameters.

Second: How do the repeated lambda calls in RK4 work?

回答1:

First of all, read about higher-order functions.

First: How is the function RK4 receiving (t, y, dt) when dy is called? It looks appears that the lambda in dy = RK4(..) is only taking two parameters.

Ok, simple facts:

def RK4(f)

RK4 is recieving one argument. Which, in this case, is a (lambda) function:

dy = RK4(lambda t, y: t*sqrt(y))

So in this case, f is going to be lambda t, y: t*sqrt(y).

RK4 returns a function, which takes 3 arguments:

return lambda t, y, dt: ( ... )

So the call dy( t, y, dt ) is fine.



回答2:

A lambda is syntactic sugar for creating a simple function with minimal boilerplate.

For example:

 f = lamdbda x: x + 5

The above is morally equivalent to:

def __lambda_123_line_567(x): # name is for illustrative purposes, only
   return x + 5               # Python internally may name it however it
                              # chooses to do so
f = __lambda_123_line_567

The general syntax of a lambda is the keyword "lambda", followed by a list of parameters, followed by a colon, followed by the expression that should be returned by the function.



回答3:

The lambda operator or lambda function is a way to create small anonymous functions. Here's the general format:

lambda(variable: test for, apply to)


标签: python lambda