Related Articles

# Python | sympy.diag() method

• Last Updated : 29 Jun, 2019

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

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up