Skip to content

LHI and heat, written with matrices

Goal: construct local Hermite interpolation using LocalApproximation, then assemble a stationary equation and a heat time step with ordinary SciPy algebra. LHI is a choice of local functionals and their mathematical interpretation; it does not require a separate numerical engine.

Read sampled functionals and operators first.

1. Choose solution, PDE, and boundary points

We use \(L=-\Delta\) on the unit square and prescribed boundary values. Interior solution unknowns are stored in \(U=u(X_u)\). PDE and boundary data are sampled at \(X_f\) and \(X_b\). For this example, \(X_f=X_u\) with the same global ordering.

import numpy as np
import scipy.sparse as sparse
from scipy.sparse.linalg import spsolve, splu
import rbflab as rbf
implementation = rbf.PythonBackend()
    cloud = rbf.geometry.unit_box_grid(6)
    Xu = cloud.points[cloud.interior_indices]
    Xb = cloud.points[cloud.boundary_indices]
    Xf = Xu.copy()  # identical global ordering; local PDE stencils exclude their target
    I = rbf.Identity(2)
    L = -rbf.Laplacian(2)

A regular point grid keeps the example short. The construction needs points and functionals, not grid connectivity; mesh generation supplies other domains and irregular clouds.

2. Fit functional data with a Hermite trial

The local data are values \(u(X_u)\), PDE samples \((Lu)(X_f)\), and boundary values \(u(X_b)\). Their kernel representers span the trial approximation.

    space = rbf.ScalarSpace(rbf.PHS(5), polynomial_degree=2)
    source = {
        "u": rbf.Samples(Xu, operator=I, size=15),
        "pde": rbf.Samples(Xf, operator=L, size=8, target="exclude"),
        "boundary": rbf.Samples(Xb, operator=I, size=6),
    }
    local = rbf.LocalApproximation(
        source=source,
        trial=space.representers(source),
        backend=implementation,
    )
    ops = local.operators(targets=Xu, operators={"L": L})
    Su = ops.L["u"].to_scipy()
    Sf = ops.L["pde"].to_scipy()
    Sb = ops.L["boundary"].to_scipy()

to_scipy() explicitly exports Float64 sparse blocks for the SciPy algorithm below. Native .matrix preserves backend storage: Torch returns a sparse tensor. This export ends a Torch differentiation graph and does not preserve extended precision; use native arithmetic if either property is needed.

There are independent neighborhood sizes for each group. The sizes here illustrate the API; they are not a universal recipe. Quadratic enrichment allows the local approximation to reproduce quadratics. The signed PHS5 kernel \(-r^5\) has the smoothness needed for the fourth-order kernel actions generated by two Laplacians.

target="exclude" removes a coincident PDE sample from each target's local PDE stencil. Otherwise reconstructing a PDE functional already supplied as data can just return that datum, leaving a tautological equation. Value samples can retain the target.

The assembled reconstruction is

\[Lu(X_u)\approx S_uU+S_f(Lu)(X_f)+S_bu(X_b).\]

The block shapes are \(N_u\times N_u\), \(N_u\times N_f\), and \(N_u\times N_b\). The approximation engine constructs these maps; it does not decide which data are prescribed or unknown.

3. Assemble a stationary equation

For \(Lu=f\) and boundary values \(u=g\),

\[S_uU=f(X_u)-S_ff(X_f)-S_bg(X_b).\]

Use \(u=1+x^2+y^2\), so \(f=-4\) and \(g\) is its boundary trace:

    def exact(X):
        return 1 + np.sum(X**2, axis=1)

    fu = np.full(len(Xu), -4.)
    ff = np.full(len(Xf), -4.)
    gb = exact(Xb)
    A = Su
    rhs = fu - Sf @ ff - Sb @ gb
    U = spsolve(A, rhs)

The polynomial solution is a short algebraic check. For a new problem, change fu, ff, and gb; for another differential operator, rebuild the maps.

4. Derive the evolution matrices

For heat,

\[u_t+Lu=f,\qquad u|_{\partial\Omega}=g,\]

PDE-center data are \((Lu)(X_f)=f(X_f)-u_t(X_f)\). Because this example uses identical solution and PDE point ordering, \(u_t(X_f)=\dot U\). Substitution gives

\[\boxed{(I-S_f)\dot U+S_uU=f(X_u)-S_ff(X_f)-S_bg(X_b).}\]

Thus \(M=I-S_f\) and \(A=S_u\). For zero forcing and homogeneous boundary data, backward Euler reads

\[\left(M/\Delta t+A\right)U^{n+1}=MU^n/\Delta t.\]
steps, dt = 5, .001
    # u_t - Laplacian(u) = 0, homogeneous Dirichlet data.
    M = sparse.eye(len(Xu), format="csc") - Sf
    solve_step = splu((M/dt + A).tocsc()).solve
    U = np.sin(np.pi*Xu[:, 0])*np.sin(np.pi*Xu[:, 1])
    for n in range(steps):
        U = solve_step(M @ U/dt)

The factorization is reused while \(M\), \(A\), and \(\Delta t\) remain unchanged. The initial field \(\sin(\pi x)\sin(\pi y)\) has exact evolution \(e^{-2\pi^2t}\sin(\pi x)\sin(\pi y)\). The example prints the sampled final error. A short run illustrates matrix assembly; it does not establish long-time stability of every stencil/kernel recipe.

5. Change the integrator or point layout

After one backward-Euler startup, BDF2 uses

\[\left(3M/(2\Delta t)+A\right)U^{n+1} =M(2U^n-U^{n-1}/2)/\Delta t+b(t_{n+1}).\]

You choose the time loop, solver, and storage. Do not form \(M^{-1}\): a constrained system can have a singular mass matrix even when an implicit step is solvable.

For independent PDE centers, an explicit map \(P\) is needed if you choose \(u_t(X_f)=P\dot U\). Then \(M=I-S_fP\), not \(I-S_f\). If the chosen reconstruction also contains time-dependent prescribed data, their derivatives contribute to the forcing. Alternatively, enlarge the unknown layout. The library must not silently choose either formulation for you.

Retain extended precision

For arbitrary-precision maps, set precision=rbf.Precision(local_digits=50, global_dtype="mpmath") with Python or C++ and use each block's .matrix directly. Do not call .to_scipy(): that is an explicit Float64 export. The returned MPSparseMatrix supports addition, subtraction, scalar scaling, transpose, and sparse matrix products. Its MPSparseMatrix.eye(ctx, n) constructor and the MPSparseLU solver are available in rbflab.sparse_precision for writing the same time loop without rounding. Evaluate forcing and time-step constants in the same arithmetic context. Geometry remains Float64; the global extended solver uses mpmath even when C++ MPFR constructs the local weights.

Run and adapt

python -m examples.tutorials.lhi_matrices
python -m examples.tutorials.lhi_matrices --backend cpp
python -m examples.tutorials.lhi_matrices --backend torch

See backend installation. The LHI construction and independent center groups use this same explicit functional interface. For an equation-driven scalar solve, LHI assembles local weights through a compatibility adapter while retaining its data assembly and reconstruction behavior. Specialized Stokes and coupled paths use additional implementations.