flopscope.

flopscope.numpy.linalg.pinv

fnp.linalg.pinv(a: 'ArrayLike', rcond: 'float | None' = None, hermitian: 'bool' = False, *, rtol: 'float | None' = None) -> 'FlopscopeArray'[flopscope source][numpy source]

Compute the (Moore-Penrose) pseudo-inverse of a matrix.

Adapted from NumPy docs np.linalg.pinv

Arealinalg
Typecustom
Cost
mnmin(m,n)m \cdot n \cdot \min(m,n)
Flopscope Context

Pseudoinverse. Cost: m*n*min(m,n) (via SVD).

Calculate the generalized inverse of a matrix using its singular-value decomposition (SVD) and including all large singular values.

Parameters

a:(..., M, N) array_like

Matrix or stack of matrices to be pseudo-inverted.

rcond:(...) array_like of float, optional

Cutoff for small singular values. Singular values less than or equal to rcond * largest_singular_value are set to zero. Broadcasts against the stack of matrices. Default: 1e-15.

hermitian:bool, optional

If True, a is assumed to be Hermitian (symmetric if real-valued), enabling a more efficient method for finding singular values. Defaults to False.

rtol:(...) array_like of float, optional

Same as rcond, but it's an Array API compatible parameter name. Only rcond or rtol can be set at a time. If none of them are provided then NumPy's 1e-15 default is used. If rtol=None is passed then the API standard default is used.

Added in version 2.0.0.

Returns

B:(..., N, M) ndarray

The pseudo-inverse of a. If a is a matrix instance, then so is B.

Raises

:LinAlgError

If the SVD computation does not converge.

See also

Notes

The pseudo-inverse of a matrix A, denoted A+A^+, is defined as: "the matrix that 'solves' [the least-squares problem] Ax=bAx = b," i.e., if xˉ\bar{x} is said solution, then A+A^+ is that matrix such that xˉ=A+b\bar{x} = A^+b.

It can be shown that if Q1ΣQ2T=AQ_1 \Sigma Q_2^T = A is the singular value decomposition of A, then A+=Q2Σ+Q1TA^+ = Q_2 \Sigma^+ Q_1^T, where Q1,2Q_{1,2} are orthogonal matrices, Σ\Sigma is a diagonal matrix consisting of A's so-called singular values, (followed, typically, by zeros), and then Σ+\Sigma^+ is simply the diagonal matrix consisting of the reciprocals of A's singular values (again, followed by zeros). [1]_

References

footnote
1

G. Strang, Linear Algebra and Its Applications, 2nd Ed., Orlando,
FL, Academic Press, Inc., 1980, pp. 139-142.

Examples

The following example checks that a * a+ * a == a and a+ * a * a+ == a+:

>>> import flopscope.numpy as fnp
>>> rng = flops.random.default_rng()
>>> a = rng.normal(size=(9, 6))
>>> B = flops.linalg.pinv(a)
>>> flops.allclose(a, flops.dot(a, flops.dot(B, a)))
True
>>> flops.allclose(B, flops.dot(B, flops.dot(a, B)))
True