scipy.linalg.

pinv#

scipy.linalg.pinv(a,*,atol=None,rtol=None,return_rank=False,check_finite=True)[source]#

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

Calculate a generalized inverse of a matrix using itssingular-value decompositionU@S@V in the economy mode and pickingup only the columns/rows that are associated with significant singularvalues.

Ifs is the maximum singular value ofa, then thesignificance cut-off value is determined byatol+rtol*s. Anysingular value below this value is assumed insignificant.

The documentation is written assuming array arguments are of specified“core” shapes. However, array argument(s) of this function may have additional“batch” dimensions prepended to the core shape. In this case, the array is treatedas a batch of lower-dimensional slices; seeBatched Linear Operations for details.

Parameters:
a(M, N) array_like

Matrix to be pseudo-inverted.

atolfloat, optional

Absolute threshold term, default value is 0.

Added in version 1.7.0.

rtolfloat, optional

Relative threshold term, default value ismax(M,N)*eps whereeps is the machine precision value of the datatype ofa.

Added in version 1.7.0.

return_rankbool, optional

If True, return the effective rank of the matrix.

check_finitebool, optional

Whether to check that the input matrix contains only finite numbers.Disabling may give a performance gain, but may result in problems(crashes, non-termination) if the inputs do contain infinities or NaNs.

Returns:
B(N, M) ndarray

The pseudo-inverse of matrixa.

rankint

The effective rank of the matrix. Returned ifreturn_rank is True.

Raises:
LinAlgError

If SVD computation does not converge.

See also

pinvh

Moore-Penrose pseudoinverse of a hermitian matrix.

Notes

IfA is invertible then the Moore-Penrose pseudoinverse is exactlythe inverse ofA[1]. IfA is not invertible then theMoore-Penrose pseudoinverse computes thex solution toAx=b suchthat||Ax-b|| is minimized[1].

References

[1](1,2,3)

Penrose, R. (1956). On best approximate solutions of linear matrixequations. Mathematical Proceedings of the Cambridge PhilosophicalSociety, 52(1), 17-19. doi:10.1017/S0305004100030929

Examples

Given anmxn matrixA and annxm matrixB the fourMoore-Penrose conditions are:

  1. ABA=A (B is a generalized inverse ofA),

  2. BAB=B (A is a generalized inverse ofB),

  3. (AB)*=AB (AB is hermitian),

  4. (BA)*=BA (BA is hermitian)[1].

Here,A* denotes the conjugate transpose. The Moore-Penrosepseudoinverse is a uniqueB that satisfies all four of theseconditions and exists for anyA. Note that, unlike the standardmatrix inverse,A does not have to be a square matrix or havelinearly independent columns/rows.

As an example, we can calculate the Moore-Penrose pseudoinverse of arandom non-square matrix and verify it satisfies the four conditions.

>>>importnumpyasnp>>>fromscipyimportlinalg>>>rng=np.random.default_rng()>>>A=rng.standard_normal((9,6))>>>B=linalg.pinv(A)>>>np.allclose(A@B@A,A)# Condition 1True>>>np.allclose(B@A@B,B)# Condition 2True>>>np.allclose((A@B).conj().T,A@B)# Condition 3True>>>np.allclose((B@A).conj().T,B@A)# Condition 4True
On this page