brainpy.integration package

diff_equation module

Expression(var, code)

DiffEquation(func)

Differential Equation.

class brainpy.integration.Expression(var, code)[source]
class brainpy.integration.DiffEquation(func)[source]

Differential Equation.

A differential equation is defined as the standard form:

dx/dt = f(x) + g(x) dW

Parameters

func (callable) – The user defined differential equation.

replace_f_expressions(name, y_sub, t_sub=None)[source]

Replace expressions of df part.

Parameters
  • name (str) – The name of the new expression.

  • y_sub (str) – The new name of the variable “y”.

  • t_sub (str, optional) – The new name of the variable “t”.

Returns

list_of_expr – A list of expressions.

Return type

list

integrator module

integrate([func, method])

Generate the one-step integrator function for differential equations.

Integrator(diff_eq)

Euler(diff_eq)

Forward Euler method.

Heun(diff_eq)

Two-stage method for numerical integrator.

MidPoint(diff_eq)

Explicit midpoint Euler method.

RK2(diff_eq[, beta])

Parametric second-order Runge-Kutta (RK2).

RK3(diff_eq)

Kutta’s third-order method (commonly known as RK3).

RK4(diff_eq)

Fourth-order Runge-Kutta (RK4) 9 10 11 .

RK4Alternative(diff_eq)

An alternative of fourth-order Runge-Kutta method.

ExponentialEuler(diff_eq)

First order, explicit exponential Euler method.

MilsteinIto(diff_eq)

Itô stochastic integral.

MilsteinStra(diff_eq)

Heun two-stage stochastic numerical method for Stratonovich integral.

class brainpy.integration.Integrator(diff_eq)[source]
class brainpy.integration.Euler(diff_eq)[source]

Forward Euler method. Also named as explicit_Euler.

The simplest way for solving ordinary differential equations is “the Euler method” by Press et al. (1992) 1 :

\[y_{n+1} = y_n + f(y_n, t_n) \Delta t\]

This formula advances a solution from \(y_n\) to \(y_{n+1}=y_n+h\). Note that the method increments a solution through an interval \(h\) while using derivative information from only the beginning of the interval. As a result, the step’s error is \(O(h^2)\).

For SDE equations, this approximation is a continuous time stochastic process that satisfy the iterative scheme 1.

\[Y_{n+1} = Y_n + f(Y_n)h_n + g(Y_n)\Delta W_n\]

where \(n=0,1, \cdots , N-1\), \(Y_0=x_0\), \(Y_n = Y(t_n)\), \(h_n = t_{n+1} - t_n\) is the step size, \(\Delta W_n = [W(t_{n+1}) - W(t_n)] \sim N(0, h_n)=\sqrt{h}N(0, 1)\) with \(W(t_0) = 0\).

For simplicity, we rewrite the above equation into

\[Y_{n+1} = Y_n + f_n h + g_n \Delta W_n\]

As the order of convergence for the Euler-Maruyama method is low (strong order of convergence 0.5, weak order of convergence 1), the numerical results are inaccurate unless a small step size is used. By adding one more term from the stochastic Taylor expansion, one obtains a 1.0 strong order of convergence scheme known as Milstein scheme 2.

Parameters

diff_eq (DiffEquation, callable) – The differential equation.

Returns

func – The one-step numerical integrator function.

Return type

callable

References

1(1,2)

W. H.; Flannery, B. P.; Teukolsky, S. A.; and Vetterling, W. T. Numerical Recipes in FORTRAN: The Art of Scientific Computing, 2nd ed. Cambridge, England: Cambridge University Press, p. 710, 1992.

2

U. Picchini, Sde toolbox: Simulation and estimation of stochastic differential equations with matlab.

class brainpy.integration.Heun(diff_eq)[source]

Two-stage method for numerical integrator.

For ODE, please see “RK2”.

For stochastic Stratonovich integral, the Heun algorithm is given by, according to paper 4 5.

\[ \begin{align}\begin{aligned}Y_{n+1} &= Y_n + f_n h + {1 \over 2}[g_n + g(\overline{Y}_n)] \Delta W_n\\\overline{Y}_n &= Y_n + g_n \Delta W_n\end{aligned}\end{align} \]

Or, it is written as

\[ \begin{align}\begin{aligned}Y_1 &= y_n + f(y_n)h + g_n \Delta W_n\\y_{n+1} &= y_n + {1 \over 2}[f(y_n) + f(Y_1)]h + {1 \over 2} [g(y_n) + g(Y_1)] \Delta W_n\end{aligned}\end{align} \]
Parameters

diff_eq (DiffEquation) – The differential equation.

Returns

func – The one-step numerical integrator function.

Return type

callable

References

4

H. Gilsing and T. Shardlow, SDELab: A package for solving stochastic differential equations in MATLAB, Journal of Computational and Applied Mathematics 205 (2007), no. 2, 1002-1018.

5

P.reversal_potential. Kloeden, reversal_potential. Platen, and H. Schurz, Numerical solution of SDE through computer experiments, Springer, 1994.

class brainpy.integration.MidPoint(diff_eq)[source]

Explicit midpoint Euler method. Also named as modified_Euler.

Parameters

diff_eq (DiffEquation) – The differential equation.

Returns

func – The one-step numerical integrator function.

Return type

callable

See also

RK2, Heun

class brainpy.integration.RK2(diff_eq, beta=0.6666666666666666)[source]

Parametric second-order Runge-Kutta (RK2). Also named as RK2.

It is given in parametric form by 3 .

\[\begin{split}k_1 &= f(y_n, t_n) \\ k_2 &= f(y_n + \beta \Delta t k_1, t_n + \beta \Delta t) \\ y_{n+1} &= y_n + \Delta t [(1-\frac{1}{2\beta})k_1+\frac{1}{2\beta}k_2]\end{split}\]
Parameters
  • diff_eq (DiffEquation) – The differential equation.

  • beta (float) – Popular choices for ‘beta’: 1/2 : explicit midpoint method 2/3 : Ralston’s method 1 : Heun’s method, also known as the explicit trapezoid rule

Returns

func – The one-step numerical integrator function.

Return type

callable

References

3

https://lpsa.swarthmore.edu/NumInt/NumIntSecond.html

See also

Heun, MidPoint

class brainpy.integration.RK3(diff_eq)[source]

Kutta’s third-order method (commonly known as RK3). Also named as RK3 6 7 8 .

\[\begin{split}k_1 &= f(y_n, t_n) \\ k_2 &= f(y_n + \frac{\Delta t}{2}k_1, tn+\frac{\Delta t}{2}) \\ k_3 &= f(y_n -\Delta t k_1 + 2\Delta t k_2, t_n + \Delta t) \\ y_{n+1} &= y_{n} + \frac{\Delta t}{6}(k_1 + 4k_2+k_3)\end{split}\]
Parameters

diff_eq (DiffEquation) – The differential equation.

Returns

func – The one-step numerical integrator function.

Return type

callable

References

6

http://mathworld.wolfram.com/Runge-KuttaMethod.html

7

https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods

8

https://zh.wikipedia.org/wiki/龙格-库塔法

class brainpy.integration.RK4(diff_eq)[source]

Fourth-order Runge-Kutta (RK4) 9 10 11 .

\[\begin{split}k_1 &= f(y_n, t_n) \\ k_2 &= f(y_n + \frac{\Delta t}{2}k_1, t_n + \frac{\Delta t}{2}) \\ k_3 &= f(y_n + \frac{\Delta t}{2}k_2, t_n + \frac{\Delta t}{2}) \\ k_4 &= f(y_n + \Delta t k_3, t_n + \Delta t) \\ y_{n+1} &= y_n + \frac{\Delta t}{6}(k_1 + 2*k_2 + 2* k_3 + k_4)\end{split}\]
Parameters

diff_eq (DiffEquation) – The differential equation.

Returns

func – The one-step numerical integrator function.

Return type

callable

References

9(1,2)

http://mathworld.wolfram.com/Runge-KuttaMethod.html

10(1,2)

https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods

11(1,2)

https://zh.wikipedia.org/wiki/龙格-库塔法

class brainpy.integration.RK4Alternative(diff_eq)[source]

An alternative of fourth-order Runge-Kutta method. Also named as RK4_alternative (“3/8” rule).

It is a less often used fourth-order explicit RK method, and was also proposed by Kutta 12:

\[\begin{split}k_1 &= f(y_n, t_n) \\ k_2 &= f(y_n + \frac{\Delta t}{3}k_1, t_n + \frac{\Delta t}{3}) \\ k_3 &= f(y_n - \frac{\Delta t}{3}k_1 + \Delta t k_2, t_n + \frac{2 \Delta t}{3}) \\ k_4 &= f(y_n + \Delta t k_1 - \Delta t k_2 + \Delta t k_3, t_n + \Delta t) \\ y_{n+1} &= y_n + \frac{\Delta t}{8}(k_1 + 3*k_2 + 3* k_3 + k_4)\end{split}\]
Parameters

diff_eq (DiffEquation) – The differential equation.

Returns

func – The one-step numerical integrator function.

Return type

callable

References

12

https://en.wikipedia.org/wiki/List_of_Runge%E2%80%93Kutta_methods

class brainpy.integration.ExponentialEuler(diff_eq)[source]

First order, explicit exponential Euler method.

For an ODE equation of the form

\[y^{\prime}=f(y), \quad y(0)=y_{0}\]

its schema is given by

\[y_{n+1}= y_{n}+h \varphi(hA) f (y_{n})\]

where \(A=f^{\prime}(y_{n})\) and \(\varphi(z)=\frac{e^{z}-1}{z}\).

For linear ODE system: \(y^{\prime} = Ay + B\), the above equation is equal to

\[y_{n+1}= y_{n}e^{hA}-B/A(1-e^{hA})\]

For a SDE equation of the form

\[d y=(Ay+ F(y))dt + g(y)dW(t) = f(y)dt + g(y)dW(t), \quad y(0)=y_{0}\]

its schema is given by 16

\[\begin{split}y_{n+1} & =e^{\Delta t A}(y_{n}+ g(y_n)\Delta W_{n})+\varphi(\Delta t A) F(y_{n}) \Delta t \\ &= y_n + \Delta t \varphi(\Delta t A) f(y) + e^{\Delta t A}g(y_n)\Delta W_{n}\end{split}\]

where \(\varphi(z)=\frac{e^{z}-1}{z}\).

Parameters

diff_eq (DiffEquation) – The differential equation.

Returns

func – The one-step numerical integrator function.

Return type

callable

References

16

Erdoğan, Utku, and Gabriel J. Lord. “A new class of exponential integrators for stochastic differential equations with multiplicative noise.” arXiv preprint arXiv:1608.07096 (2016).

class brainpy.integration.MilsteinIto(diff_eq)[source]

Itô stochastic integral. The derivative-free Milstein method is an order 1.0 strong Taylor schema.

The following implementation approximates this derivative thanks to a Runge-Kutta approach 13.

In Itô scheme, it is expressed as

\[Y_{n+1} = Y_n + f_n h + g_n \Delta W_n + {1 \over 2\sqrt{h}} [g(\overline{Y_n}) - g_n] [(\Delta W_n)^2-h]\]

where \(\overline{Y_n} = Y_n + f_n h + g_n \sqrt{h}\).

Parameters

diff_eq (DiffEquation) – The differential equation.

Returns

func – The one-step numerical integrator function.

Return type

callable

References

13

P.reversal_potential. Kloeden, reversal_potential. Platen, and H. Schurz, Numerical solution of SDE through computer experiments, Springer, 1994.

class brainpy.integration.MilsteinStra(diff_eq)[source]

Heun two-stage stochastic numerical method for Stratonovich integral.

Use the Stratonovich Heun algorithm to integrate Stratonovich equation, according to paper 14 15.

\[ \begin{align}\begin{aligned}Y_{n+1} &= Y_n + f_n h + {1 \over 2}[g_n + g(\overline{Y}_n)] \Delta W_n\\\overline{Y}_n &= Y_n + g_n \Delta W_n\end{aligned}\end{align} \]

Or, it is written as

\[ \begin{align}\begin{aligned}Y_1 &= y_n + f(y_n)h + g_n \Delta W_n\\y_{n+1} &= y_n + {1 \over 2}[f(y_n) + f(Y_1)]h + {1 \over 2} [g(y_n) + g(Y_1)] \Delta W_n\end{aligned}\end{align} \]
Parameters

diff_eq (DiffEquation) – The differential equation.

Returns

func – The one-step numerical integrator function.

Return type

callable

References

14

H. Gilsing and T. Shardlow, SDELab: A package for solving stochastic differential equations in MATLAB, Journal of Computational and Applied Mathematics 205 (2007), no. 2, 1002-1018.

15

P.reversal_potential. Kloeden, reversal_potential. Platen, and H. Schurz, Numerical solution of SDE through computer experiments, Springer, 1994.

See also

MilsteinIto

sympy_tools module

Parser(expr)

Printer([settings])

Printer that overrides the printing of some basic sympy objects.

str2sympy(str_expr)

sympy2str(sympy_expr)

class brainpy.integration.Parser(expr)[source]
render_BinOp_parentheses(left, right, op)[source]

Use a simplified checking whether it is possible to omit parentheses: only omit parentheses for numbers, variable names or function calls. This means we still put needless parentheses because we ignore precedence rules, e.g. we write “3 + (4 * 5)” but at least we do not do “(3) + ((4) + (5))”

render_element_parentheses(node)[source]

Render an element with parentheses around it or leave them away for numbers, names and function calls.

class brainpy.integration.Printer(settings=None)[source]

Printer that overrides the printing of some basic sympy objects. reversal_potential.g. print “a and b” instead of “And(a, b)”.