矩阵的 Python 逆矩阵 [英] Python Inverse of a Matrix
问题描述
如何在python中获得矩阵的逆?我自己实现了它,但它是纯 python 的,我怀疑有更快的模块可以做到这一点.
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.
推荐答案
你应该看看 numpy 如果您进行矩阵操作.这是一个主要用C编写的模块,比用纯python编程要快得多.这是一个如何求逆矩阵和进行其他矩阵操作的示例.
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.
您还可以查看 array 模块,这是一个当您只需要处理一种数据类型时,列表的实现效率会更高.
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.
这篇关于矩阵的 Python 逆矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!