Skip to content

function_solve_VIE_1

Solve the Volterra integral equation of the first kind

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

with callable kernel and right-hand side on an arbitrary mesh. Zero is not a permitted collocation node (both sides of the equation vanish at t=0).

Parameters:

Name Type Description Default
kernel callable

kernel(u) returns \(K(u)\): a scalar, or a \((d, d)\) matrix for vector and matrix-valued equations.

required
g callable

g(t) returns the right-hand side: scalar, \((d,)\), or \((d, m)\) for the matrix-valued case (\(m\) right-hand sides). Defaults to zero (trivial y=0). A 2-D return signals the matrix case.

None
soln_init_value float or array_like

\(y(0)\). Required only when force_continuous=True; ignored otherwise. A warning is emitted if a value is passed when it has no effect. For a matrix-valued problem with force_continuous=True it must have shape \((d, m)\).

None
mesh_breakpoints array_like

Strictly-increasing 1-D array starting at 0.

required
coll_divs int, list of int

Collocation nodes lie at coll_choices[k] / coll_divs in (0, 1]. Zero is excluded from coll_choices. Defaults to coll_divs=3, coll_choices=[1, 2, 3]. Mutually exclusive with coll_nodes.

None
coll_choices int, list of int

Collocation nodes lie at coll_choices[k] / coll_divs in (0, 1]. Zero is excluded from coll_choices. Defaults to coll_divs=3, coll_choices=[1, 2, 3]. Mutually exclusive with coll_nodes.

None
coll_nodes array_like

Collocation node positions given directly as floats in \((0, 1]\) (zero excluded); see function_solve_VIE_2. Mutually exclusive with coll_divs/coll_choices. The convergence check (see Notes) is applied to both the integer and the coll_nodes paths.

None
force_continuous bool

If True, use the continuous collocation method (Brunner's \(S_m^{(0)}\)): the solution is a globally \(C^0\) piecewise polynomial of degree $m = $ len(coll_choices), anchored at $y(0) = $ soln_init_value. This requires the last node to be the right endpoint, \(c_m = 1\) (see Notes for the convergence condition). The default discontinuous method (\(S_{m-1}^{(-1)}\)) imposes no such restriction and is generally the better default.

False
kernel_singularity

See function_solve_VIE_2.

None
return_function

See function_solve_VIE_2.

None
show_warnings

See function_solve_VIE_2.

None
Notes

Whether a given node set yields a convergent method depends on the nodes (Brunner 2004, smooth-kernel chapter). For a smooth kernel with \(|K(t, t)| \ge k_0 > 0\), writing \(c_1 < \dots < c_m\) for the nodes:

  • Discontinuous (default): converges iff \(|\rho_m| := \prod_{i=1}^{m} (1 - c_i)/c_i \le 1\) (Thm 2.4.2), with global order \(m\) (reduced to \(m - 1\) at \(\rho_m = 1\)).
  • Continuous (force_continuous): requires \(c_m = 1\) and converges iff \(|\rho_{m-1}| := \prod_{i=1}^{m-1} (1 - c_i)/c_i \le 1\) (Thm 2.4.5), with global order \(m + 1\) (reduced to \(m\) at \(\rho_{m-1} = 1\)).

Node sets violating these are rejected with a ValueError. The criteria do not apply to weakly-singular kernels (where \(|K(t, t)|\) is unbounded), so the check is skipped when kernel_singularity is given and convergence is then the caller's responsibility.

These theorems are proved for a uniform mesh. The amplification factors \(\rho\) depend only on the nodes, so the conditions carry over to quasi-uniform meshes (all interval widths within fixed constant multiples of each other). They are not guaranteed on strongly graded meshes -- e.g. the geometric refinement of optimal_graded_mesh, where the smallest interval shrinks far faster than the largest -- but such meshes normally accompany a declared kernel_singularity, for which the check is already skipped.

Source code in src/voles/_callable_solvers.py
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
@_escalate_complex_warning
def function_solve_VIE_1(*, kernel, g=None, soln_init_value=None,
                          mesh_breakpoints,
                          coll_divs: int = None, coll_choices: list[int] = None,
                          coll_nodes=None,
                          kernel_singularity=None,
                          return_function: bool = False,
                          force_continuous: bool = False,
                          show_warnings: bool = True,
                          _smooth_gl_order: int = 6):
    r"""Solve the Volterra integral equation of the first kind

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

    with callable kernel and right-hand side on an arbitrary mesh. Zero is not
    a permitted collocation node (both sides of the equation vanish at t=0).

    Parameters
    ----------
    kernel : callable
        ``kernel(u)`` returns $K(u)$: a scalar, or a $(d, d)$ matrix for vector
        and matrix-valued equations.
    g : callable, optional
        ``g(t)`` returns the right-hand side: scalar, $(d,)$, or $(d, m)$ for
        the matrix-valued case ($m$ right-hand sides). Defaults to zero
        (trivial y=0). A 2-D return signals the matrix case.
    soln_init_value : float or array_like, optional
        $y(0)$. Required only when ``force_continuous=True``; ignored
        otherwise. A warning is emitted if a value is passed when it has no
        effect. For a matrix-valued problem with ``force_continuous=True`` it
        must have shape $(d, m)$.
    mesh_breakpoints : array_like
        Strictly-increasing 1-D array starting at 0.
    coll_divs, coll_choices : int, list of int, optional
        Collocation nodes lie at ``coll_choices[k] / coll_divs`` in (0, 1].
        Zero is excluded from ``coll_choices``. Defaults to
        ``coll_divs=3, coll_choices=[1, 2, 3]``. Mutually exclusive with
        ``coll_nodes``.
    coll_nodes : array_like, optional
        Collocation node positions given directly as floats in $(0, 1]$ (zero
        excluded); see ``function_solve_VIE_2``. Mutually exclusive with
        ``coll_divs``/``coll_choices``. The convergence check (see Notes) is
        applied to both the integer and the ``coll_nodes`` paths.
    force_continuous : bool, optional
        If ``True``, use the continuous collocation method (Brunner's
        $S_m^{(0)}$): the solution is a globally $C^0$ piecewise polynomial of
        degree $m = $ ``len(coll_choices)``, anchored at $y(0) = $
        ``soln_init_value``. This requires the last node to be the right
        endpoint, $c_m = 1$ (see Notes for the convergence condition). The
        default discontinuous method ($S_{m-1}^{(-1)}$) imposes no such
        restriction and is generally the better default.
    kernel_singularity, return_function, show_warnings :
        See ``function_solve_VIE_2``.

    Notes
    -----
    Whether a given node set yields a convergent method depends on the nodes
    (Brunner 2004, smooth-kernel chapter). For a smooth kernel with
    $|K(t, t)| \ge k_0 > 0$, writing $c_1 < \dots < c_m$ for the nodes:

    - Discontinuous (default): converges iff
      $|\rho_m| := \prod_{i=1}^{m} (1 - c_i)/c_i \le 1$ (Thm 2.4.2), with global
      order $m$ (reduced to $m - 1$ at $\rho_m = 1$).
    - Continuous (``force_continuous``): requires $c_m = 1$ and converges iff
      $|\rho_{m-1}| := \prod_{i=1}^{m-1} (1 - c_i)/c_i \le 1$ (Thm 2.4.5), with
      global order $m + 1$ (reduced to $m$ at $\rho_{m-1} = 1$).

    Node sets violating these are rejected with a ``ValueError``. The criteria
    do not apply to weakly-singular kernels (where $|K(t, t)|$ is unbounded), so
    the check is skipped when ``kernel_singularity`` is given and convergence is
    then the caller's responsibility.

    These theorems are proved for a *uniform* mesh. The amplification factors
    $\rho$ depend only on the nodes, so the conditions carry over to
    *quasi-uniform* meshes (all interval widths within fixed constant multiples
    of each other). They are not guaranteed on *strongly graded* meshes -- e.g.
    the geometric refinement of ``optimal_graded_mesh``, where the smallest
    interval shrinks far faster than the largest -- but such meshes normally
    accompany a declared ``kernel_singularity``, for which the check is already
    skipped.
    """
    mesh_breakpoints = np.asarray(mesh_breakpoints, dtype=float)
    if mesh_breakpoints.ndim != 1 or len(mesh_breakpoints) < 2:
        raise ValueError("mesh_breakpoints must be 1-D with at least two entries")
    if not np.all(np.diff(mesh_breakpoints) > 0):
        raise ValueError("mesh_breakpoints must be strictly increasing")
    if mesh_breakpoints[0] != 0.0:
        raise ValueError("mesh_breakpoints[0] must be 0")

    node_pos, _divs, _choices = _resolve_node_pos(
        coll_nodes, coll_divs, coll_choices,
        default_divs=3, default_choices=[1, 2, 3],
        exclude_zero=True, fname="function_solve_VIE_1")

    # Reject node sets for which the chosen VIE-1 method does not converge.
    # The criteria assume a smooth kernel (Brunner Thm 2.4.2(b): |K(t,t)|>=k0>0),
    # so they are skipped when a weakly-singular kernel is declared.
    if kernel_singularity is None:
        if force_continuous:
            # Continuous S_m^(0): converges iff c_m = 1 and |rho_{m-1}| <= 1.
            if abs(node_pos[-1] - 1.0) > 1e-12:
                raise ValueError(
                    "force_continuous (continuous S_m^(0) collocation) requires the "
                    "last collocation node to be the right endpoint c_m = 1; got "
                    f"c_m = {node_pos[-1]:.6g}. Append 1.0 to your nodes.")
            rho = _vie1_cont_amplification(node_pos)
            if rho > 1.0 + 1e-9:
                raise ValueError(
                    "Collocation nodes do not yield a convergent continuous (S_m^(0)) "
                    f"VIE-1 solver: the leading-node amplification |rho_{{m-1}}| = "
                    f"prod_(i<m) (1 - c_i)/c_i = {rho:.4g} exceeds 1 (Brunner Thm 2.4.5).")
        elif not _vie1_convergent(node_pos):
            # Discontinuous S_{m-1}^{(-1)}: converges iff |rho_m| <= 1.
            rho = _vie1_amplification(node_pos)
            if _choices is not None:
                raise ValueError(
                    f"Collocation setting (coll_divs={_divs}, coll_choices={_choices}) "
                    f"does not produce a convergent VIE-1 solver and is not supported "
                    f"(amplification |rho_m| = {rho:.4g} > 1).")
            raise ValueError(
                f"Collocation nodes {np.array2string(node_pos, precision=6)} do not "
                f"produce a convergent VIE-1 solver: the amplification factor "
                f"|rho_m| = prod (1 - c_i)/c_i = {rho:.4g} exceeds 1. Choose nodes "
                f"with a smaller amplification (e.g. include c = 1, as Radau IIA does).")

    if not callable(kernel):
        raise TypeError("kernel must be callable")
    if g is not None and not callable(g):
        raise TypeError("g must be callable or None")

    if force_continuous and soln_init_value is None:
        raise ValueError("must specify soln_init_value when force_continuous=True")
    if not force_continuous and soln_init_value is not None and show_warnings:
        print("warning: setting soln_init_value has no effect when force_continuous=False.")

    # Complex dispatch. soln_init_value only contributes to the complex check
    # when force_continuous=True (otherwise it has no effect on the result).
    init_for_complex = soln_init_value if force_continuous else None
    if _samples_indicate_complex([kernel, g], mesh_breakpoints, init_for_complex):
        sample_u = float(np.diff(mesh_breakpoints)[0]) * 0.5
        d_orig = _detect_complex_d_orig(kernel, sample_u)
        init_wrapped = (_block_expand_init(soln_init_value, d_orig)
                        if force_continuous else None)
        result = function_solve_VIE_1(
            kernel=_block_wrap_kernel(kernel, d_orig),
            g=_block_wrap_g(g, d_orig),
            soln_init_value=init_wrapped,
            mesh_breakpoints=mesh_breakpoints,
            coll_nodes=node_pos,
            kernel_singularity=kernel_singularity,
            return_function=return_function,
            force_continuous=force_continuous,
            show_warnings=show_warnings,
            _smooth_gl_order=_smooth_gl_order)
        if return_function:
            y_real, y_func_real = result
            return (_recombine_complex_y(y_real, d_orig),
                    _ComplexSolutionFunction(y_func_real, d_orig))
        return _recombine_complex_y(result, d_orig)

    M = len(mesh_breakpoints) - 1
    p = len(node_pos)
    widths = np.diff(mesh_breakpoints)

    from . import _dlang as _dlang_module
    max_p = _dlang_module.function_solve_max_p_d()
    if p > max_p:
        raise ValueError(
            f"number of collocation nodes p = {p} exceeds the maximum compiled "
            f"into the D extension ({max_p}).")

    sample_u = float(widths[0]) * 0.5
    is_vector, d = _detect_kernel_shape(kernel, sample_u)

    _maybe_warn_mesh_uniform_with_singularity(
        mesh_breakpoints, kernel_singularity, show_warnings)

    if not is_vector:
        g_arr = np.zeros((M, p), dtype=np.float64)
        if g is not None:
            for n in range(M):
                t_n = mesh_breakpoints[n]
                h_n = widths[n]
                for i in range(p):
                    g_arr[n, i] = float(g(t_n + node_pos[i] * h_n))
        if force_continuous:
            # Brunner S_m^(0): degree-m polynomial on {0} ∪ node_pos, with the
            # boundary value carried forward for continuity. Extended weight
            # tensor against the augmented basis (value cols + boundary col).
            cont_basis = _vie1_cont_basis_coefs(node_pos)
            W = _build_W_with_basis_scalar(
                kernel, mesh_breakpoints, node_pos,
                kernel_singularity, _smooth_gl_order, cont_basis)
            adv_U, adv_0 = _vie1_cont_advance(node_pos)
            y, boundary = _dlang_module.function_solve_vie1_cont_d(
                W, g_arr, adv_U, adv_0, float(soln_init_value))
            if return_function:
                polys = _build_vie1_cont_polynomials_scalar(
                    y, boundary, mesh_breakpoints, node_pos)
                return y, _SolutionFunction(polys, mesh_breakpoints, d=0)
            return y

        W = _build_W_scalar(kernel, mesh_breakpoints, node_pos,
                            kernel_singularity, _smooth_gl_order)
        y = _dlang_module.function_solve_vie1_d(W, g_arr)
        if return_function:
            polys = _build_polynomials(y, mesh_breakpoints, node_pos)
            y_func = _SolutionFunction(polys, mesh_breakpoints, d=0)
            return y, y_func
        return y

    # ----- Vector / matrix path -----
    max_d = _dlang_module.function_solve_max_d_d()
    if d > max_d:
        raise ValueError(
            f"kernel dimension d = {d} exceeds the maximum compiled into the "
            f"D extension ({max_d}).")

    # W depends only on the kernel; built once and shared across columns. The
    # continuous (S_m^(0)) method uses the augmented value+boundary basis.
    if force_continuous:
        cont_basis = _vie1_cont_basis_coefs(node_pos)
        W = _build_W_with_basis_vector(kernel, mesh_breakpoints, node_pos,
                                       kernel_singularity, _smooth_gl_order, d,
                                       cont_basis)
        adv_U, adv_0 = _vie1_cont_advance(node_pos)
    else:
        W = _build_W_vector(kernel, mesh_breakpoints, node_pos,
                            kernel_singularity, _smooth_gl_order, d)

    sample_t = float(mesh_breakpoints[0] + node_pos[0] * widths[0])
    m = _detect_g_matrix_cols(g, d, sample_t)

    if m is not None:
        # Matrix-valued problem: m simultaneous right-hand sides sharing W.
        if force_continuous:
            init_mat = np.asarray(soln_init_value, dtype=np.float64)
            if init_mat.shape != (d, m):
                raise ValueError(
                    f"soln_init_value must have shape ({d}, {m}) for a "
                    f"matrix-valued problem with force_continuous=True; "
                    f"got shape {tuple(init_mat.shape)}")

        G = _sample_g_at_coll_matrix(g, mesh_breakpoints, node_pos, widths,
                                     M, p, d, m)

        if force_continuous:
            def _col_solve(j):
                g_arr_j = np.ascontiguousarray(G[:, :, :, j])
                return _dlang_module.function_solve_vie1_cont_vec_d(
                    W, g_arr_j, adv_U, adv_0,
                    np.ascontiguousarray(init_mat[:, j]))
            with ThreadPoolExecutor(max_workers=m) as ex:
                results = list(ex.map(_col_solve, range(m)))
            y = np.stack([r[0] for r in results], axis=3)        # (M, p, d, m)
            if return_function:
                boundary = np.stack([r[1] for r in results], axis=2)  # (M+1, d, m)
                polys = _build_vie1_cont_polynomials_matrix(
                    y, boundary, mesh_breakpoints, node_pos, d, m)
                return y, _SolutionFunction(polys, mesh_breakpoints, d=d, m=m)
            return y

        def _col_solve(j):
            g_arr_j = np.ascontiguousarray(G[:, :, :, j])
            return _dlang_module.function_solve_vie1_vec_d(W, g_arr_j)

        with ThreadPoolExecutor(max_workers=m) as ex:
            cols = list(ex.map(_col_solve, range(m)))
        y = np.stack(cols, axis=3)  # (M, p, d, m)

        if return_function:
            polys = _build_polynomials_matrix(y, mesh_breakpoints, node_pos,
                                              d, m)
            return y, _SolutionFunction(polys, mesh_breakpoints, d=d, m=m)
        return y

    g_arr = _sample_g_at_coll_vec(g, mesh_breakpoints, node_pos, widths, M, p, d)

    if force_continuous:
        init_vec = np.asarray(soln_init_value, dtype=np.float64)
        if init_vec.shape != (d,):
            raise ValueError(
                f"soln_init_value must have shape ({d},) for vector kernel; "
                f"got shape {tuple(init_vec.shape)}")
        y, boundary = _dlang_module.function_solve_vie1_cont_vec_d(
            W, g_arr, adv_U, adv_0, init_vec)
        if return_function:
            polys = _build_vie1_cont_polynomials_vector(
                y, boundary, mesh_breakpoints, node_pos, d)
            return y, _SolutionFunction(polys, mesh_breakpoints, d=d)
        return y

    y = _dlang_module.function_solve_vie1_vec_d(W, g_arr)

    if return_function:
        polys = _build_polynomials_vector(y, mesh_breakpoints, node_pos, d)
        y_func = _SolutionFunction(polys, mesh_breakpoints, d=d)
        return y, y_func
    return y