rgpycrumbs.surfaces._kernels

Kernel functions for Gaussian process surface fitting.

Provides element-wise and matrix kernel functions for Matern 5/2, inverse multiquadric (IMQ), squared exponential (SE), rational quadratic (RQ), and thin-plate spline (TPS) kernels. Each kernel family includes a full_covariance_* function that builds the gradient-enhanced covariance block [[k, dk/dx2], [dk/dx1, d2k/dx1dx2]]. Matern 5/2 uses a cancellation-free closed form. The other families use JAX automatic differentiation.

Added in version 1.0.0.

Attributes

Functions

_tps_kernel_matrix(x)

Compute the thin-plate spline kernel matrix.

_matern_kernel_matrix(x, length_scale)

Compute the Matern 5/2 kernel matrix.

matern_kernel_elem(x1, x2[, length_scale])

Evaluate the Matern 5/2 kernel for a single pair of points.

_matern_radial(x1, x2, length_scale)

Cancellation-free Matern 5/2 value and dk/dx2.

_matern_query_row(x1, x2, length_scale)

Value and dk/dx2 for a query, finite under jax.grad at r = 0.

full_covariance_matern(x1, x2, length_scale)

Build the gradient-enhanced covariance block for the Matern 5/2 kernel.

_imq_kernel_matrix(x, epsilon)

Compute the inverse multiquadric (IMQ) kernel matrix.

imq_kernel_elem(x1, x2[, epsilon])

Evaluate the IMQ kernel for a single pair of points.

full_covariance_imq(x1, x2, epsilon)

Build the gradient-enhanced covariance block for the IMQ kernel.

se_kernel_elem(x1, x2[, length_scale])

Evaluate the squared exponential (SE) kernel for a single pair of points.

full_covariance_se(x1, x2, length_scale)

Build the gradient-enhanced covariance block for the SE kernel.

rq_kernel_base(x1, x2, length_scale, alpha)

Evaluate the rational quadratic (RQ) base kernel.

rq_kernel_elem(x1, x2, params)

Evaluate the RQ kernel with mirror symmetry for a single pair.

full_covariance_rq(x1, x2, params)

Build the gradient-enhanced covariance block for the RQ kernel.

gradient_covariance(K_blocks)

Arrange (N, M, D+1, D+1) pair blocks as one (N(D+1), M(D+1)) matrix.

gradient_kernel_matrix(k_matrix_grad_map, x, params)

Return the gradient-enhanced covariance of x with itself.

Module Contents

rgpycrumbs.surfaces._kernels._tps_kernel_matrix(x)[source]

Compute the thin-plate spline kernel matrix.

Evaluates K_{ij} = r_{ij}^2 * log(r_{ij}) where r_{ij} is the Euclidean distance between points x_i and x_j.

Parameters:

x – Input points, shape (N, D).

Returns:

Kernel matrix, shape (N, N).

Added in version 1.0.0.

rgpycrumbs.surfaces._kernels._matern_kernel_matrix(x, length_scale)[source]

Compute the Matern 5/2 kernel matrix.

Evaluates k(r) = (1 + sqrt(5)*r/l + 5*r^2/(3*l^2)) * exp(-sqrt(5)*r/l).

Parameters:
  • x – Input points, shape (N, D).

  • length_scale – Kernel length scale parameter.

Returns:

Kernel matrix, shape (N, N).

Added in version 1.0.0.

rgpycrumbs.surfaces._kernels.matern_kernel_elem(x1, x2, length_scale=1.0)[source]

Evaluate the Matern 5/2 kernel for a single pair of points.

Parameters:
  • x1 – First point, shape (D,).

  • x2 – Second point, shape (D,).

  • length_scale – Kernel length scale parameter.

Returns:

Scalar kernel value.

Added in version 1.0.0.

rgpycrumbs.surfaces._kernels._matern_radial(x1, x2, length_scale)[source]

Cancellation-free Matern 5/2 value and dk/dx2.

With diff = x1 - x2 and u = sqrt(5) ||diff|| / l,

k = (1 + u + u**2 / 3) exp(-u) and dk/dx2 = (5 / (3 l**2)) exp(-u) (1 + u) diff,

which is the identity -dk/dr / r = (5 / (3 l**2)) (1 + u) exp(-u). At a repeated point this is (1, 0) with no 0/0 quotient. Automatic differentiation of matern_kernel_elem() subtracts two O(1/l) terms there and is wrong by several percent in float32.

rgpycrumbs.surfaces._kernels._matern_query_row(x1, x2, length_scale)[source]

Value and dk/dx2 for a query, finite under jax.grad at r = 0.

The train-train block keeps _matern_radial(). A bare sqrt of the squared distance has an infinite derivative at a repeated point, so differentiating a prediction or a variance with respect to the query is NaN exactly at a training point. The double where is the identity for r > 0 and returns 0 at r = 0, so the forward values match the closed form.

rgpycrumbs.surfaces._kernels.full_covariance_matern(x1, x2, length_scale)[source]

Build the gradient-enhanced covariance block for the Matern 5/2 kernel.

Constructs a (D+1, D+1) matrix containing the energy-energy, energy-gradient, gradient-energy, and gradient-gradient covariance entries from the closed form in _matern_radial(). The Hessian block is

d**2 k / (dx1 dx2) = c ((1 + u) I - (5 / l**2) outer(diff, diff))

with c = (5 / (3 l**2)) exp(-u), so a repeated point has diagonal 5 / (3 l**2) and the matrix stays positive definite in float32.

Parameters:
  • x1 – First point, shape (D,).

  • x2 – Second point, shape (D,).

  • length_scale – Kernel length scale parameter.

Returns:

Covariance block, shape (D+1, D+1).

Added in version 1.1.0.

rgpycrumbs.surfaces._kernels.k_matrix_matern_grad_map[source]
rgpycrumbs.surfaces._kernels._imq_kernel_matrix(x, epsilon)[source]

Compute the inverse multiquadric (IMQ) kernel matrix.

Evaluates k(r) = 1 / sqrt(r^2 + epsilon^2).

Parameters:
  • x – Input points, shape (N, D).

  • epsilon – Shape parameter controlling kernel width.

Returns:

Kernel matrix, shape (N, N).

Added in version 1.0.0.

rgpycrumbs.surfaces._kernels.imq_kernel_elem(x1, x2, epsilon=1.0)[source]

Evaluate the IMQ kernel for a single pair of points.

Parameters:
  • x1 – First point, shape (D,).

  • x2 – Second point, shape (D,).

  • epsilon – Shape parameter controlling kernel width.

Returns:

Scalar kernel value.

Added in version 1.0.0.

rgpycrumbs.surfaces._kernels.full_covariance_imq(x1, x2, epsilon)[source]

Build the gradient-enhanced covariance block for the IMQ kernel.

Constructs a (D+1, D+1) matrix containing energy-energy, energy-gradient, gradient-energy, and gradient-gradient covariance entries.

Parameters:
  • x1 – First point, shape (D,).

  • x2 – Second point, shape (D,).

  • epsilon – Shape parameter controlling kernel width.

Returns:

Covariance block, shape (D+1, D+1).

Added in version 1.1.0.

rgpycrumbs.surfaces._kernels.k_matrix_imq_grad_map[source]
rgpycrumbs.surfaces._kernels.se_kernel_elem(x1, x2, length_scale=1.0)[source]

Evaluate the squared exponential (SE) kernel for a single pair of points.

Computes k(r) = exp(-r^2 / (2 * l^2)).

Parameters:
  • x1 – First point, shape (D,).

  • x2 – Second point, shape (D,).

  • length_scale – Kernel length scale parameter.

Returns:

Scalar kernel value.

Added in version 1.0.0.

rgpycrumbs.surfaces._kernels.full_covariance_se(x1, x2, length_scale)[source]

Build the gradient-enhanced covariance block for the SE kernel.

Constructs a (D+1, D+1) matrix containing energy-energy, energy-gradient, gradient-energy, and gradient-gradient covariance entries.

Parameters:
  • x1 – First point, shape (D,).

  • x2 – Second point, shape (D,).

  • length_scale – Kernel length scale parameter.

Returns:

Covariance block, shape (D+1, D+1).

Added in version 1.1.0.

rgpycrumbs.surfaces._kernels.k_matrix_se_grad_map[source]
rgpycrumbs.surfaces._kernels.rq_kernel_base(x1, x2, length_scale, alpha)[source]

Evaluate the rational quadratic (RQ) base kernel.

Computes k(r) = (1 + r^2 / (2 * alpha * l^2))^(-alpha).

Parameters:
  • x1 – First point, shape (D,).

  • x2 – Second point, shape (D,).

  • length_scale – Kernel length scale parameter.

  • alpha – Shape parameter controlling the mixture of length scales.

Returns:

Scalar kernel value.

Added in version 1.1.0.

rgpycrumbs.surfaces._kernels.rq_kernel_elem(x1, x2, params)[source]

Evaluate the RQ kernel with mirror symmetry for a single pair.

Computes k(x1, x2) + k(flip(x1), x2) to enforce symmetry under coordinate reversal.

Parameters:
  • x1 – First point, shape (D,).

  • x2 – Second point, shape (D,).

  • params – Array of [length_scale, alpha].

Returns:

Scalar kernel value (sum of direct and mirrored terms).

Added in version 1.1.0.

rgpycrumbs.surfaces._kernels.full_covariance_rq(x1, x2, params)[source]

Build the gradient-enhanced covariance block for the RQ kernel.

Constructs a (D+1, D+1) matrix containing energy-energy, energy-gradient, gradient-energy, and gradient-gradient covariance entries. Row 1 + i and column 1 + j hold the derivative in x1[i] and x2[j].

Parameters:
  • x1 – First point, shape (D,).

  • x2 – Second point, shape (D,).

  • params – Array of [length_scale, alpha].

Returns:

Covariance block, shape (D+1, D+1).

Added in version 1.1.0.

Changed in version 1.14.0: The gradient-gradient block is no longer transposed.

rgpycrumbs.surfaces._kernels.k_matrix_rq_grad_map[source]
rgpycrumbs.surfaces._kernels.gradient_covariance(K_blocks)[source]

Arrange (N, M, D+1, D+1) pair blocks as one (N(D+1), M(D+1)) matrix.

Parameters:

K_blocks – Pair blocks from a k_matrix_*_grad_map function.

Returns:

Covariance matrix ordered by point, then by value and gradient component.

Added in version 1.14.0.

rgpycrumbs.surfaces._kernels.gradient_kernel_matrix(k_matrix_grad_map, x, params)[source]

Return the gradient-enhanced covariance of x with itself.

Compiles once per kernel map and input shape, as the model solves do.

Parameters:
  • k_matrix_grad_map – A k_matrix_*_grad_map function.

  • x – Inputs (N, D).

  • params – Kernel parameters of that map.

Returns:

The (N(D+1), N(D+1)) matrix from gradient_covariance.

Added in version 1.14.0.