Skip to contents

The pseudoinverse may be defined algebraically_ but it is more computationally convenient to understand it through the SVD_ Supports input of float, double, cfloat and cdouble dtypes. Also supports batches of matrices, and if A is a batch of matrices then the output has the same batch dimensions.


linalg_pinv(A, rcond = NULL, hermitian = FALSE, atol = NULL, rtol = NULL)



(Tensor): tensor of shape (*, m, n) where * is zero or more batch dimensions.


(float or Tensor, optional): the tolerance value to determine when is a singular value zero If it is a torch_Tensor, its shape must be broadcastable to that of the singular values of A as returned by linalg_svd(). Alias for rtol. Default: 0.


(bool, optional): indicates whether A is Hermitian if complex or symmetric if real. Default: FALSE.


the absolute tolerance value. When NULL it’s considered to be zero.


the relative tolerance value. See above for the value it takes when NULL.


If hermitian= TRUE, A is assumed to be Hermitian if complex or symmetric if real, but this is not checked internally. Instead, just the lower triangular part of the matrix is used in the computations. The singular values (or the norm of the eigenvalues when hermitian= TRUE) that are below the specified rcond threshold are treated as zero and discarded in the computation.


This function uses linalg_svd() if hermitian= FALSE and linalg_eigh() if hermitian= TRUE. For CUDA inputs, this function synchronizes that device with the CPU.

Consider using linalg_lstsq() if possible for multiplying a matrix on the left by the pseudoinverse, as linalg_lstsq(A, B)$solution == A$pinv() %*% B

It is always prefered to use linalg_lstsq() when possible, as it is faster and more numerically stable than computing the pseudoinverse explicitly.


if (torch_is_installed()) {
A <- torch_randn(3, 5)
#> torch_tensor
#>  0.6886  0.2109  0.0239
#>  0.0302  0.1808  0.1159
#>  0.3785  0.2301 -0.3357
#> -0.4896 -0.6582  0.2559
#> -0.1375 -0.4979  0.0205
#> [ CPUFloatType{5,3} ]