Skip to main content

Collocation-method solvers for Volterra integral and integro-differential equations

Project description

volterra-equation-solvers

Tests (Linux) Tests (macOS) Tests (Windows) License: GPL v3 Python Documentation

Volterra equation solvers mascot

Collocation-method solvers for Volterra integral and integro-differential equations, based on:

Brunner H. Collocation Methods for Volterra Integral and Related Functional Differential Equations. Cambridge University Press; 2004.

The solvers are implemented as a compiled extension written in the D language. Performance should be on par with optimized C or FORTRAN code. All solvers support real-valued and complex-valued data.

Solvers

solve_VIE_1

Given functions $K$ and $g$, solves for $y(t)$ in the Type-1 Volterra integral equation (VIE-1):

$$g(t) = \int_0^t K(t-s)\, y(s)\, ds$$

The solver handles the following cases:

  • All functions are scalar-valued.
  • $y(t)$ and $g(t)$ are $d$-dimensional vectors and $K(t)$ is a $d \times d$ matrix.
  • $y(t)$ and $g(t)$ are $d \times m$ matrices and $K(t)$ is a $d \times d$ matrix.

For a full description of the solver inputs/outputs and available settings, see: API reference

solve_VIE_2

Given functions $K$ and $g$, solves for $y(t)$ in the Type-2 Volterra integral equation (VIE-2):

$$y(t) = g(t) + \int_0^t K(t-s)\, y(s)\, ds$$

The solver handles the following cases:

  • All functions are scalar-valued.
  • $y(t)$ and $g(t)$ are $d$-dimensional vectors and $K(t)$ is a $d \times d$ matrix.
  • $y(t)$ and $g(t)$ are $d \times m$ matrices and $K(t)$ is a $d \times d$ matrix.

For a full description of the solver inputs/outputs and available settings, see: API reference

solve_VIDE

Given functions $K$, $a$, and $g$ and an initial value $y(0)$, solves for $y(t)$ in the Volterra integro-differential equation (VIDE):

$$y'(t) = a(t)\, y(t) + g(t) + \int_0^t K(t-s)\, y(s)\, ds$$

The solver handles the following cases:

  • All functions are scalar-valued.
  • $y(t)$ and $g(t)$ are $d$-dimensional vectors and $K(t)$ and $a(t)$ are $d \times d$ matrices.
  • $y(t)$ and $g(t)$ are $d \times m$ matrices and $K(t)$ and $a(t)$ are $d \times d$ matrices.

For a full description of the solver inputs/outputs and available settings, see: API reference

Installation

pip install volterra-equation-solvers

Pre-built wheels are provided for Linux x86_64, macOS arm64 (Apple Silicon), and Windows x64. The D extension is bundled in the wheel and requires no extra tooling. Intel Macs are no longer supported as of 0.3.2; users can pin to volterra-equation-solvers==0.3.1 or build from source (see CONTRIBUTING.md).

Requirements: Python ≥ 3.10, numpy Optional: numba, scipy (only needed for non-standard collocation settings not supported by the D extension)

To build from source (e.g. on an unsupported platform), see CONTRIBUTING.md.

Quick start

import numpy as np
from volterra_equation_solvers import solve_VIE_2

# y(t) = sin(t) satisfies this VIE-2 with K(s) = exp(-s)
time_step = 0.05
times = np.arange(0, 2.1, time_step)   # 42 points
kernel = np.exp(-times)
g = np.sin(times) - 0.5*(np.exp(-times) + np.sin(times) - np.cos(times))

# Default solver settings require length of form 4k+1; input will be
# truncated from 42 to 41, so soln has 41 elements, not 42.
soln = solve_VIE_2(
    kernel_values=kernel,
    g_values=g,
    time_step=time_step,
)
print(f"Max error: {max(abs(soln - np.sin(times[:len(soln)]))):.2e}")

All solvers accept return_polys=True to also return the piecewise polynomial solution as a list of numpy.polynomial.Polynomial objects.

The solvers require input arrays to satisfy an internal size constraint. Any length can be passed; if the length doesn't meet the constraint, the arrays are automatically truncated to the nearest valid length and a warning is printed. See the API reference for each solver for details.

Vector and Matrix Valued Equations

All three solvers can solve for vector-valued and matrix-valued functions $y(t)$. When $y(t)$ is a $d$-dimensional vector, $g(t)$ is also a $d$-dimensional vector and $K(t)$ and $a(t)$ are $d \times d$ matrices. When $y(t)$ is a $d \times m$ matrix, $g(t)$ is also a $d \times m$ matrix and $K(t)$ and $a(t)$ are $d \times d$ matrices. The solver detects which case applies automatically from the shapes of the input arrays.

import numpy as np
from volterra_equation_solvers import solve_VIE_1

# 2×2 VIE-1 with constant kernel K = [[3/2, -1/2], [-1/2, 3/2]],
# g(t) = [t + (3/2)t², t - (1/2)t²], and exact solution y(t) = [1+2t, 1]
time_step = 0.1
times = np.arange(0, 9.1, time_step)   # 91 pts = 10×3² + 1
N = len(times)

kernel = np.full((N, 2, 2), [[1.5, -0.5], [-0.5, 1.5]])

g = np.zeros((N, 2))
g[:, 0] = times + 1.5 * times**2
g[:, 1] = times - 0.5 * times**2

soln = solve_VIE_1(kernel_values=kernel, g_values=g, time_step=time_step)
# soln shape: (N, 2)
exact = np.column_stack([1 + 2*times, np.ones(N)])
print(f"Max error: {np.max(np.abs(soln - exact)):.2e}")

Complex-Valued Equations

All three solvers accept complex-valued inputs. Pass complex NumPy arrays for the kernel, forcing function, and (for VIDE) initial value, and the solver returns a complex-valued solution. This works for scalar, vector, and matrix cases alike.

import numpy as np
from volterra_equation_solvers import solve_VIE_2

time_step = 0.05
times = np.arange(0, 2.1, time_step)
kernel = np.exp(-1j * times)               # complex kernel
g = np.ones_like(times, dtype=complex)

soln = solve_VIE_2(kernel_values=kernel, g_values=g, time_step=time_step)
# soln is a complex-valued array

How the Collocation Method Works

The solvers approximate each component of $y(t)$ as a piecewise polynomial. The time axis is divided into mesh intervals, and on each interval the solution is represented by a polynomial whose coefficients are determined by requiring the Volterra equation to hold exactly at a set of collocation points within that interval.

Because the solution on each mesh interval is an explicit polynomial, the solver can optionally return it (see Polynomial Solutions below). This is useful for evaluating the solution at arbitrary times, differentiating, integrating, and so on.

Piecewise Polynomial Illustration

The figure below illustrates the idea. The time axis is split into mesh intervals (dashed vertical lines), and on each interval the solver finds a polynomial $p_i(t)$ that satisfies the equation at a set of collocation points (dots). Stitching the pieces together produces a piecewise polynomial that closely tracks the exact solution.

Piecewise polynomial illustration

Polynomial Solutions

Passing return_polys=True to any solver returns a (soln_values, polys) tuple, where polys is a list of numpy.polynomial.Polynomial objects covering successive mesh intervals. These can be evaluated at any point, differentiated, integrated, and so on. The following example uses solve_VIDE to solve for $y(t) = \sin(t)$, then evaluates the solution and its derivative at a point not on the time grid:

import numpy as np
from volterra_equation_solvers import solve_VIDE

# y(t) = sin(t) satisfies this VIDE with K(s) = exp(-s), a(t) = -1
time_step = 0.1
times = np.arange(0, 9.1, time_step)   # 91 points
kernel = np.exp(-times)
a = np.full(len(times), -1.0)
g = 1.5*np.cos(times) + 0.5*np.sin(times) - 0.5*np.exp(-times)

soln_vals, polys = solve_VIDE(
    kernel_values=kernel,
    a_values=a,
    g_values=g,
    soln_init_value=0.0,
    time_step=time_step,
    return_polys=True,
)

# polys[i] is a numpy.polynomial.Polynomial covering mesh interval i.
# Evaluate at a point not on the time grid:
p = polys[0]                            # covers t ∈ [0, 0.4]
print(f"y(0.2)  ≈ {p(0.2):.6f},  exact = {np.sin(0.2):.6f}")

# Differentiate to recover y'(t):
print(f"y'(0.2) ≈ {p.deriv()(0.2):.6f},  exact = {np.cos(0.2):.6f}")

Benchmarks

All three solvers have the same expected asymptotic complexity in N, d, and m, where N is the number of input points, d is the number of rows in the solution, and m is the number of columns:

Scalar Vector (d×1) Matrix (d×m)*
Time O(N²) O(N²d²) O(N²d²m)
Memory O(N) O(Nd²) O(Nd²m)

* The m columns of the solution are independent and the code runs them in parallel.

The quadratic time scaling arises because each new mesh step requires a history sum over all previous steps. The coll_divs and coll_choices parameters affect the constant factor but not the asymptotic scaling in N, d, and m.

Run on a GitHub Actions ubuntu-22.04 runner (2-core x86_64 VM on an Intel Xeon 8370C, 2.8 GHz base / 3.5 GHz boost). Mean time is averaged over a variable number of calibrated rounds (from ~9 for large inputs up to ~6000 for small inputs).

Benchmarks

See the Getting Started page for complete examples.

Worked derivations of the analytic solutions used in the test suite are in docs/scalar_solutions.pdf and docs/coupled_vector_solutions.pdf.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

volterra_equation_solvers-0.3.2-py3-none-win_amd64.whl (5.7 MB view details)

Uploaded Python 3Windows x86-64

volterra_equation_solvers-0.3.2-py3-none-manylinux_2_31_x86_64.whl (10.6 MB view details)

Uploaded Python 3manylinux: glibc 2.31+ x86-64

volterra_equation_solvers-0.3.2-py3-none-macosx_11_0_arm64.whl (9.3 MB view details)

Uploaded Python 3macOS 11.0+ ARM64

File details

Details for the file volterra_equation_solvers-0.3.2-py3-none-win_amd64.whl.

File metadata

File hashes

Hashes for volterra_equation_solvers-0.3.2-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 c038742296ef3312c670fedc032480d31f34ff27b76f90ed534e22006f05eb2f
MD5 c3cd2c8ed914038b491984780b11ea8e
BLAKE2b-256 2218a0c7c709e4b5ed597a14e69e11888000c9ac2197c194731ad4f28f66ac1b

See more details on using hashes here.

Provenance

The following attestation bundles were made for volterra_equation_solvers-0.3.2-py3-none-win_amd64.whl:

Publisher: build-wheels.yml on trout314/volterra-equation-solvers

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file volterra_equation_solvers-0.3.2-py3-none-manylinux_2_31_x86_64.whl.

File metadata

File hashes

Hashes for volterra_equation_solvers-0.3.2-py3-none-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 f6f61789f4a06a9bd8e6662525a391f201712a08605a8c4ead615337fb60dda1
MD5 f7c1daaaebf0746caedea7a756b24eaf
BLAKE2b-256 80ee749b9b2fa4adfcccafe2ae977c650bfd7ef91fc5b734d69769c712bd8275

See more details on using hashes here.

Provenance

The following attestation bundles were made for volterra_equation_solvers-0.3.2-py3-none-manylinux_2_31_x86_64.whl:

Publisher: build-wheels.yml on trout314/volterra-equation-solvers

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file volterra_equation_solvers-0.3.2-py3-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for volterra_equation_solvers-0.3.2-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7e43b956eeb1a62e941fa996c9fca7c5458a817ba39e8149fe5f3bc310b4c53d
MD5 3d0841a729429f19e6ae83de63b06d4f
BLAKE2b-256 e83306892bd974c7b739f8a98ca3981164ed69e429535f6896ba637133c335c2

See more details on using hashes here.

Provenance

The following attestation bundles were made for volterra_equation_solvers-0.3.2-py3-none-macosx_11_0_arm64.whl:

Publisher: build-wheels.yml on trout314/volterra-equation-solvers

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page