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?
The lambda operator or lambda function is a way to create small anonymous functions. Here's the general format:
First of all, read about higher-order functions.
Ok, simple facts:
RK4 is recieving one argument. Which, in this case, is a (lambda) function:
So in this case,
f
is going to belambda t, y: t*sqrt(y)
.RK4 returns a function, which takes 3 arguments:
So the call
dy( t, y, dt )
is fine.A lambda is syntactic sugar for creating a simple function with minimal boilerplate.
For example:
The above is morally equivalent to:
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.