Python Program to check whether given matrix is a Hankel matrix or not
In this article, we will learn how to check whether a given matrix is a Hankel matrix or not.
The Hankel matrix is a square matrix in which each ascending skew-diagonal from left to right is constant. The Hankel matrix is a symmetric matrix. To know more about the Hankel matrix click here
Example
Input: matrix = [[1, 2, 3, 5], [2, 3, 5, 8], [3, 5, 8, 0], [5, 8, 0, 9]] Output: Hankel matrix
Hankel Matrix in Python
1. Load the input matrix.
2. Iterate through every row and column of the matrix.
3. Now check, whether the current element is equal to the corresponding diagonal constant if not return false
4. Else, return true.
def Hankelmatrix(matrix): n = len(matrix) # for each row for i in range(n): # for each column for j in range(n): if (i+j<n): if (matrix[i][j]!=matrix[i+j][0]): return False else: if (matrix[i][j] != matrix[i+j-n+1][n-1]): return False return True matrix = [[1, 4, 3, 5], [4, 3, 5, 6], [3, 5, 6, 0], [5, 6, 0, 1]] print("The given matrix: ") for i in range(len(matrix)): for j in range(len(matrix[0])): print(matrix[i][j], end= ' ') print() if(Hankelmatrix(matrix)): print("Hankel matrix") else: print("Not a Hankel matrix")
Output
The given matrix: 1 4 3 5 4 3 5 6 3 5 6 0 5 6 0 1 Hankel matrix The given matrix: 1 14 3 5 4 6 5 61 3 5 6 10 5 6 0 8 Not a Hankel matrix
Also, refer:
- Matrix Chain Multiplication in Python
- Convert a Matrix to a Sparse Matrix in Python
- Matrix transpose without NumPy in Pytho
Leave a Reply