Check if a given matrix is Hankel or not
Last Updated :
17 Aug, 2022
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
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 4
bool checkHankelMatrix( int n, int m[N][N])
{
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
if (i + j < n) {
if (m[i][j] != m[i + j][0])
return false ;
}
else {
if (m[i][j] != m[i + j - n + 1][n - 1])
return false ;
}
}
}
return true ;
}
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
import java.io.*;
import java.util.*;
class GFG {
static boolean checkHankelMatrix( int n,
int m[][])
{
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < n; j++) {
if (i + j < n) {
if (m[i][j] != m[i + j][ 0 ])
return false ;
}
else {
if (m[i][j] !=
m[i + j - n + 1 ][n - 1 ])
return false ;
}
}
}
return true ;
}
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" );
}
}
|
Python 3
N = 4
def checkHankelMatrix(n, m):
for i in range ( 0 , n):
for j in range ( 0 , n):
if (i + j < n):
if (m[i][j] ! = m[i + j][ 0 ]):
return False
else :
if (m[i][j] ! =
m[i + j - n + 1 ][n - 1 ]):
return False
return True
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" ))
|
C#
using System;
class GFG {
static bool checkHankelMatrix( int n,
int [,]m)
{
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
if (i + j < n) {
if (m[i, j] != m[i + j, 0])
return false ;
}
else {
if (m[i,j] != m[i + j - n
+ 1, n - 1])
return false ;
}
}
}
return true ;
}
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" );
}
}
|
PHP
<?php
$N = 4;
function checkHankelMatrix( $n , $m )
{
for ( $i = 0; $i < $n ; $i ++) {
for ( $j = 0; $j < $n ; $j ++) {
if ( $i + $j < $n ) {
if ( $m [ $i ][ $j ] != $m [ $i + $j ][0])
return false;
}
else {
if ( $m [ $i ][ $j ] != $m [ $i + $j -
$n + 1][ $n - 1])
return false;
}
}
}
return true;
}
$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" ;
?>
|
Javascript
<script>
function checkHankelMatrix(n,m)
{
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (i + j < n) {
if (m[i][j] != m[i + j][0])
return false ;
}
else {
if (m[i][j] !=
m[i + j - n + 1][n - 1])
return false ;
}
}
}
return true ;
}
let n = 4;
let m = [
[1, 2, 3, 5 ],
[ 2, 3, 5, 8] ,
[ 3, 5, 8, 0 ],
[ 5, 8, 0, 9 ]
];
if (checkHankelMatrix(n, m))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Complexity Analysis:
- Time Complexity: O(N2)
- Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...