Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate the inverse of a non-square matrix using numpy

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

like image 422
H A Avatar asked Oct 18 '25 13:10

H A


2 Answers

You are probably looking for np.linalg.pinv.

EDIT 23-08-12:

Alaboration: pinv (psaudu-inverse) computes the Moore–Penrose pseudo inverse of a matrix using its SVD, when the matrix is non invertible ( thanks https://stackoverflow.com/users/774575/mins )

like image 75
YoniChechik Avatar answered Oct 22 '25 05:10

YoniChechik


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

like image 26
Ramahanisha Gunda Avatar answered Oct 22 '25 03:10

Ramahanisha Gunda