Skip to content
Specular Differentiation

Scalar ODE methods

The ODE API solves scalar initial-value problems in the notation of the specular ellipse method:

\[ u'(t)=F(t,u(t)), \qquad u(t_0)=u_0, \qquad t\in[t_0,T]. \]

The public entry points are specular.euler_scheme_1, specular.euler_scheme_2, specular.euler_scheme_5, and specular.ellipse_scheme. Importing specular does not eagerly load the ODE implementation. Vector-valued states are not currently supported. In Python, F receives one NumPy float64 vector point = [t, u] of shape (2,) and returns a finite real scalar. The mathematical notation below remains \(F(t,u)\).

The same functions are also available from the public specular.ode.solver module.

The angular means used by these methods are evaluated by the currently selected calculation backend. The scalar result is converted back to a Python float for the ODE iteration; NumPy therefore remains the default and the reproducible float64 path.

The result contains numerical data only. Plotting, table generation, event handling, and dense output are intentionally outside this API.

The Scalar ODE page reproduces the seven numerical figures and four tables discussed in the manuscript.

Unscaled specular Euler methods

The functions euler_scheme_1, euler_scheme_2, and euler_scheme_5 implement the unscaled specular Euler methods of Types 1, 2, and 5. Throughout this section, the mesh is uniform and

\[ N_h:=\left\lfloor\frac{T-t_0}{h}\right\rfloor, \qquad t_n:=t_0+nh, \qquad \mathcal C:=\mathcal C_1, \]

where h is a prescribed positive step satisfying h <= T - t_0 and \(\mathcal C_1\) is the unscaled angular mean. Each method first allocates a two-row grid containing these time nodes and the initial value, then fills its solution row in place. No shortened final step is added; the last node may precede T. The same prescribed h is used in every update.

Type numbers and convergence-order labels

Type 2 here means the two-step method implemented by euler_scheme_2. On the numerical-examples page, SE2, SE3, and SE4 instead denote second-, third-, and fourth-order configurations of ellipse_scheme. In particular, ellipse SE2 is Type 5, not Type 2.

Type 1

Type 1 is the explicit two-step recurrence

\[ u_{n+1}=u_n+h\mathcal C\!\left( F(t_n,u_n),F(t_{n-1},u_{n-1}) \right), \qquad n=1,\ldots,N_h-1. \]

The value u_1 at t[1] is supplied externally. The function neither constructs nor modifies that starting value:

import numpy as np
import specular


def F(point):
    return -point[1]


t_0 = 0.0
T = 1.0
h = 0.01
u_0 = 1.0
u_1 = u_0 + h * F(np.array([t_0, u_0]))  # starter chosen by the caller

se1 = specular.euler_scheme_1(
    F,
    t_0,
    T,
    u_0,
    u_1,
    h=h,
)

Type 2

Type 2 is the explicit two-step recurrence

\[ u_{n+1}=u_n+h\mathcal C\!\left( F(t_n,u_n),\frac{u_n-u_{n-1}}{h} \right), \qquad n=1,\ldots,N_h-1. \]

It has the same external u_1 contract as SE1:

se2 = specular.euler_scheme_2(
    F,
    t_0,
    T,
    u_0,
    u_1,
    h=h,
)

Types 1 and 2 are generically only first-order consistent. No higher-order theorem is implied by these functions or by the caller's choice of u_1.

Type 5

Type 5 is the implicit one-step recurrence

\[ u_{n+1}=u_n+h\mathcal C\!\left( F(t_{n+1},u_{n+1}),F(t_n,u_n) \right), \qquad n=0,\ldots,N_h-1. \]

It is exactly the ellipse method with the fixed scale \(\sigma_n=1\):

se5 = specular.euler_scheme_5(
    F,
    t_0,
    T,
    u_0,
    h=h,
)

same_method = specular.ellipse_scheme(
    F,
    t_0,
    T,
    u_0,
    h=h,
    sigma_n=1.0,
)

euler_scheme_5 accepts the same atol, max_iter, domain, and admissible controls as ellipse_scheme. Its compatibility parameter rtol must be zero.

Prescribed scale

For a prescribed scale, the ellipse method is

\[ u_{n+1} =u_n+h\mathcal C_{\sigma_n}\!\left( F(t_{n+1},u_{n+1}),F(t_n,u_n) \right), \qquad n=0,\ldots,N_h-1. \]

Pass sigma_n as either a positive real scalar or a callable. A scalar uses the same scale at every step.

import numpy as np
import specular


def F(point):
    return -point[1]


result = specular.ellipse_scheme(
    F,
    0.0,
    1.0,
    1.0,
    h=0.01,
    sigma_n=1.0,
)

print(result.t[-1], result.u[-1])

A callable scale has the contract

sigma_n(n, t_n, u_n, h) -> positive float

It is evaluated once at the accepted left endpoint and frozen during that step's implicit solve. The index n permits prescribed sequences. The fourth argument is always the prescribed constant h, including when adjacent stored time nodes have a slightly different difference because of floating-point rounding. Consequently, the scale may depend explicitly on the mesh:

def sigma_n(n, t_n, u_n, h):
    return h**0.5

Nonlinear solve controls

Every ellipse mode and euler_scheme_5 use the manuscript's fixed-point iteration. The initial trial is the Euler predictor \(v_0=u_n+hF(t_n,u_n)\). Each iteration checks the trial, computes \(v_{m+1}=\Phi_n(v_m)\), and checks the updated value. It accepts the updated value only when

\[ |v_{m+1}-v_m|<\texttt{atol}. \]

atol is a finite positive absolute tolerance. rtol must be zero, and max_iter is the maximum number of fixed-point updates per time step. An inadmissible trial or failure to meet the strict stopping condition after max_iter updates raises RuntimeError. There are no alternative starting points or bracket-search retries. Convergence depends on the field and step size; reducing h may help. Exceptions raised by the supplied field are preserved. This API is not intended to replace a general stiff ODE solver.

The optional predicate domain(t, u) represents \(\Omega\) and is checked before field evaluations, including numerical derivative samples. admissible(t_next, v) represents the set \(V\) at the next time node and is checked before and after each fixed-point update. These predicates receive two positional floats and return booleans. When omitted, they impose no restriction beyond finite coordinates. Trial values must also lie in domain, so the implemented admissible set is contained in the corresponding slice of \(\Omega\). The initial state is checked against domain; it need not belong to the trial set at the next time.

Automatic scale-selection modes

Set exactly one of third_order, fourth_order, or auto to request automatic scale selection. Leave sigma_n=None in any of these modes:

third_order_result = specular.ellipse_scheme(
    F, 0.0, 1.0, 1.0, h=0.01, third_order=True,
)

fourth_order_result = specular.ellipse_scheme(
    F, 0.0, 1.0, 1.0, h=0.01, fourth_order=True,
)

auto_result = specular.ellipse_scheme(
    F, 0.0, 1.0, 1.0, h=0.01, auto=True,
)

third_order=True. At the left endpoint, set \(\beta_n=F(t_n,u_n)\), \(a_n=D_F^2F(t_n,u_n)\), and \(p_n=(D_FF(t_n,u_n))^2\). If the elliptic-malleability condition \(0<\beta_na_n<3p_n\) holds, select

\[ \sigma_n=\left(\frac{3\beta_np_n}{a_n}-\beta_n^2\right)^{1/2}. \]

Otherwise use sigma_n=1. The selected scale remains fixed throughout that step's implicit solve; an earlier step's scale is not reused.

fourth_order=True. At each trial endpoint, start with scale 1. When elliptic malleability holds at both endpoints and the two-endpoint coefficients satisfy \(AC<0\), use the positive defect-balancing scale

\[ \Sigma(t,x;s,y) = \left( \frac{-B+\operatorname{sgn}(A)\sqrt D}{2A} \right)^{1/2}, \qquad D=B^2-4AC. \]

The coefficients are those of the two-endpoint defect in the manuscript. They are evaluated using the accepted left state and the current trial right state. The scale is recomputed at every trial; if any condition fails, use scale 1. Right-endpoint derivatives are not evaluated when the left-endpoint condition fails. This is the pure SE4 configuration used by the paper's experiments.

auto=True. Prefer the same SE4 scale. If its conditions fail, use the left-endpoint SE3 scale when its elliptic-malleability condition holds, and use scale 1 otherwise. This fallback decision is made inside the map for each trial right endpoint. An SE3 fallback does not carry a fourth-order guarantee.

All three modes require derivatives of F along solution curves of the ODE:

\[ D_FF,\qquad D_F^2F, \qquad D_F=\partial_t+F\partial_u. \]

By default these are estimated numerically along a local RK4 flow, using centered differences and Richardson extrapolation. derivative_step may be used to set the finite-difference radius. Samples can evaluate F outside [t_0, T], so this mode requires F to be defined on a neighborhood of the time interval. Near a hard domain boundary, provide derivatives_of_F instead. For an exact, symbolic, or automatic-differentiation implementation, pass a callable mapping a length-two NumPy array to another length-two array. For the field \(F(t,u)=-u\) used above:

def derivatives_of_F(point):
    t, u = point
    return np.array([u, -u])  # D_F F and D_F^2 F for F(t,u)=-u


with_exact_derivatives = specular.ellipse_scheme(
    F, 0.0, 1.0, 1.0, h=0.01, fourth_order=True,
    derivatives_of_F=derivatives_of_F,
)

The input and output both have shape (2,). The returned components are D_F F and D_F^2 F, in that order. derivative_step cannot be supplied together with derivatives_of_F, because the callback bypasses numerical differentiation. Derivative controls are valid only in automatic modes.

The flags third_order, fourth_order, and auto are mutually exclusive. Supplying sigma_n together with any of them is also an error: a prescribed scale and an automatically selected scale are different modes.

Conditional order

Neither order flag provides an unconditional convergence-order guarantee. Third-order convergence depends on the smoothness and boundedness of the selected branch and on sufficiently accurate field derivatives. The fourth-order theorem additionally requires its uniform solution-tube hypotheses, including elliptic malleability, smoothness, boundedness, and nondegeneracy. Checking the conditions at accepted numerical pairs alone does not establish those hypotheses. Steps using a fallback scale do not inherit the fourth-order guarantee.

Finite-difference error can produce an accuracy plateau or reduced order as the mesh is refined. Rapid variation, changes of time units, or variation that is small relative to a large additive offset in F may require an explicit derivative_step or an accurate derivatives_of_F callback.

Result

ODEResult.t and ODEResult.u contain the initial value and every accepted step, with shape (N_h + 1,). They are views of separate rows of the same preallocated two-row grid. The nodes are t_0 + n*h; T is included only when it is the last represented node.

ODEResult.sigma has shape (N_h,). It records the scale actually used in the last fixed-point update that produced each accepted value, without another scale evaluation at that value. For Type 1, Type 2, and Type 5 it contains ones, representing the convention \(\mathcal C=\mathcal C_1\). In the two-step methods this also covers the externally supplied first interval; it does not describe how u_1 was produced.

In pure SE4, a fallback is recorded as 1.0. In auto mode, the recorded scale may instead be the left-endpoint SE3 scale. All recorded scales are finite and positive.

ODEResult.number_of_field_evaluations records the total number of calls to F(point) with point=[t,u] made by the solver. Calls made internally by a user-provided derivatives_of_F callback are outside this count.

See the specular.ode API reference for function signatures, parameters, and return values.