在SymPy中反转矩阵?

时间:2010-11-21 17:34:56

标签: python matrix sympy

我想知道如何使用Python中的SymPy创建矩阵并计算其逆?

例如,对于这个符号矩阵:

2 个答案:

答案 0 :(得分:13)

如果您的问题是:如何在同一时间计算矩阵M的倒数:

M_inverse = M.inv()

至于如何创建矩阵:

M = Matrix(2,3, [1,2,3,4,5,6])

将为您提供以下2X3矩阵:

1 2 3

4 5 6

请参阅:http://docs.sympy.org/0.7.2/modules/matrices/matrices.html

答案 1 :(得分:2)

以下是我们如何为符号矩阵计算逆的示例(从问题中选择一个):

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]])

现在打印反matrix.inv()将给出:
enter image description here

可以进一步简化为sym.simplify(matrix.inv())
enter image description here

相关问题