quadsv.kernels#
Kernel matrix construction and spectral properties.
- class quadsv.kernels.Kernel(n: int, method: str = 'gaussian', **kwargs)[source]#
Bases:
ABCAbstract base class for spatial kernels.
Handles dense, sparse, and implicit (operator-based) kernels. Implements a dual-mode system that switches between explicit (dense) and implicit (sparse) computation based on problem size.
- is_implicit#
If True, uses sparse solver for implicit kernels (N > 5000). If False, uses dense matrix operations.
- Type:
- realization() ndarray[source]#
Return the realized (N, N) kernel matrix.
- Returns:
Dense (N, N) kernel matrix.
- Return type:
np.ndarray
Warning
If is_implicit is True, this forces expensive dense inversion of the precision matrix. Use xtKx() and trace() for implicit kernels instead.
- eigenvalues(k: int | None = None) ndarray[source]#
Compute the k largest eigenvalues of the kernel matrix.
- Parameters:
k (int, optional) – Number of largest eigenvalues to return. If None, returns all.
- Returns:
Eigenvalues sorted in descending order, shape (k,) or (n,).
- Return type:
np.ndarray
- xtKx(x: ndarray | spmatrix) float | ndarray[source]#
Efficiently compute the quadratic form x^T K x.
Handles both single vectors and batches. Uses implicit solvers for large N (implicit_threshold) to avoid dense matrix operations. Supports sparse input matrices without densification.
- Parameters:
x (np.ndarray or scipy.sparse matrix) – Input vector of shape (N,) or batch of vectors (N, M). Can be dense numpy array or sparse matrix (CSC/CSR format).
- Returns:
Quadratic form value(s). Scalar if input was 1D, shape (M,) if input was 2D.
- Return type:
float or np.ndarray
Notes
For implicit kernels, uses sparse solve instead of matrix inversion. For sparse input, maintains sparsity throughout computation where possible.
- trace() float[source]#
Compute the trace of the kernel matrix Tr(K).
For implicit kernels, uses Hutchinson’s trick with random vectors for efficient O(N) estimation instead of O(N³) eigendecomposition.
- Returns:
Trace of the kernel matrix.
- Return type:
- class quadsv.kernels.SpatialKernel(data: ndarray, mode: str = 'coords', method: str = 'matern', **kwargs)[source]#
Bases:
KernelConcrete implementation of Spatial Kernel.
Can be built from raw coordinates OR from precomputed matrices.
- data#
Raw data (coordinates or matrix) used to construct the kernel.
- Type:
np.ndarray
- mode#
How the kernel was initialized.
- Type:
{‘coords’, ‘precomputed’, ‘precomputed_inverse’}
- __init__(data: ndarray, mode: str = 'coords', method: str = 'matern', **kwargs) None[source]#
Internal constructor. Use .from_coordinates() or .from_matrix() instead.
- classmethod from_coordinates(coords: ndarray, method: str = 'matern', **kwargs) SpatialKernel[source]#
Build kernel from spatial coordinates.
- Parameters:
- Returns:
Initialized kernel object.
- Return type:
Examples
>>> coords = np.random.randn(100, 2) >>> kernel = SpatialKernel.from_coordinates(coords, method='gaussian', bandwidth=1.0)
- classmethod from_matrix(matrix: ndarray | spmatrix, is_inverse: bool = False, method: str = 'precomputed', **kwargs) SpatialKernel[source]#
Build kernel from a precomputed kernel matrix or its inverse.
- Parameters:
matrix (np.ndarray or scipy.sparse matrix) – Kernel matrix (N, N) or its inverse (precision matrix).
is_inverse (bool, default False) – If True, matrix is treated as the inverse (precision) matrix K^-1.
method (str, default 'precomputed') – The logical kernel method (e.g., ‘car’ for precision matrices).
**kwargs (dict) – Additional parameters.
- Returns:
Initialized kernel object.
- Return type:
Examples
>>> K = np.array([[2, -1], [-1, 2]]) # kernel matrix >>> kernel = SpatialKernel.from_matrix(K, is_inverse=False)