Check if a given matrix is Hankel or not
Given a matrix m[][] of size n x n. The task is to check whether given matrix is Hankel Matrix or not.
In linear algebra, a Hankel matrix (or catalecticant matrix), named after Hermann Hankel, is a square matrix in which each ascending skew-diagonal from left to right is constant.
Examples:
Input : n = 4,
m[][] = {
{1, 2, 3, 5},
{2, 3, 5, 8},
{3, 5, 8, 0},
{5, 8, 0, 9}
};
Output : Yes
All diagonal {1}, {2, 2}, {3, 3, 3}, {5, 5, 5, 5}, {8, 8, 8}, {9} have constant value.
So given matrix is Hankel Matrix.Input : n = 3,
m[][] = {
{1, 2, 3},
{2, 3, 5},
{3, 9, 8}
};
Output : No
Observe, for a matrix to be Hankel Matrix, it must be of the form,
a0 a1 a2 a3 a1 a2 a3 a4 a2 a3 a4 a5 a3 a4 a5 a6
Therefore, to check if the given matrix is Hankel Matrix, we need check if each m[i][j] == ai + j. Now, ai + j can be define as:
m[i+j][0], if i + j < n ai + j = m[i + j - n + 1][n-1], otherwise
C++
// C++ Program to check if given matrix is // Hankel Matrix or not. #include <bits/stdc++.h> using namespace std; #define N 4 // Function to check if given matrix is Hankel // Matrix or not. bool checkHankelMatrix( int n, int m[N][N]) { // for each row for ( int i = 0; i < n; i++) { // for each column for ( int j = 0; j < n; j++) { // checking if i + j is less than n if (i + j < n) { // checking if the element is equal to the // corresponding diagonal constant if (m[i][j] != m[i + j][0]) return false ; } else { // checking if the element is equal to the // corresponding diagonal constant if (m[i][j] != m[i + j - n + 1][n - 1]) return false ; } } } return true ; } // Drivers code int main() { int n = 4; int m[N][N] = { { 1, 2, 3, 5 }, { 2, 3, 5, 8 }, { 3, 5, 8, 0 }, { 5, 8, 0, 9 } }; checkHankelMatrix(n, m) ? (cout << "Yes" ) : (cout << "No" ); return 0; } |
Java
// Java Program to check if given matrix is // Hankel Matrix or not. import java.io.*; import java.util.*; class GFG { // Function to check if given matrix // is Hankel Matrix or not. static boolean checkHankelMatrix( int n, int m[][]) { // for each row for ( int i = 0 ; i < n; i++) { // for each column for ( int j = 0 ; j < n; j++) { // checking if i + j is less // than n if (i + j < n) { // checking if the element // is equal to the // corresponding diagonal // constant if (m[i][j] != m[i + j][ 0 ]) return false ; } else { // checking if the element // is equal to the // corresponding diagonal // constant if (m[i][j] != m[i + j - n + 1 ][n - 1 ]) return false ; } } } return true ; } // Drivers code public static void main(String args[]) { int n = 4 ; int m[][] = { { 1 , 2 , 3 , 5 }, { 2 , 3 , 5 , 8 }, { 3 , 5 , 8 , 0 }, { 5 , 8 , 0 , 9 } }; if (checkHankelMatrix(n, m)) System.out.println( "Yes" ); else System.out.println( "No" ); } } // This code is contributed by Anuj_67. |
Python 3
# Python 3 Program to check if given matrix is # Hankel Matrix or not. N = 4 # Function to check if given matrix is Hankel # Matrix or not. def checkHankelMatrix(n, m): # for each row for i in range ( 0 , n): # for each column for j in range ( 0 , n): # checking if i + j is less # than n if (i + j < n): # checking if the element is # equal to the corresponding # diagonal constant if (m[i][j] ! = m[i + j][ 0 ]): return False else : # checking if the element is # equal to the corresponding # diagonal constant if (m[i][j] ! = m[i + j - n + 1 ][n - 1 ]): return False return True # Drivers code n = 4 m = [[ 1 , 2 , 3 , 5 ,], [ 2 , 3 , 5 , 8 ,], [ 3 , 5 , 8 , 0 ,], [ 5 , 8 , 0 , 9 ]] ( print ( "Yes" ) if checkHankelMatrix(n, m) else print ( "No" )) # This code is contributed by Smitha. |
C#
// C# Program to check if given matrix is // Hankel Matrix or not. using System; class GFG { // Function to check if given matrix // is Hankel Matrix or not. static bool checkHankelMatrix( int n, int [,]m) { // for each row for ( int i = 0; i < n; i++) { // for each column for ( int j = 0; j < n; j++) { // checking if i + j is less // than n if (i + j < n) { // checking if the element // is equal to the // corresponding diagonal // constant if (m[i, j] != m[i + j, 0]) return false ; } else { // checking if the element // is equal to the // corresponding diagonal // constant if (m[i,j] != m[i + j - n + 1, n - 1]) return false ; } } } return true ; } // Drivers code public static void Main() { int n = 4; int [,]m = { { 1, 2, 3, 5 }, { 2, 3, 5, 8 }, { 3, 5, 8, 0 }, { 5, 8, 0, 9 } }; if (checkHankelMatrix(n, m)) Console.Write( "Yes" ); else Console.Write( "No" ); } } // This code is contributed by Anuj_67. |
PHP
<?php // PHP Program to check if given matrix is // Hankel Matrix or not. $N = 4; // Function to check if // given matrix is Hankel // Matrix or not. function checkHankelMatrix( $n , $m ) { // for each row for ( $i = 0; $i < $n ; $i ++) { // for each column for ( $j = 0; $j < $n ; $j ++) { // checking if i + j // is less than n if ( $i + $j < $n ) { // checking if the element // is equal to the corresponding // diagonal constant if ( $m [ $i ][ $j ] != $m [ $i + $j ][0]) return false; } else { // checking if the element // is equal to the // corresponding diagonal constant if ( $m [ $i ][ $j ] != $m [ $i + $j - $n + 1][ $n - 1]) return false; } } } return true; } // Driver code $n = 4; $m = array ( array ( 1, 2, 3, 5 ), array ( 2, 3, 5, 8 ), array ( 3, 5, 8, 0 ), array ( 5, 8, 0, 9 )); if (checkHankelMatrix( $n , $m )) echo "Yes" ; else echo "No" ; // This code is contributed by Anuj_67. ?> |
Yes
Complexity : O(n + n)
Recommended Posts:
- Check if matrix can be converted to another matrix by transposing square sub-matrices
- Program to check diagonal matrix and scalar matrix
- Check if it is possible to make the given matrix increasing matrix or not
- Program to check if a matrix is Binary matrix or not
- Check whether a given matrix is orthogonal or not
- Check if a Matrix is Invertible
- Check if a given matrix is sparse or not
- Check for possible path in 2D matrix
- Program to check idempotent matrix
- Check if sums of i-th row and i-th column are same in matrix
- Program to check Involutory Matrix
- Check if possible to cross the matrix with given power
- Program to check if a matrix is symmetric
- Check given matrix is magic square or not
- Program to check if matrix is singular or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.