kups.core.utils.math
¶
Mathematical utilities for numerical computations.
This module provides specialized numerical algorithms including logarithmic factorial ratios, polynomial root finding, and optimized matrix operations for 3×3 matrices.
MatmulSide
¶
SquareMatrix
¶
Bases: Protocol
Source code in src/kups/core/utils/math.py
array
property
¶
Underlying array of shape (..., 3, 3) representing the matrix.
det()
¶
inverse()
¶
matmul(x, *, side=MatmulSide.RIGHT)
¶
Matrix-vector or matrix-matrix multiplication.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Array | SquareMatrix
|
Vector or matrix to multiply. |
required |
side
|
MatmulSide | str
|
Multiplication side:
- |
RIGHT
|
Returns:
| Type | Description |
|---|---|
Array | SquareMatrix
|
Result of the multiplication. |
Source code in src/kups/core/utils/math.py
scale(factor)
¶
Scale the matrix by a given factor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
factor
|
Array
|
Scalar factor to scale the matrix. |
required |
Returns:
| Type | Description |
|---|---|
SquareMatrix
|
Scaled matrix. |
cubic_roots(coefficients)
¶
Find all roots of a cubic polynomial using the companion matrix method.
Solves ax³ + bx² + cx + d = 0 by computing eigenvalues of the companion
matrix. Returns all three roots (real or complex).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coefficients
|
Array
|
Array of shape |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Array of shape |
Example
Note
This method is numerically stable and handles multiple polynomials in parallel via vectorization.
Source code in src/kups/core/utils/math.py
det_and_inverse_3x3(A)
¶
Compute determinant and inverse of 3×3 matrices via the adjugate method.
Efficiently computes both the determinant and inverse of 3×3 matrices using explicit formulas for the cofactor matrix. More efficient than general matrix inversion for small matrices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
A
|
Array
|
Array of shape |
required |
Returns:
| Type | Description |
|---|---|
tuple[Array, Array]
|
Tuple of (determinant, inverse):
- determinant: Array of shape |
Source code in src/kups/core/utils/math.py
log_factorial_ratio(N, M)
¶
Compute log(N!/M!) efficiently using the log-gamma function.
Uses the identity log(n!) = lgamma(n+1) to compute the ratio directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
N
|
Array
|
Integer array (numerator factorials). |
required |
M
|
Array
|
Integer array (denominator factorials). |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Result containing |
Example
Source code in src/kups/core/utils/math.py
logm(A, *, hermitian=False)
¶
Compute the principal matrix logarithm via eigendecomposition.
Inverse of jax.scipy.linalg.expm: returns L with expm(L) == A for
diagonalizable A. Diagonalizing \(A = V\,\mathrm{diag}(\lambda)\,V^{-1}\)
gives \(\log A = V\,\mathrm{diag}(\log\lambda)\,V^{-1}\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
A
|
Array
|
Array of shape |
required |
hermitian
|
bool
|
If True, treat |
False
|
Returns:
| Type | Description |
|---|---|
Array
|
Array of shape |
Array
|
|
Source code in src/kups/core/utils/math.py
next_higher_power(value, base=2.0)
¶
Compute the next higher power of a given base for each element.
For each element in value, finds the smallest power of base that is
greater than or equal to that element.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Array
|
Array of shape |
required |
base
|
Array | float
|
Base for the power calculation (default is 2.0). |
2.0
|
Returns:
| Type | Description |
|---|---|
Array
|
Array of shape |
Example
Source code in src/kups/core/utils/math.py
solve_affine_ode(A, b, x0, dt)
¶
Solve the affine ODE \(\dot{x} = A\,x + b\) exactly over \([0, \Delta t]\).
Computes
Uses the augmented-matrix trick (Al-Mohy & Higham 2011) so that a single
matrix exponential delivers both terms and the formula is well-defined
even when A has zero eigenvalues:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
A
|
Array
|
Coefficient matrix, shape |
required |
b
|
Array
|
Constant inhomogeneous term, shape |
required |
x0
|
Array
|
Initial state, shape |
required |
dt
|
Array | float
|
Scalar timestep (broadcastable to the batch shape). |
required |
Returns:
| Type | Description |
|---|---|
Array
|
State at |
Source code in src/kups/core/utils/math.py
triangular_3x3_det_and_inverse(A, *, lower=True)
¶
Compute determinant and inverse of triangular 3×3 matrices.
Exploits triangular structure with closed-form expressions: the determinant is the product of diagonal elements and the inverse follows from forward substitution. Upper-triangular inputs are handled by transposition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
A
|
Array
|
Array of shape |
required |
lower
|
bool
|
Whether matrices are lower (True) or upper (False) triangular. |
True
|
Returns:
| Type | Description |
|---|---|
tuple[Array, Array]
|
Tuple of (determinant, inverse):
- determinant: Array of shape |
Example
Source code in src/kups/core/utils/math.py
triangular_3x3_expm(A, *, lower=True)
¶
Matrix exponential of triangular 3×3 matrices.
Closed form from the fact that B and A = expm(B) commute: the diagonal
of A is exp of B's diagonal, and the off-diagonal couplings are
divided differences of exp over the diagonal entries. Using divided
differences keeps repeated diagonal entries (e.g. cubic / tetragonal cells)
well-conditioned.
Real and gradient-safe inverse of
triangular_3x3_logm; agrees with
jax.scipy.linalg.expm on triangular matrices but avoids its Padé
scaling-and-squaring iteration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
A
|
Array
|
Array of shape |
required |
lower
|
bool
|
Whether matrices are lower (True) or upper (False) triangular. |
True
|
Returns:
| Type | Description |
|---|---|
Array
|
Array of shape |
Source code in src/kups/core/utils/math.py
triangular_3x3_from_tril(tril)
¶
Assemble a lower-triangular 3×3 matrix from its 6 elements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tril
|
Array
|
Array of shape |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Array of shape [[m00, 0, 0], [m10, m11, 0], [m20, m21, m22]] |
Source code in src/kups/core/utils/math.py
triangular_3x3_logm(A, *, lower=True)
¶
Principal matrix logarithm of triangular 3×3 matrices with positive diagonal.
Closed form from the fact that L and B = logm(L) commute (BL = LB):
the diagonal of B is log of L's diagonal, and the off-diagonal
couplings are divided differences of log over the diagonal entries. Using
divided differences keeps repeated diagonal entries (e.g. cubic / tetragonal
cells) well-conditioned.
Real and gradient-safe, unlike the eigendecomposition-based
logm which is complex and inaccurate for these
(defective) matrices. Inverse of jax.scipy.linalg.expm on triangular
matrices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
A
|
Array
|
Array of shape |
required |
lower
|
bool
|
Whether matrices are lower (True) or upper (False) triangular. |
True
|
Returns:
| Type | Description |
|---|---|
Array
|
Array of shape |
Source code in src/kups/core/utils/math.py
triangular_3x3_matmul(L, x, *, lower=True, side=MatmulSide.RIGHT)
¶
Optimized matrix-vector multiplication for triangular 3×3 matrices.
Specialized implementation that exploits triangular structure to avoid computing with known-zero elements. On CPU, uses unrolled loops for better performance than einsum.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
L
|
Array
|
Array of shape |
required |
x
|
Array
|
Array of shape |
required |
lower
|
bool
|
Whether |
True
|
side
|
MatmulSide | str
|
Multiplication side:
- |
RIGHT
|
Returns:
| Type | Description |
|---|---|
Array
|
Shape |
Example
Note
Automatically selects between einsum (GPU) and unrolled loops (CPU) for optimal performance.