Python同情 | Matrix.diagonalize() 方法
借助sympy.Matrix().diagonalize()方法,我们可以对矩阵进行对角化。对角化()返回一个元组 , 在哪里是对角线并且 .
Syntax: Matrix().diagonalize()
Returns: Returns a tuple of matrix where the second element represents the diagonal of the matrix.
示例 #1:
# import sympy
from sympy import * M = Matrix([[3, -2, 4, -2],
[5, 3, -3, -2],
[5, -2, 2, -2],
[5, -2, -3, 3]])
print("Matrix : {} ".format(M))
# Use sympy.diagonalize() method
P, D = M.diagonalize()
print("Diagonal of a matrix : {}".format(D))
输出:
Matrix : Matrix([[3, -2, 4, -2], [5, 3, -3, -2], [5, -2, 2, -2], [5, -2, -3, 3]])
Diagonal of a matrix : Matrix([[-2, 0, 0, 0], [0, 3, 0, 0], [0, 0, 5, 0], [0, 0, 0, 5]])
示例 #2:
# import sympy
from sympy import * M = Matrix([[1, -3, 3], [3, -5, 3], [6, -6, 4]])
print("Matrix : {} ".format(M))
# Use sympy.diagonalize() method
P, D = M.diagonalize()
print("Diagonal of a matrix : {}".format(D))
输出:
Matrix : Matrix([[1, -3, 3], [3, -5, 3], [6, -6, 4]])
Diagonal of a matrix : Matrix([[-2, 0, 0], [0, -2, 0], [0, 0, 4]])