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
¶
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
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
triangular_3x3_det_and_inverse(A, *, lower=True)
¶
Compute determinant and inverse of triangular 3×3 matrices.
Optimized computation for triangular matrices that exploits their structure. Determinant is simply the product of diagonal elements, and inverse is computed via triangular solve.
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_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.