Calculate the inverse of a non-square matrix using numpy

Viewed 4930

Is there a way where I can calculate the inverse of a mxn non-square matrix using numpy? Since using la.inv(S) seems to give me an error of ValueError: expected square matrix

2 Answers

To calculate the non square matrix mxn, We can use np.linalg.pinv(S), here s is the data you want to pass.

For square matrix we use np.linalg.inv(S), The inverse of a matrix is such that if it is multiplied by the original matrix, it results in identity matrix.

note: np is numpy

We can also use np.linalg.inv(S) for non square matrix but in order to not get any error you need to slice the data S.

For more details on np.linalg.pinv : https://numpy.org/doc/stable/reference/generated/numpy.linalg.pinv.html

Related