VOOZH about

URL: https://www.geeksforgeeks.org/python/tridiagonal-matrix-in-python/

⇱ Tridiagonal matrix in python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Tridiagonal matrix in python

Last Updated : 2 Feb, 2021

A tridiagonal matrix is a matrix that has non-zero elements only at the main diagonal, diagonal below and above it. All other elements are zero. For this reason tridiagonal matrices of dimension smaller than or equal to 3 seem meaningless.

Example 1:

[a11,  a22,  0  ,  0  ,  0  ,  0  ]   

[a21,  a22,  a23,  0  ,  0  ,  0  ]

[0  ,  a32,  a33,  a34,  0  ,  0  ]

[0  ,  0  ,  a43,  a44,  a55,  0  ]

[0  ,  0  ,  0  ,  a54,  a55,  a56]

[0  ,  0  ,  0  ,  0  ,  a65,  a66]

Example 2:

[1,  1,  0,  0,  0,  0]   

[1,  1,  1,  0,  0,  0]

[0,  1,  1,  1,  0,  0]

[0,  0,  1,  1,  1,  0]

[0,  0,  0,  1,  1,  1]

[0,  0,  0,  0,  1,  1]

Approach 

  • Take the size of the matrix as input
  • Check if greater than 3
    • if not, exit
    • if yes, proceed further
  • Take elements of the matrix as input
  • Now put zero everywhere except at main diagonal and diagonals below and above the main diagonal.

Program:

Output:

👁 Image
output of our program
Comment
Article Tags:
Article Tags: