Find if given matrix is Toeplitz or not

Here given code implementation process.

/*
  C Program 
+ Find if given matrix is Toeplitz or not
*/
#include<stdio.h>
#define ROW 5
#define COL 5

//Function which is check diagonal element values are similar or not
int diagonal(int matrix[ROW][COL],int start_row,int start_col)
{
  
  int status  = 1;
  int element = matrix[start_row][start_col];
  for (int i = start_row+1,j=start_col+1; i < ROW && j<COL; ++i,++j)
  {
    if(matrix[i][j]!=element)
    {
      return 0;
    }
  }
 return 1;
}
//Determine whether matrix is Toeplitz matrix or not
void is_toeplitz(int matrix[ROW][COL])
{
  
  int result=1;

  for (int i = 0; i < COL && result==1; ++i)
  {
    //check upper triangular
    result = diagonal(matrix,0,i);
  }
  for (int i = 1; i < ROW && result==1; ++i)
  {
    //check lower triangular
    result = diagonal(matrix,i,0);
  }

  if(result==1)
  {
    printf("Toeplitz matrix\n");
  }
  else
  {
    printf("Not a Toeplitz matrix\n");
  }
 
}
int main(){

  int arr[ROW][COL]={

    {1,  2,  3,  4,  5},
    {6,  1,  2,  3,  4}, 
    {8,  6,  1,  2,  3},
    {7,  8,  6,  1,  2},
    {9,  7,  8,  6,  1}
  };

  is_toeplitz(arr);

  return 0;
}

Output

Toeplitz matrix
/*
  C++ Program
  Find if given matrix is Toeplitz or not
*/
#include<iostream>
#define ROW 5
#define COL 5
using namespace std;


class MyMatrix {
	public:
		int rows;
	int cols;
	MyMatrix() {
		//Get matrix size
		this->rows = ROW;
		this->cols = COL;
	}
	//Function which is check diagonal element values are similar or not
	bool diagonal(int matrix[][COL], int start_row, int start_col) {
		int status = 1;
		int element = matrix[start_row][start_col];
		for (int i = start_row + 1, j = start_col + 1; i < this->rows && j < this->cols; ++i, ++j) {
			if (matrix[i][j] != element) {
				return false;
			}
		}
		return true;
	}
	// Determine whether matrix is Toeplitz matrix or not
	void is_toeplitz(int matrix[][COL]) {
		bool result = true;
		for (int i = 0; i < this->cols && result == true; ++i) {
			//check upper triangular
			result = this->diagonal(matrix, 0, i);
		}
		for (int i = 1; i < this->rows && result == true; ++i) {
			//check lower triangular
			result = this->diagonal(matrix, i, 0);
		}
		if (result == true) {
			cout << "Toeplitz matrix\n";
		} else {
			cout << "Not a Toeplitz matrix\n";
		}
	}
};
int main() {
	int matrix[][COL] = {
		{
			1,
			2,
			3,
			4,
			5
		},
		{
			6,
			1,
			2,
			3,
			4
		},
		{
			8,
			6,
			1,
			2,
			3
		},
		{
			7,
			8,
			6,
			1,
			2
		},
		{
			9,
			7,
			8,
			6,
			1
		}
	};
	MyMatrix obj ;
	obj.is_toeplitz(matrix);
	return 0;
}

Output

Toeplitz matrix
using System;

/*
  C# Program
  Find if given matrix is Toeplitz or not
*/

public class MyMatrix {
	int rows;
	int cols;
	MyMatrix(int[,] matrix) {
		//Get matrix size
		this.rows = matrix.GetLength(0);
		this.cols = matrix.GetLength(1);
	}
	//Function which is check diagonal element values are similar or not
	public Boolean diagonal(int[,] matrix, int start_row, int start_col) {
	
		int element = matrix[start_row,start_col];
		for (int i = start_row + 1, j = start_col + 1; i < this.rows && j < this.cols; ++i, ++j) {
			if (matrix[i,j] != element) {
				return false;
			}
		}
		return true;
	}
	// Determine whether matrix is Toeplitz matrix or not
	public void is_toeplitz(int[,] matrix) {
		Boolean result = true;
		for (int i = 0; i < this.cols && result == true; ++i) {
			//check upper triangular
			result = diagonal(matrix, 0, i);
		}
		for (int i = 1; i < this.rows && result == true; ++i) {
			//check lower triangular
			result = diagonal(matrix, i, 0);
		}
		if (result == true) {
			Console.Write("Toeplitz matrix\n");
		} else {
			Console.Write("Not a Toeplitz matrix\n");
		}
	}
	public static void Main(String[] args) {
		int[,]
		//Define matrix element
		matrix = {
			{
				1,
				2,
				3,
				4,
				5
			},
			{
				6,
				1,
				2,
				3,
				4
			},
			{
				8,
				6,
				1,
				2,
				3
			},
			{
				7,
				8,
				6,
				1,
				2
			},
			{
				9,
				7,
				8,
				6,
				1
			}
		};
		MyMatrix obj = new MyMatrix(matrix);
		obj.is_toeplitz(matrix);
	}
}

Output

Toeplitz matrix
<?php
/*
  Php Program
  Find if given matrix is Toeplitz or not
*/
class MyMatrix {
	public $rows;
	public $cols;

	function __construct($matrix) {
		//Get matrix size
		$this->rows = count($matrix);
		$this->cols = count($matrix[0]);
	}
	//Function which is check diagonal element values are similar or not

	public 	function diagonal($matrix, $start_row, $start_col) {
		$element = $matrix[$start_row][$start_col];
		for ($i = $start_row + 1, $j = $start_col + 1; $i < $this->rows && $j < $this->cols; ++$i, ++$j) {
			if ($matrix[$i][$j] != $element) {
				return false;
			}
		}
		return true;
	}
	// Determine whether matrix is Toeplitz matrix or not

	public 	function is_toeplitz($matrix) {
		$result = true;
		for ($i = 0; $i < $this->cols && $result == true; ++$i) {
			//check upper triangular
			$result = $this->diagonal($matrix, 0, $i);
		}
		for ($i = 1; $i < $this->rows && $result == true; ++$i) {
			//check lower triangular
			$result = $this->diagonal($matrix, $i, 0);
		}
		if ($result == true) {
			echo("Toeplitz matrix\n");
		} else {
			echo("Not a Toeplitz matrix\n");
		}
	}
}

function main() {
	//Define matrix element
	$matrix = array(array(1, 2, 3, 4, 5), array(6, 1, 2, 3, 4), array(8, 6, 1, 2, 3), array(7, 8, 6, 1, 2), array(9, 7, 8, 6, 1));
	$obj = new MyMatrix($matrix);
	$obj->is_toeplitz($matrix);

}
main();

Output

Toeplitz matrix
/*
  Node Js Program
  Find if given matrix is Toeplitz or not
*/
class MyMatrix {
	
	constructor(matrix) {
		//Get matrix size
		this.rows = matrix.length;
		this.cols = matrix[0].length;
	}

	//Function which is check diagonal element values are similar or not
	diagonal(matrix, start_row, start_col) {
		var element = matrix[start_row][start_col];
		for (var i = start_row + 1, j = start_col + 1; i < this.rows && j < this.cols; ++i, ++j) {
			if (matrix[i][j] != element) {
				return false;
			}
		}

		return true;
	}

	// Determine whether matrix is Toeplitz matrix or not
	is_toeplitz(matrix) {
		var result = true;
		for (var i = 0; i < this.cols && result == true; ++i) {
			//check upper triangular
			result = this.diagonal(matrix, 0, i);
		}

		for (var i = 1; i < this.rows && result == true; ++i) {
			//check lower triangular
			result = this.diagonal(matrix, i, 0);
		}

		if (result == true) {
			process.stdout.write("Toeplitz matrix\n");
		} else {
			process.stdout.write("Not a Toeplitz matrix\n");
		}
	}
}

function main(args) {
	//Define matrix element
	var matrix = [
		[1, 2, 3, 4, 5],
		[6, 1, 2, 3, 4],
		[8, 6, 1, 2, 3],
		[7, 8, 6, 1, 2],
		[9, 7, 8, 6, 1]
	];
	var obj = new MyMatrix(matrix);
	obj.is_toeplitz(matrix);
}

main();

Output

Toeplitz matrix
#   Python 3 Program
#   Find if given matrix is Toeplitz or not
class MyMatrix :
	
	def __init__(self, matrix) :
		# Get matrix size
		self.rows = len(matrix)
		self.cols = len(matrix[0])
	
	# Function which is check diagonal element values are similar or not
	def diagonal(self, matrix, start_row, start_col) :
		element = matrix[start_row][start_col]
		i = start_row + 1
		j = start_col + 1
		while (i < self.rows and j < self.cols) :
			if (matrix[i][j] != element) :
				return False
			
			i += 1
			j += 1
		
		return True
	
	#  Determine whether matrix is Toeplitz matrix or not
	def is_toeplitz(self, matrix) :
		result = True
		i = 0
		while (i < self.cols and result == True) :
			# check upper triangular
			result = self.diagonal(matrix, 0, i)
			i += 1
		
		i = 1
		while (i < self.rows and result == True) :
			# check lower triangular
			result = self.diagonal(matrix, i, 0)
			i += 1
		
		if (result == True) :
			print("Toeplitz matrix\n", end = "")
		else :
			print("Not a Toeplitz matrix\n", end = "")
		
	

def main() :
	matrix = [
		[1, 2, 3, 4, 5],
		[6, 1, 2, 3, 4],
		[8, 6, 1, 2, 3],
		[7, 8, 6, 1, 2],
		[9, 7, 8, 6, 1]
	]
	obj = MyMatrix(matrix)
	obj.is_toeplitz(matrix)


if __name__ == "__main__":
	main()

Output

Toeplitz matrix
# Ruby Program
# Find if given matrix is Toeplitz or not
class MyMatrix 
	# Define the accessor and reader of class MyMatrix
    attr_reader :rows, :cols
    attr_accessor :rows, :cols
	def initialize(matrix) 
		# Get matrix size
		self.rows = matrix.length
		self.cols = matrix[0].length
	end
	# Function which is check diagonal element values are similar or not
	def diagonal(matrix, start_row, start_col) 
		element = matrix[start_row][start_col]
		i = start_row + 1
		j = start_col + 1
		while (i < self.rows && j < self.cols) 
			if (matrix[i][j] != element) 
				return false
			end
			i += 1
			j += 1
		end
		return true
	end
	#  Determine whether matrix is Toeplitz matrix or not
	def is_toeplitz(matrix) 
		result = true
		i = 0
		while (i < self.cols && result == true) 
			# check upper triangular
			result = self.diagonal(matrix, 0, i)
			i += 1
		end
		i = 1
		while (i < self.rows && result == true) 
			# check lower triangular
			result = self.diagonal(matrix, i, 0)
			i += 1
		end
		if (result == true) 
			print("Toeplitz matrix\n")
		else 
			print("Not a Toeplitz matrix\n")
		end
	end
end
def main() 
	matrix = [
		[1, 2, 3, 4, 5],
		[6, 1, 2, 3, 4],
		[8, 6, 1, 2, 3],
		[7, 8, 6, 1, 2],
		[9, 7, 8, 6, 1]
	]
	obj = MyMatrix.new(matrix)
	obj.is_toeplitz(matrix)
end


main()

Output

Toeplitz matrix
/*
  Scala Program
  Find if given matrix is Toeplitz or not
*/
class MyMatrix(var rows: Int,
		var cols: Int) {

	def this(matrix: Array[Array[Int]]) {
		//Get matrix size
		this(matrix.length,matrix(0).length);
	}
	//Function which is check diagonal element values are similar or not
	def diagonal(matrix: Array[Array[Int]], start_row: Int, start_col: Int): Boolean = {
		val element: Int = matrix(start_row)(start_col);
		var i: Int = start_row + 1;
		var j: Int = start_col + 1;
		while (i < this.rows && j < this.cols) {
			if (matrix(i)(j) != element) {
				return false;
			}
			i += 1;
			j += 1;
		}
		return true;
	}
	// Determine whether matrix is Toeplitz matrix or not
	def is_toeplitz(matrix: Array[Array[Int]]): Unit = {
		var result: Boolean = true;
		var i: Int = 0;
		while (i < this.cols && result == true) {
			//check upper triangular
			result = this.diagonal(matrix, 0, i);
			i += 1;
		}
		i = 1;
		while (i < this.rows && result == true) {
			//check lower triangular
			result = this.diagonal(matrix, i, 0);
			i += 1;
		}
		if (result == true) {
			print("Toeplitz matrix\n");
		} else {
			print("Not a Toeplitz matrix\n");
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		val matrix: Array[Array[Int]] = Array(
			Array(1, 2, 3, 4, 5),
			Array(6, 1, 2, 3, 4),
			Array(8, 6, 1, 2, 3),
			Array(7, 8, 6, 1, 2),
			Array(9, 7, 8, 6, 1));
		val obj: MyMatrix = new MyMatrix(matrix);
		obj.is_toeplitz(matrix);
	}
}

Output

Toeplitz matrix
/*
  Swift Program
  Find if given matrix is Toeplitz or not
*/
class MyMatrix {
	var rows: Int;
	var cols: Int;
	init(_ matrix: [
		[Int]
	]) {
		//Get matrix size
		self.rows = matrix.count;
		self.cols = matrix[0].count;
	}
	//Function which is check diagonal element values are similar or not
	func diagonal(_ matrix: [
		[Int]
	], _ start_row: Int, _ start_col: Int) -> Bool {
		let element: Int = matrix[start_row][start_col];
		var i: Int = start_row + 1;
		var j: Int = start_col + 1;
		while (i < self.rows && j < self.cols) {
			if (matrix[i][j] != element) {
				return false;
			}
			i += 1;
			j += 1;
		}
		return true;
	}
	// Determine whether matrix is Toeplitz matrix or not
	func is_toeplitz(_ matrix: [
		[Int]
	]) {
		var result: Bool = true;
		var i: Int = 0;
		while (i < self.cols && result == true) {
			//check upper triangular
			result = self.diagonal(matrix, 0, i);
			i += 1;
		}
		i = 1;
		while (i < self.rows && result == true) {
			//check lower triangular
			result = self.diagonal(matrix, i, 0);
			i += 1;
		}
		if (result == true) {
			print("Toeplitz matrix\n", terminator: "");
		} else {
			print("Not a Toeplitz matrix\n", terminator: "");
		}
	}
}
func main() {
	let matrix: [
		[Int]
	] = [
		[1, 2, 3, 4, 5],
		[6, 1, 2, 3, 4],
		[8, 6, 1, 2, 3],
		[7, 8, 6, 1, 2],
		[9, 7, 8, 6, 1]
	];
	let obj: MyMatrix = MyMatrix(matrix);
	obj.is_toeplitz(matrix);
}
main();

Output

Toeplitz matrix
/*
  Java Program
  Find if given matrix is Toeplitz or not
*/
public class MyMatrix {

  public int rows;
  public int cols;
  public MyMatrix(int [][]matrix)
  {
    //Get matrix size
    this.rows = matrix.length;
    this.cols = matrix[0].length;

  }
  //Function which is check diagonal element values are similar or not
  public boolean diagonal(int [][]matrix,int start_row,int start_col)
  {
    int element = matrix[start_row][start_col];
    for (int i = start_row+1,j=start_col+1; i < this.rows && j<this.cols; ++i,++j)
    {
      if(matrix[i][j]!=element)
      {
        return false;
      }
    }
   return true;
  }
  // Determine whether matrix is Toeplitz matrix or not
  public void is_toeplitz(int [][]matrix)
  {
    
    boolean result=true;

    for (int i = 0; i < this.cols && result==true; ++i)
    {
      //check upper triangular
      result=diagonal(matrix,0,i);
    }
    for (int i = 1; i < this.rows && result==true; ++i)
    {
      //check lower triangular
      result=diagonal(matrix,i,0);
    }

    if(result==true)
    {
      System.out.print("Toeplitz matrix\n");
    }
    else
    {
      System.out.print("Not a Toeplitz matrix\n");
    }
   
  }

  public static void main(String[] args) 
  {
    //Define matrix element
    int [][]matrix ={

      {1,  2,  3,  4,  5},
      {6,  1,  2,  3,  4}, 
      {8,  6,  1,  2,  3},
      {7,  8,  6,  1,  2},
      {9,  7,  8,  6,  1}
    }; 
    MyMatrix obj = new MyMatrix(matrix);
    obj.is_toeplitz(matrix);
  }
}

Output

Toeplitz matrix


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved