filtered_reciprocal#
- t3toolbox.backend.linalg.filtered_reciprocal(s, eps, rcond=None)#
def filtered_reciprocal( s: NDArray, # singular values, shape=C+(k,), descending eps: typ.Optional[NDArray], # damping, shape=C (broadcast over k); None -> exact clipped 1/s rcond: typ.Optional[typ.Union[float, NDArray]] = None, # relative clip vs s[..., :1]: a scalar, or # broadcastable to C+(1,); None -> dtype eps * k ) -> NDArray: # f(s), shape=C+(k,); 0 where clipped
The clipped, optionally damped reciprocal of a spectrum:
f(s) = s / (s^2 + eps^2).Every
sat or below the cliprcond * s[..., :1](relative to the largest value, so the spectrum must be descending, as an SVD returns it) maps to exactly 0, whateverepsis.rcondis one scalar for the whole stack, or an array broadcastable toC+(1,)– one clip per stack element (the uniform lift passes each padded block’s clip at its REAL dimensions this way):eps=None– the exact clipped reciprocal1/s, the filter of a minimum-norm pseudo-inverse – the shared geometry’s clipped group solve (its tied projection and retraction) uses exactly this, bit for bit what it always computed;epsgiven (one value per stack element, broadcast over thekvalues) – the Tikhonov filters / (s^2 + eps^2):~1/sfors >> eps,~s/eps^2fors << eps, and never above1/(2 eps).eps = 0is the exact reciprocal up to rounding (s/s^2).
The clip is what makes
eps = 0safe at a zero-padded warm start, whose padded singular values come back from LAPACK at roundoff (~1e-17) rather than at an exact zero; a zero spectrum (the zero tensor) is clipped entirely, sofis identically 0 there. Both guards are a doublewhere(no division by zero is ever evaluated on a selected branch), so the result is finite everywhere and jit-safe. Dispatch is inferred fromsandeps.Examples
>>> import numpy as np >>> import t3toolbox.backend.linalg as linalg >>> s = np.array([4.0, 2.0, 1e-20]) # the last value is a roundoff "zero" >>> linalg.filtered_reciprocal(s, None).tolist() # exact 1/s, roundoff clipped to 0 [0.25, 0.5, 0.0] >>> f = linalg.filtered_reciprocal(s, np.array(1.0)) # damped: s / (s^2 + 1) >>> print(np.allclose(f, [4 / 17, 2 / 5, 0.0])) True
A spectrum stack carries one damping per element (shape
C), broadcast over its values:>>> S = np.array([[4.0, 2.0], [1.0, 0.5]]) # C = (2,), k = 2 >>> print(linalg.filtered_reciprocal(S, np.array([0.0, 1.0])).round(3).tolist()) [[0.25, 0.5], [0.5, 0.4]]