Saturday, September 6, 2025
HomeLanguagesPython sympy | Matrix.diagonalize() method

Python sympy | Matrix.diagonalize() method

With the help of sympy.Matrix().diagonalize() method, we can diagonalize a matrix. diagonalize() returns a tuple (P, D), where D is diagonal and M = PDP^{-1}.

Syntax: Matrix().diagonalize()

Returns: Returns a tuple of matrix where the second element represents the diagonal of the matrix.

Example #1:




# import sympy 
from sympy import * M = Matrix([[3, -24, -2],
                                [53, -3, -2],
                                [5, -22, -2],
                                [5, -2, -33]])
  
print("Matrix : {} ".format(M))
   
# Use sympy.diagonalize() method 
P, D = M.diagonalize()  
      
print("Diagonal of a matrix : {}".format(D))  


Output:

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

Example #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))


Output:

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

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32271 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6640 POSTS0 COMMENTS
Nicole Veronica
11806 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11869 POSTS0 COMMENTS
Shaida Kate Naidoo
6754 POSTS0 COMMENTS
Ted Musemwa
7030 POSTS0 COMMENTS
Thapelo Manthata
6705 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS