Common helpers (spec models) #
This file centralizes small utilities used across multiple spec‑level models:
- simple matrix operations (minor, determinant, inverse),
- distance functions for KNN,
- normalization helpers,
- and other "model glue" functions.
Intent / tradeoffs #
These definitions prioritize:
- mathematical clarity, and
- shape safety (via
Spec.Tensor), over performance.
In particular, determinant_spec uses Laplace expansion, which is exponentially expensive and is
only meant for small matrices (e.g. 2×2, 3×3) and/or proof‑oriented reference code. If you need
large‑scale linear algebra, use the runtime layer with array‑backed kernels.
Helper lemma for minor/index computations.
When forming a matrix minor, we "skip" a row/column and map an index i : Fin (n-1) to the
corresponding original index in Fin n by either leaving it unchanged (if it is before the skipped
index) or shifting it by +1 (if it is at/after the skipped index). This lemma proves the resulting
index is still < n.
Matrix minor: delete row and col from an n × n matrix, producing an (n-1) × (n-1) matrix.
This is used by determinant_spec (Laplace expansion) and the adjugate-based inverse below.
Instances For
Determinant of an n × n matrix (spec-level reference implementation).
This uses Laplace expansion (cofactor expansion) along the first row, with special-cased base cases
for n = 0, 1, 2. It is mathematically clear but exponentially slow, so it is intended only for
very small n and/or proof-oriented reference code.
Instances For
Matrix inverse via the adjugate formula (spec-level reference implementation).
The result is none when the determinant is zero. Returning an unrelated matrix for a singular
input would make downstream statistical formulas appear defined when they are not.
PyTorch analogue: torch.linalg.inv, with failure represented explicitly by Option.
Instances For
Approximate the leading eigenpair by 20 steps of power iteration.
The scalar is the final Rayleigh quotient and the tensor is the corresponding normalized iterate.
This definition does not claim to compute a full eigendecomposition. Convergence to a dominant
eigenvector requires the usual spectral assumptions on matrix and a suitable initial vector.
Instances For
Instances For
Euclidean (L2) distance between two feature vectors.
PyTorch analogue: torch.linalg.vector_norm(x - y) or torch.cdist (batched).
Instances For
Squared Euclidean distance (avoids the final square root).
Instances For
Manhattan (L1) distance between two feature vectors.
Instances For
Cosine distance 1 - cos(theta) between two feature vectors.
If either vector has zero norm, this returns 1.
Instances For
Minkowski distance of order p between two feature vectors.
This generalizes L1 (Manhattan) and L2 (Euclidean). For p = 1 this is the L1 norm, and for
p = 2 it is the L2 norm.
Instances For
Normalize a vector of (nonnegative) scores into a probability distribution.
If the total is 0, this returns the uniform distribution.
PyTorch analogue: probs / probs.sum() (with an explicit zero-sum guard).
Instances For
L2-normalize a vector.
If the norm is 0, this returns the input unchanged.
Instances For
Z-score normalization: subtract mean and divide by standard deviation.
If the standard deviation is 0, this returns the mean-centered vector.