Python Inverse Matrix

How to get inverse matrix in python? I implemented it myself, but this is pure python, and I suspect there are faster modules for this.

+55
python algorithm matrix linear-algebra matrix-inverse
Oct 17 '08 at 5:30
source share
6 answers

You should take a look at numpy if you are doing matrix manipulations. 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 perform other manipulations with matrices.

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 AT # Transpose of A. print A*x # Matrix multiplication of A and x. print AI # Inverse of A. print linalg.solve(A, x) # Solve the linear equation system. 

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

+99
Oct 17 '08 at 5:41
source share
β€” -

Make sure you really need to invert the matrix. This is often unnecessary and can be numerically unstable. When most people ask how to invert the matrix, they really want to know how to solve Ax = b, where A is the matrix, and x and b are vectors. It is more efficient and more accurate to use code that solves the equation Ax = b for x directly than to calculate A, and then multiply the inverse by B. Even if you need to solve Ax = b for many values ​​of b, this is not very good the idea is to invert A. If you need to solve the system for several values ​​of b, keep the Cholesky A factorization, but don't invert it.

See Do not invert this matrix .

+52
Oct 18 '08 at 20:12
source share
It is a pity that the selected matrix repeated here again is either singular or poorly conditioned:
 A = matrix( [[1,2,3],[11,12,13],[21,22,23]]) 

By definition, the inverse of A when multiplied by the matrix A should give the identity matrix. And, chosen in a highly praised explanation, it does not. In fact, just looking at inversion gives the key to conclude that inversion does not work correctly. Look at the magnitude of the individual terms - they are very, very large compared to the terms of the original matrix A ...

It’s great that people, when choosing an example of a matrix, can so often choose a particular matrix!

I had a problem with the solution, so I examined it further. On the ubuntu-kubuntu platform, the numian package of the debian package has no linalg matrix and subpackages, so in addition to importing numpy, you also need to import scipy.

If the diagonal terms of A are multiplied by a sufficiently large coefficient, say 2, the matrix will most likely cease to be singular or almost singular. So,

 A = matrix( [[2,2,3],[11,24,13],[21,22,46]]) 

does not become either singular or almost singular, and the example gives significant results ... When working with floating numbers, it is necessary to monitor the consequences of the inevitable rounding errors.

Thanks for your input,

OldAl.

+10
Jun 27 2018-10-18T00:
source share

You can calculate the determinant of a matrix that is recursive and then form the adjoint matrix

Here is a short tutorial

I think this only works for square matrices

Another way of calculating is to orthogonalize the gram-schmidt, and then transpose the matrix, transpose the orthogonalized matrix is ​​its inverse!

+6
03 Mar. '09 at 7:46
source share

Numpy will be suitable for most people, but you can also make matrices in Sympy

Try these commands at http://live.sympy.org/

 M = Matrix([[1, 3], [-2, 3]]) M M**-1 

For fun, try M**(1/2)

+5
Jan 14 '14 at 23:49
source share

If you hate numpy, exit RPy and your local copy of R and use it instead.

(I would also like for you to really need to invert the matrix. In R, for example, linalg.solve and the solve () function do not actually perform a complete inversion, as this is optional.)

+1
Oct 17 '08 at 20:25
source share



All Articles