Inverse of non square matrix python

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

asked Nov 21, 2020 at 15:08

2

You are probably looking for np.linalg.pinv.

answered Nov 21, 2020 at 17:16

YoniChechikYoniChechik

1,21817 silver badges23 bronze badges

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 : //numpy.org/doc/stable/reference/generated/numpy.linalg.pinv.html

answered Sep 4, 2021 at 12:03

2

In this article we will discuss the steps and intuition for calculating the inverse of a matrix and show examples using Python.

Table of contents

  • Introduction
  • Inverse of a matrix explained
  • Inverse of a matrix defined
  • When does an inverse of a matrix exist?
  • Inverse of a 2x2 matrix
  • Inverses of larger matrices
  • Inverse of a matrix in Python
  • Conclusion

Introduction

The inverse of a matrix is an important concept in linear algebra. It is often seen in many equations and the simplest use case for it is helping find the solution of a system of linear equations though inversing a matrix.

We already know what a matrix represents, so now we can take a look at what is its inverse and how to calculate it.

To continue following this tutorial we will need the following Python library: numpy.

If you don’t have them installed, please open “Command Prompt” [on Windows] and install them using the following code:

pip install numpy

Inverse matrix explained

We already know what a matrix is and understand the use cases for it in linear algebra. So what is an inverse matrix?

Inverse of a matrix defined

Let’s take a step back and and think about numbers. You can pick any number, for example 5. Can we find an inverse of 5? Yes! It’s simply its reciprocal, which is 1/5, which we also write as: 5^{-1}.

What is so unique about it? If you multiply a number by it’s inverse, your equation will be equal to 1:

Image by Author

Matrices work in a similar way. Consider we have some 2×2 matrix A:

Image by Author

and a 2×2 identity matrix I:

$$I = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}$$

We would think that there exists an inverse matrix A^{-1}, such that:

Image by Author

or in our case:

Image by Author

When does an inverse of a matrix exist?

But it turns out that the inverse matrix may not always exist! The inverse matrix of matrix A exists if and only if two of the below conditions are satisfied:

  1. Matrix A is a square matrix [2×2, 3×3, and so on] where the number of rows equals to the number of columns
  2. The determinant of matrix A is not equal to zero: det[A] ≠ 0

Inverse of a 2×2 matrix

In our example we are working with a 2×2 matrix, whose determinant is equal to:

Image by Author

Then how do we actually get the inverse matrix A^{-1}?

In case of a 2×2 matrix it’s quite simple:

Image by Author

In our example:

Image by Author

Now we found the inverse of matrix A!

How can we check that it works?

Recall:

Image by Author

We can use matrix multiplication to check our result:

Image by Author

We have correctly calculate the inverse of a 2×2 matrix A!

We can also think about it graphically.

Let’s can represent matrix A in cartesian space, where the columns of the matrix become vectors:

Image by Author

Image by Author

And also represent the identity matrix in cartesian space, which are simply the base vectors:

Image by Author

Image by Author

Following this logic, the inverse matrix will also be represented by some vectors, and they are such that if we multiply matrix A[vector {a_1}, vector {a_2}] by vectors of its inverse matrix A^{-1} [let’s call them: vector {inv_1}, vector{inv_2}], the result should be the base vectors [vector{i_1}, vector{i_2}] which represent the identity matrix I.

Since we already calculate the inverse matrix A^{-1}, the resulting vectors are:

Image by Author

Image by Author

Inverses of larger matrices

Now, it is as simple to calculate the inverse for a large matrix like 3×3 or 5×5? Not really. As the size of the matrix increases, the complexity in a sense of numbers of steps also increases!

There are a few examples of calculations of inverses of larger matrices like 3×3 and 5×5 using Gaussian elimination which you can find online.

In general, for larger size matrices we prefer to have access to technology like Python to allows us to get the results in a quick and efficient way.

Inverse of a matrix in Python

In order to calculate the inverse matrix in Python we will use the numpy library. And the first step will be to import it:

Numpy has a lot of useful functions, and for this operation we will use the linalg.inv[] function which computes the inverse of a matrix in Python.

Recall that in Python matrices are constructed as arrays. And the next step will be to define the input matrices. We are going to use the same 2×2 matrix as in the example from the previous section:

Now that we have the required matrix, we can easily calculate its inverse:

And you should get:

[[ 5. -7.]
[-2. 3.]]

which is exactly the same output as in our example where we calculated it manually.

We can also check it it’s correct by using matrix multiplication in Python:

And you should get:

[[ 1.00000000e+00 -1.77635684e-15]
[ 0.00000000e+00 1.00000000e+00]]

which are the top right value is almost zero [numpy issue], but it is an identity matrix just like:

Image by Author

Conclusion

In this article we discussed the intuition behind matrix inversion using a linear algebra approach, as well as shown a complete example using Python.

Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Linear Algebra articles.

How do you invert a non square matrix in Python?

inv[S], The inverse of a matrix is such that if it is multiplied by the original matrix, it results in identity matrix. 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.

How do you find the inverse of a non square matrix?

Non-square matrices [m-by-n matrices for which m ≠ n] do not have an inverse. However, in some cases such a matrix may have a left inverse or right inverse. If A is m-by-n and the rank of A is equal to n [n ≤ m], then A has a left inverse, an n-by-m matrix B such that BA = In.

How do you find the inverse of a matrix in Python?

Python provides a very easy method to calculate the inverse of a matrix. The function numpy. linalg. inv[] which is available in the python NumPy module is used to compute the inverse of a matrix.

What is pseudo inverse in Python?

To Compute the [Moore-Penrose] pseudo-inverse of a matrix, use the numpy. linalg. pinv[] method in Python. Calculate the generalized inverse of a matrix using its singular-value decomposition [SVD] and including all large singular values. The 1st parameter, a is a Matrix or stack of matrices to be pseudo-inverted.

Chủ Đề