Open In App

Python | sympy.diag() method

With the help of sympy.diag() method, we can create a matrix having dimension nxn and filled with numbers in the diagonal by using sympy.diag() method.

Syntax : sympy.diag()
Return : Return a new matrix.



Example #1 :
In this example, we can see that by using sympy.diag() method, we are able to create a matrix having dimension nxn all filled with numbers in a diagonal as passed in a parameter.




# import sympy
from sympy import *
  
# Use sympy.diag() method
mat = diag(3, 4)
   
print(mat)

Output :

Matrix([
[3, 0],
[0, 4]])



Example #2 :




# import sympy
from sympy import *
  
# Use sympy.diag() method
mat = diag(3, 4, 5, 8)
   
print(mat)

Output :

Matrix([
[3, 0, 0, 0],
[0, 4, 0, 0],
[0, 0, 5, 0],
[0, 0, 0, 8]])

Article Tags :