neurai.math.ode package#

Submodules#

class neurai.math.ode.euler.Euler(differential_eq, dt=None)#

Bases: ExplictRungeKutta

Euler method for solving ordinary differential equations. The ButcherTableau for Euler is:

\begin{array}{c|ccc} 0 & & \\ \hline & 1 \end{array}
Parameters:
  • differential_eq (Callable) – the differential equation

  • dt (float, optional) – the numerical precision, by default None

class neurai.math.ode.exp_euler.ExponentionalEuler(differential_eq, dt=0.1)#

Bases: ODESolver

Exponentional Euler method With ODEs of the form:

\[\frac{dy}{dt} = A - By\]

Exponential Euler update is given by:

\[y(t+dt)=y(t)e^{-B*dt} + \frac{A}{B}(1-e^{-B*dt})\]
Parameters:
  • differential_eq (Callable) – the oridinary differential equation (A - By)

  • dt (float) – the step size, by default 0.1

step(*args, **kwargs)#

The integral step function

Parameters:

args (float) – the current value

class neurai.math.ode.heun.Heun2(differential_eq, dt=0.1)#

Bases: ExplictRungeKutta

Heun’s second-order method for solving ordinary differential equations. The ButcherTableau for HEUN2 is:

\begin{array}{c|ccc} 0 & & \\ 1 & 1 & \\ \hline & 1 / 2 & 1 / 2 \end{array}
Parameters:
  • differential_eq (Callable) – the differential equation

  • dt (float, optional) – the numerical precision, by default 0.1

class neurai.math.ode.heun.Heun3(differential_eq, dt=0.1)#

Bases: ExplictRungeKutta

Heun’s third-order method for solving ordinary differential equations. The ButcherTableau for HEUN3 is:

\begin{array}{c|ccc} 0 & & & \\ 1 / 3 & 1 / 3 & & \\ 2 / 3 & 0 & 2 / 3 & \\ \hline & 1 / 4 & 0 & 3 / 4 \end{array}
Parameters:
  • differential_eq (Callable) – the differential equation

  • dt (float, optional) – the numerical precision, by default 0.1

class neurai.math.ode.implict_euler.ImplictEuler(differential_eq, dt=0.1, rtol=0.001)#

Bases: ODESolver

Implict Euler method, AKA Backward Euler method This method computes the approximations using

\[y_{k+1} = y_{k} + hf(t_{k+1}, y_{k+1})\]
Parameters:
  • differential_eq (Callable) – the oridinary differential equation

  • dt (float, optional) – the step size, by default 0.1

  • rtol (float, optional) – The relative tolerance, by default 1e-3

class neurai.math.ode.midpoint.MidPoint(differential_eq, dt=None)#

Bases: ExplictRungeKutta

Midpoint method for solving ordinary differential equations. Also called second-order Runge-Kutta, it is same as RK2. The ButcherTableau for MIDPOINT is:

\begin{array}{c|ccc} 0 & & \\ 1 / 2 & 1 / 2 & \\ \hline & 0 & 1 \end{array}
Parameters:
  • differential_eq (Callable) – the differential equation

  • dt (float, optional) – the numerical precision, by default None

class neurai.math.ode.ode.ODESolver(differential_eq, dt=0.1)#

Bases: Solver

Numerical solvers for Ordinary Differential Equations (ODEs)

Parameters:
  • differential_eq (Callable) – the differential equations

  • dt (float, optional) – the step size, by default None

neurai.math.ode.ode.ode_solve(method=None, differential_eq=None, dt=None, **kwargs)#

Instantiate a numerical solver for ODEs.

Parameters:
  • method (SolverMethod, optional) – the method name for solving differential equations, by default None

  • differential_eq (Callable, optional) – the differential equation, by default None

  • dt (float, optional) – the step size, by default None

Returns:

diffeq_solve (ODESolver) – the numerical solver of ‘differential_eq’

neurai.math.ode.ode.register_ode_solver(name, odesolver)#

Register a new ode solver

Parameters:
  • name (SolverMethod_ODE) – the name of solver method for ODE

  • odesolver (ODE) – the solver of ode

class neurai.math.ode.ralston.Ralston2(differential_eq, dt=0.1)#

Bases: ExplictRungeKutta

Ralston’s second-order method for solving ordinary differential equations. The ButcherTableau for RALSTON2 is:

\begin{array}{c|ccc} 0 & & \\ 2 / 3 & 2 / 3 & \\ \hline & 1/4 & 3/4 \end{array}
Parameters:
  • differential_eq (Callable) – the differential equation

  • dt (float, optional) – the numerical precision, by default 0.1

class neurai.math.ode.ralston.Ralston3(differential_eq, dt=0.1)#

Bases: ExplictRungeKutta

Ralston’s third-order method for solving ordinary differential equations. The ButcherTableau for RALSTON3 is:

\begin{array}{c|ccc} 0 & & & \\ 1 / 2 & 1 / 2 & & \\ 3 / 4 & 0 & 3 / 4 & \\ \hline & 2 / 9 & 1 / 3 & 4 / 9 \end{array}
Parameters:
  • differential_eq (Callable) – the differential equation

  • dt (float, optional) – the numerical precision, by default 0.1

class neurai.math.ode.ralston.Ralston4(differential_eq, dt=0.1)#

Bases: ExplictRungeKutta

Ralston’s forth-order method for solving ordinary differential equations. The ButcherTableau for RALSTON4 is:

\begin{array}{c|cccc} 0 & & & & \\ .4 & .4 & & & \\ .45573725 & .29697761 & .15875964 & & \\ 1 & .21810040 & -3.05096516 & 3.83286476 & \\ \hline & .17476028 & -.55148066 & 1.20553560 & .17118478 \end{array}
Parameters:
  • differential_eq (Callable) – the differential equation

  • dt (float, optional) – the numerical precision, by default 0.1

class neurai.math.ode.rk.RK2(differential_eq, dt=0.1)#

Bases: ExplictRungeKutta

A second-order Runge-Kutta method for solving ordinary differential equations. The ButcherTableau for RK2 is:

\begin{array}{c|ccc} 0 & & \\ 1 / 2 & 1 / 2 & \\ \hline & 0 & 1 \end{array}
Parameters:
  • differential_eq (Callable) – the differential equation

  • dt (float, optional) – the numerical precision, by default 0.1

class neurai.math.ode.rk.RK3(differential_eq, dt=0.1)#

Bases: ExplictRungeKutta

A third-order Runge-Kutta method for solving ordinary differential equations. The ButcherTableau for RK3 is:

\begin{array}{c|ccc} 0 & & & \\ 1 / 2 & 1 / 2 & & \\ 1 & -1 & 2 & \\ \hline & 1 / 6 & 2 / 3 & 1 / 6 \end{array}
Parameters:
  • differential_eq (Callable) – the differential equation

  • dt (float, optional) – the numerical precision, by default 0.1

class neurai.math.ode.rk.RK4(differential_eq, dt=0.1)#

Bases: ExplictRungeKutta

A forth-order Runge-Kutta method for solving ordinary differential equations. The ButcherTableau for RK4 is:

\begin{array}{c|cccc} 0 & & & & \\ 1 / 2 & 1 / 2 & & & \\ 1 / 2 & 0 & 1 / 2 & & \\ 1 & 0 & 0 & 1 & \\ \hline & 1 / 6 & 1 / 3 & 1 / 3 & 1 / 6 \end{array}
Parameters:
  • differential_eq (Callable) – the differential equation

  • dt (float, optional) – the numerical precision, by default 0.1

class neurai.math.ode.rk.RKF45(differential_eq, dt=0.1)#

Bases: AdaptiveRungekutta

RKF45 method for solving ordinary differential equations. The ButcherTableau for RKF45 is:

\begin{array}{c|cccccc} 0 & & & & & & \\ 1/4 & 1/4 & & & & & \\ 3/8 & 3/32 & 9/32 & & & & \\ 2/13 & 1932/2197 & -7200/2197 & 7296/2197 & & & \\ 1 & 439/216 & -8 & 3680/513 & -845/4104 & & \\ 1/2 & 8/27 & 2 & -3544/2565 & 1859/4104 & -11/40 & \\ \hline & 16/135 & 0 & 6656/12825 & 28561/56430 & -9/50 & 2/55 \\ & 25/216 & 0 & 1408/2565 & 2197/4104 & -1/5 & 0 \end{array}
Parameters:
  • differential_eq (Callable) – the differential equation

  • dt (float, optional) – the numerical precision, by default 0.1

class neurai.math.ode.runge_kutta.AdaptiveRungekutta(differential_eq, dt=0.1, rtol=0.001, safety_factor=0.9)#

Bases: ODESolver

Adaptive Runge-kutta method for ordinary differential equations. This method is designed to produce an estimate of the local truncation error of a single Runge-Kutta step, and as result, allow to control the error with adaptive step-size.This is done by having two methods, one with order \(p\) and one with order \(p-1\).

\(k_i\) is calculated by:

\[k_i = f(t_n + c_i h, y_n + h \sum_{j=1}^s a_{ij} k_j)\]

The lower-order step is given by:

\[y_{n+1}^{(1)} = y_n + h \sum_{i=1}^s b_i^{(1)} k_i\]

where \(k_i\) are the same as higher-oder method:

\[y_{n+1}^{(2)} = y_n + h \sum_{i=1}^s b_i^{(2)} k_i\]

Then the error is:

\[\text{error} = |y_{n+1}^{(1)} - y_{n+1}^{(2)}|\]

If \({error} \leq {rtol}\), then the step is completed. If \({error}\) is very small, the \(h\) will increase.

If \({error} \gt {rtol}\), the h will decrease, then replace \(h\) with \(h_{new}\) and repeat the step.

Specifically referred to ampl/gsl.

Parameters:
  • differential_eq (Callable) – the differential equation

  • dt (float, optional) – the step size, by default 0.1

  • rtol (float, optional) – The relative tolerance used to determine the acceptable local error, by default 1e-3

  • safety_factor (float, optional) – The safety factor used to scale the step size, by default 0.9

step(*args, **kwargs)#

Perform an adaptive Runge-Kutta step

class neurai.math.ode.runge_kutta.ExplictRungeKutta(differential_eq, dt=None)#

Bases: ODESolver

The general generic form of an explicit Runge Kutta is

\[k_{i}=f(t_{n}+c_{i}h,y_{n}+h\sum _{j=1}^{s}a_{ij}k_{j})\]
\[y_{n+1}=y_{n}+h\sum _{i=1}^{s}b_{i}k_{i}\]
Parameters:
  • differential_eq (Callable) – the differential equation

  • dt (float, optional) – the step size, by default None

step(*args, **kwargs)#

Perform a explict Runge-Kutta step

Module contents#