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 decomposition
U@S@Vin the economy mode and pickingup only the columns/rows that are associated with significant singularvalues.If
sis 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 is
max(M,N)*epswhereepsis 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
pinvhMoore-Penrose pseudoinverse of a hermitian matrix.
Notes
If
Ais invertible then the Moore-Penrose pseudoinverse is exactlythe inverse ofA[1]. IfAis not invertible then theMoore-Penrose pseudoinverse computes thexsolution toAx=bsuchthat||Ax-b||is minimized[1].References
Examples
Given an
mxnmatrixAand annxmmatrixBthe fourMoore-Penrose conditions are:ABA=A(Bis a generalized inverse ofA),BAB=B(Ais a generalized inverse ofB),(AB)*=AB(ABis hermitian),(BA)*=BA(BAis hermitian)[1].
Here,
A*denotes the conjugate transpose. The Moore-Penrosepseudoinverse is a uniqueBthat satisfies all four of theseconditions and exists for anyA. Note that, unlike the standardmatrix inverse,Adoes 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