Skip to Content

PINV

Overview

The PINV function computes the Moore-Penrose pseudoinverse of a matrix using singular value decomposition (SVD). The pseudoinverse is a generalization of the matrix inverse that exists for any matrix, including non-square or singular matrices. It is widely used to solve linear systems that may not have a unique solution, and in applications such as least squares fitting and signal processing. The pseudoinverse A+A^+ of a matrix AA is the unique matrix that satisfies the four Moore-Penrose conditions:

ABA=ABAB=B(AB)=AB(BA)=BAABA = A \\ BAB = B \\ (AB)^* = AB \\ (BA)^* = BA

where AA^* denotes the conjugate transpose. If AA is invertible then the Moore-Penrose pseudoinverse is exactly the inverse of AA. If AA is not invertible then the Moore-Penrose pseudoinverse computes the xx solution to Ax=bAx = b such that Axb\|Ax - b\| is minimized. For more details, see the SciPy documentation.

This example function is provided as-is without any representation of accuracy.

Usage

To use the function in Excel:

=PINV(matrix, [atol], [rtol], [return_rank], [check_finite])
  • matrix (2D list, required): The matrix to be pseudo-inverted. Must be a 2D list of numbers.
  • atol (float, optional, default=0.0): Absolute threshold for small singular values to be considered zero.
  • rtol (float, optional, default=None): Relative threshold for small singular values to be considered zero. If None, uses max(M, N) * eps.
  • return_rank (bool, optional, default=False): If True, also returns the effective rank of the matrix.
  • check_finite (bool, optional, default=True): If True, checks that the input contains only finite numbers.

The function returns the pseudoinverse of the matrix as a 2D list. If return_rank is True, returns a list [pseudoinverse, rank]. If the input is invalid, returns an error message (string or 2D list of strings).

Wrap all JSX or HTML characters outside code blocks in backticks.

Examples

Example 1: Pseudoinverse of a 2x2 invertible matrix

Inputs:

12
34

Excel formula:

=PINV({1,2;3,4})

Expected output:

-2.01.0
1.5-0.5

Example 2: Pseudoinverse of a 3x2 matrix

Inputs:

12
34
56

Excel formula:

=PINV({1,2;3,4;5,6})

Expected output:

-1.333333-0.3333330.666667
1.0833330.333333-0.416667

Example 3: Pseudoinverse with return_rank=TRUE

Excel formula:

=PINV({1,2;3,4}, , , TRUE)

Expected output:

2

Example 4: Pseudoinverse with atol and rtol

Excel formula:

=PINV({1,2;3,4}, 1e-5, 1e-5)

Expected output:

-2.01.0
1.5-0.5

Python Code

import numpy as np from scipy.linalg import pinv as scipy_pinv def pinv(matrix, atol=0.0, rtol=None, return_rank=False, check_finite=True): """ Compute the Moore-Penrose pseudoinverse of a matrix using SVD. Args: matrix: 2D list of numbers to be pseudo-inverted. atol: Absolute threshold for small singular values (default: 0.0). rtol: Relative threshold for small singular values (default: None). return_rank: If True, only return the effective rank as a 2D list (default: False). check_finite: If True, check that the input contains only finite numbers (default: True). Returns: 2D list pseudoinverse of the matrix, or [[rank]] if return_rank is True, or error message (str or 2D list of str) if input is invalid. This example function is provided as-is without any representation of accuracy. """ # Validate input if not isinstance(matrix, list) or not matrix or not isinstance(matrix[0], list): return [["Invalid input: matrix must be a 2D list."]] try: arr = np.array(matrix, dtype=float) except Exception: return [["Invalid input: matrix must contain only numbers."]] try: if return_rank: pinv_result, rank = scipy_pinv(arr, atol=atol, rtol=rtol, return_rank=True, check_finite=check_finite) return [[int(rank)]] else: result = scipy_pinv(arr, atol=atol, rtol=rtol, check_finite=check_finite) return result.tolist() except Exception as e: return [[f"scipy.linalg.pinv error: {e}"]]

Live Notebook

Edit this function in a live notebook.

Live Demo

Last updated on