Tensor Numerical Algorithms #
Reference implementations shared by classical models, graph specifications, and runtime checks:
- matrix minors, determinants, inverses, and power iteration;
- distances between vectors;
- vector normalization.
Intent / tradeoffs #
These definitions prioritize:
- mathematical clarity, and
- shape safety (via
TorchLean.Tensor), over performance.
In particular, determinantSpec uses Laplace expansion, which is exponentially expensive and is
only meant for small matrices (for example, 2 x 2 or 3 x 3) and proof-oriented reference code. For
large-scale linear algebra, use the runtime layer with array-backed kernels.
Matrix minor: delete row and col from an n × n matrix, producing an (n-1) × (n-1) matrix.
This is used by determinantSpec (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 a caller-selected number of power-iteration steps.
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). The explicit positivity hypothesis rules out the undefined order-zero and negative-order cases.
Instances For
Divide a vector by its sum when that sum is positive.
If the sum is not positive, this returns the uniform vector. When the input entries are nonnegative, the positive-sum branch is a probability 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
L2-normalize a vector with an additive regularizer under the square root.
The denominator is
sqrt (sumᵢ vector[i] ^ 2 + regularizer).
Unlike normalizeL2Spec, this operation has no zero-norm branch. Callers are responsible for
choosing a regularizer that makes the denominator meaningful in their scalar context. This is the
normalization convention used by several attention and recurrent architectures, where the exact
regularizer is part of the model specification.
Instances For
Z-score normalization: subtract the mean and divide by the population standard deviation.
Zero denominators follow the same convention as normalizeL2Spec and
normalizeByPositiveSumSpec: each denominator is tested before it is used. When n = 0 there is
nothing to normalize and the empty input is returned without ever forming sum / n. When the
standard deviation is 0, the mean-centered vector is returned.