Inverse of a matrix in SymPy?
I was wondering how to create a matr开发者_运维知识库ix and compute its inverse using SymPy in Python?
For example, for this symbolic matrix:
If your question was: How to compute the inverse of a matrix M in sympy then:
M_inverse = M.inv()
As for how to create a matrix:
M = Matrix(2,3, [1,2,3,4,5,6])
will give you the following 2X3 matrix:
1 2 3
4 5 6
See: http://docs.sympy.org/0.7.2/modules/matrices/matrices.html
Here is example of how we can compute inverse for a symbolic matrix (taking the one from the question):
import sympy as sym
# Not necessary but gives nice-looking latex output
# More info at: http://docs.sympy.org/latest/tutorial/printing.html
sym.init_printing()
sx, sy, rho = sym.symbols('sigma_x sigma_y rho')
matrix = sym.Matrix([[sx ** 2, rho * sx * sy],
[rho * sx * sy, sy ** 2]])
Now printing the inverse matrix.inv()
will give:
which can be further simplified like sym.simplify(matrix.inv())
:
精彩评论