Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Inverse of a Matrix

How do I get the inverse of a matrix in python? I've implemented it myself, but it's pure python, and I suspect there are faster modules out there to do it.

like image 352
Claudiu Avatar asked Oct 17 '08 05:10

Claudiu


People also ask

How do you take 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.

How do you take the inverse of a Numpy matrix?

We use numpy. linalg. inv() function to calculate the inverse of a matrix. The inverse of a matrix is such that if it is multiplied by the original matrix, it results in identity matrix.

How do you write inverse in Python?

The acos() function in Python returns the inverse cosine of a number. To be more specific, it returns the inverse cosine of a number in the radians.


1 Answers

You should have a look at numpy if you do matrix manipulation. This is a module mainly written in C, which will be much faster than programming in pure python. Here is an example of how to invert a matrix, and do other matrix manipulation.

from numpy import matrix from numpy import linalg A = matrix( [[1,2,3],[11,12,13],[21,22,23]]) # Creates a matrix. x = matrix( [[1],[2],[3]] )                  # Creates a matrix (like a column vector). y = matrix( [[1,2,3]] )                      # Creates a matrix (like a row vector). print A.T                                    # Transpose of A. print A*x                                    # Matrix multiplication of A and x. print A.I                                    # Inverse of A. print linalg.solve(A, x)     # Solve the linear equation system. 

You can also have a look at the array module, which is a much more efficient implementation of lists when you have to deal with only one data type.

like image 95
Mapad Avatar answered Sep 20 '22 14:09

Mapad