Skip to content

solve_vie2

solve_vie2

solve_vie2(*, kernel, g=None, dt=1.0, coll_setting=None, quadrature='product', samples_per_interval=None, kernel_interp_degree=None, mesh=None, coll_nodes=None, kernel_singularity=None, reuse_adaptive_blocks=True)

Solve a Volterra integral equation of the second kind.

\[y(t) = g(t) + \int_0^t K(t-s)\, y(s)\, ds, \qquad 0 \le t \le T\]

by collocation: on each mesh interval \(y\) is a polynomial that satisfies the equation at the collocation nodes.

The inputs are either arrays of samples at \(t = 0, dt, 2\,dt, \ldots\) (the sampled regime) or callables (the functional regime, solved on the breakpoints mesh); see Notes.

Parameters:

Name Type Description Default
kernel array_like or callable

\(K\). Sampled: shape (N,), or (N, d, d) for a system of \(d\) equations, at \(u = 0, dt, \ldots, (N-1)\,dt\). Callable: kernel(u) returning a scalar or a (d, d) matrix; it may be vectorised over a 1-D u, which is faster.

required
g array_like or callable

\(g\). Sampled: (N,), (N, d) or (N, d, m) for \(m\) right-hand sides. Callable: g(t) returning a scalar, (d,) or (d, m). Default zero.

None
dt float

Sampled inputs: the sample spacing. Default 1.0.

1.0
coll_setting int or (int, sequence of int)

Sampled inputs: the collocation nodes choices / divs as (divs, choices), with choices in 0..divs, or a bare divs meaning every point 0..divs. divs also sets the mesh width (see samples_per_interval). Default (2, [0, 1, 2]).

None
quadrature ('product', 'collocation')

Sampled inputs: how the integrals are evaluated from the samples. "product" (default) integrates a piecewise polynomial interpolant of the kernel exactly and accepts any mesh width and node set. "collocation" applies the interpolatory rule on the nodes; it is faster but fixes the mesh at divs**2 samples and runs only compiled settings (or the numba fallback).

"product"
samples_per_interval int

Sampled inputs: samples per mesh interval. A multiple of divs with product quadrature (default divs, the finest mesh); must be divs**2 with collocation quadrature.

None
kernel_interp_degree int

Sampled inputs, product quadrature: the degree of the kernel interpolant. Default: the number of nodes.

None
mesh array_like

Callable inputs: the mesh breakpoints, strictly increasing from 0 to \(T\). Required for callable inputs.

None
coll_nodes array_like

Callable inputs: node positions in \([0, 1]\), e.g. voles.gauss_legendre_nodes(3). Default [0, 1/2, 1].

None
kernel_singularity (float, list, dict or callable)

Callable inputs: where the kernel is weakly singular, in \(u = t - s\): a location, a list of locations, {location: alpha} for \(K \sim |u - u_0|^{-\alpha}\), or a callable returning the singular \(s\)-locations for a time \(t\). Declare the exponent when you know it: the blocks the singularity touches then use Gauss–Jacobi rules instead of adaptive quadrature, typically 3–10× faster for the whole solve.

None
reuse_adaptive_blocks bool

Callable inputs: on a uniform mesh, reuse adaptive-quadrature blocks across mesh intervals. Default True.

True

Returns:

Type Description
VolterraSolution

The solution: sol.t and sol.y on the input grid (the samples, or the mesh breakpoints), sol(t) anywhere in \([0, T]\), plus the collocation data and coefficients.

Raises:

Type Description
TypeError

An argument of the wrong type (a float where an integer is needed, a non-bool flag, a non-numeric array), or a required one missing.

ValueError

A wrong value: shapes that don't fit, an option from the other regime, an invalid node set, NaN or inf in the data, too few samples for one mesh interval.

NotImplementedError

Mixed callable and sampled inputs, or an uncompiled setting under quadrature="collocation" with no fallback.

LinAlgError

A collocation system is singular.

Warns:

Type Description
AccuracyWarning

A singularity declared on a uniform mesh.

PerformanceWarning

The numba fallback is used; or a declared exponent in kernel_singularity failed its accuracy check, so the singular blocks used adaptive quadrature.

VolesWarning

A kernel_singularity location outside \([0, T]\).

See Also
Notes

The regime is chosen by the input types: arrays give the sampled regime (dt, coll_setting, quadrature, samples_per_interval, kernel_interp_degree apply), callables the functional regime (mesh, coll_nodes, kernel_singularity, reuse_adaptive_blocks apply). An option from the other regime is an error. Mixing callable and sampled inputs is not supported yet.

Examples:

Sampled data: \(y(t) = 1 + \int_0^t y(s)\,ds\) has the solution \(e^t\).

>>> import numpy as np
>>> import voles
>>> t = np.linspace(0, 1, 101)
>>> sol = voles.solve_vie2(kernel=np.ones_like(t), g=np.ones_like(t), dt=t[1])
>>> sol
<VolterraSolution vie2, sampled, 50 intervals on [0, 1], degree 2, ...>
>>> print(f"{sol(1.0):.8f}  {np.e:.8f}")
2.71828183  2.71828183

Callables, on a mesh of your choice: \(K(u) = e^{-u}\) and \(g = 1\) give \(y = 1 + t\), which the quadratic collocation polynomials represent exactly.

>>> sol = voles.solve_vie2(kernel=lambda u: np.exp(-u), g=lambda t: np.ones_like(t),
...                        mesh=np.linspace(0, 1, 11))
>>> sol.t.shape, sol.node_values.shape
((11,), (10, 3))
>>> print(f"{sol(0.55):.6f}")
1.550000
Source code in src/voles/_api.py
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
def solve_vie2(*, kernel: Data, g: Optional[Data] = None,
               dt: float = 1.0, coll_setting: CollSetting = None,
               quadrature: Quadrature = "product",
               samples_per_interval: Optional[int] = None,
               kernel_interp_degree: Optional[int] = None,
               mesh: Optional[ArrayLike] = None, coll_nodes: Optional[ArrayLike] = None,
               kernel_singularity: KernelSingularity = None,
               reuse_adaptive_blocks: bool = True) -> VolterraSolution:
    r"""Solve a Volterra integral equation of the second kind.

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

    by collocation: on each mesh interval $y$ is a polynomial that satisfies
    the equation at the collocation nodes.

    The inputs are either arrays of samples at $t = 0, dt, 2\,dt, \ldots$
    (the *sampled* regime) or callables (the *functional* regime, solved on
    the breakpoints ``mesh``); see Notes.

    Parameters
    ----------
    kernel : array_like or callable
        $K$. Sampled: shape ``(N,)``, or ``(N, d, d)`` for a system of $d$
        equations, at $u = 0, dt, \ldots, (N-1)\,dt$. Callable: ``kernel(u)``
        returning a scalar or a ``(d, d)`` matrix; it may be vectorised
        over a 1-D ``u``, which is faster.
    g : array_like or callable, optional
        $g$. Sampled: ``(N,)``, ``(N, d)`` or ``(N, d, m)`` for $m$
        right-hand sides. Callable: ``g(t)`` returning a scalar, ``(d,)`` or
        ``(d, m)``. Default zero.
    dt : float, optional
        Sampled inputs: the sample spacing. Default 1.0.
    coll_setting : int or (int, sequence of int), optional
        Sampled inputs: the collocation nodes ``choices / divs`` as
        ``(divs, choices)``, with choices in ``0..divs``, or a bare ``divs``
        meaning every point ``0..divs``. ``divs`` also sets the mesh
        width (see ``samples_per_interval``). Default ``(2, [0, 1, 2])``.
    quadrature : {"product", "collocation"}, optional
        Sampled inputs: how the integrals are evaluated from the samples.
        ``"product"`` (default) integrates a piecewise polynomial
        interpolant of the kernel exactly and accepts any mesh width and
        node set. ``"collocation"`` applies the interpolatory rule on the
        nodes; it is faster but fixes the mesh at ``divs**2`` samples and
        runs only compiled settings (or the numba fallback).
    samples_per_interval : int, optional
        Sampled inputs: samples per mesh interval. A multiple of ``divs``
        with product quadrature (default ``divs``, the finest mesh); must
        be ``divs**2`` with collocation quadrature.
    kernel_interp_degree : int, optional
        Sampled inputs, product quadrature: the degree of the kernel
        interpolant. Default: the number of nodes.
    mesh : array_like, optional
        Callable inputs: the mesh breakpoints, strictly increasing from 0 to
        $T$. Required for callable inputs.
    coll_nodes : array_like, optional
        Callable inputs: node positions in $[0, 1]$, e.g.
        ``voles.gauss_legendre_nodes(3)``. Default ``[0, 1/2, 1]``.
    kernel_singularity : float, list, dict or callable, optional
        Callable inputs: where the kernel is weakly singular, in
        $u = t - s$: a location, a list of locations, ``{location: alpha}``
        for $K \sim |u - u_0|^{-\alpha}$, or a callable returning the
        singular $s$-locations for a time $t$. Declare the exponent when you
        know it: the blocks the singularity touches then use Gauss–Jacobi
        rules instead of adaptive quadrature, typically 3–10× faster for the
        whole solve.
    reuse_adaptive_blocks : bool, optional
        Callable inputs: on a uniform mesh, reuse adaptive-quadrature blocks
        across mesh intervals. Default True.

    Returns
    -------
    VolterraSolution
        The solution: ``sol.t`` and ``sol.y`` on the input grid (the samples,
        or the mesh breakpoints), ``sol(t)`` anywhere in $[0, T]$, plus the
        collocation data and coefficients.

    Raises
    ------
    TypeError
        An argument of the wrong type (a float where an integer is needed, a
        non-bool flag, a non-numeric array), or a required one missing.
    ValueError
        A wrong value: shapes that don't fit, an option from the other
        regime, an invalid node set, NaN or inf in the data, too few
        samples for one mesh interval.
    NotImplementedError
        Mixed callable and sampled inputs, or an uncompiled setting under
        ``quadrature="collocation"`` with no fallback.
    numpy.linalg.LinAlgError
        A collocation system is singular.

    Warns
    -----
    AccuracyWarning
        A singularity declared on a uniform mesh.
    PerformanceWarning
        The numba fallback is used; or a declared exponent in
        ``kernel_singularity`` failed its accuracy check, so the singular
        blocks used adaptive quadrature.
    VolesWarning
        A ``kernel_singularity`` location outside $[0, T]$.

    See Also
    --------
    - [solve_vie1][voles.solve_vie1], [solve_vide][voles.solve_vide]: the other two
      equations.
    - [VolterraSolution][voles.VolterraSolution]: the returned object.
    - [optimal_graded_mesh][voles.optimal_graded_mesh]: a mesh for a weakly singular
      kernel.
    - [gauss_legendre_nodes][voles.gauss_legendre_nodes]: node sets for ``coll_nodes``.

    Notes
    -----
    The regime is chosen by the input types: arrays give the sampled regime
    (``dt``, ``coll_setting``, ``quadrature``, ``samples_per_interval``,
    ``kernel_interp_degree`` apply), callables the functional regime
    (``mesh``, ``coll_nodes``, ``kernel_singularity``,
    ``reuse_adaptive_blocks`` apply). An option from the other regime is
    an error. Mixing callable and sampled inputs is not supported yet.

    Examples
    --------
    Sampled data: $y(t) = 1 + \int_0^t y(s)\,ds$ has the solution $e^t$.

    >>> import numpy as np
    >>> import voles
    >>> t = np.linspace(0, 1, 101)
    >>> sol = voles.solve_vie2(kernel=np.ones_like(t), g=np.ones_like(t), dt=t[1])
    >>> sol
    <VolterraSolution vie2, sampled, 50 intervals on [0, 1], degree 2, ...>
    >>> print(f"{sol(1.0):.8f}  {np.e:.8f}")
    2.71828183  2.71828183

    Callables, on a mesh of your choice: $K(u) = e^{-u}$ and $g = 1$ give
    $y = 1 + t$, which the quadratic collocation polynomials represent
    exactly.

    >>> sol = voles.solve_vie2(kernel=lambda u: np.exp(-u), g=lambda t: np.ones_like(t),
    ...                        mesh=np.linspace(0, 1, 11))
    >>> sol.t.shape, sol.node_values.shape
    ((11,), (10, 3))
    >>> print(f"{sol(0.55):.6f}")
    1.550000
    """
    return _solve("vie2", kernel=kernel, g=g, dt=dt, coll_setting=coll_setting,
                  quadrature=quadrature, samples_per_interval=samples_per_interval,
                  kernel_interp_degree=kernel_interp_degree, mesh=mesh, coll_nodes=coll_nodes,
                  kernel_singularity=kernel_singularity,
                  reuse_adaptive_blocks=reuse_adaptive_blocks)