Program to check idempotent matrix

Here given code implementation process.

/*
  C Program 
+ Program to check idempotent matrix
*/
#include <stdio.h>

#define ROW 3
#define COL 3

//Display the element of given 2d matrix
void show_data(int matrix[][COL]) {
  
  printf("-----------------\n");
  
  for (int i = 0; i < ROW; ++i) {

    for (int j = 0; j < COL; ++j) {

      printf("%4d", matrix[i][j]);
    }

    printf("\n");
  }

  printf("\n");
}
void is_idempotent(int matrix[][COL])
{

  //This matrix are store the result of multiplication 
  int result[ROW][COL];

  for (int i = 0; i < ROW; ++i) {

    for (int j = 0; j < COL; ++j) {

      //Set the initial value of new matrix element
      result[i][j] = 0;

      for (int k = 0; k < ROW; ++k) {

        //Multiply matrix A [i] row and [k] columns to the Matrix B [k] columns and [j] rows
        result[i][j] += matrix[i][k] * matrix[k][j];
      }
    }
  }
  //Display Matrix Elements
  printf("  Matrix \n");
  //print element of matrix
  show_data(matrix);
  

  for (int i = 0; i < ROW; ++i) {

    for (int j = 0; j < COL; ++j) 
    {
      //Compare two matrix element    
      if(matrix[i][j]!=result[i][j])
      {

        printf("Matrix is not idempotent\n");
        return;
      }
    }
  }
  printf("Matrix is idempotent\n");
}
int main() {
  //Define matrix 
  int matrix[ROW][COL] = {
    {
      3, -6, 0
    },

    {
      1, -2, 0
    },
    {
      0,  0, 1
    }
  };
  is_idempotent(matrix);
  
  return 0;
}

Output

  Matrix
-----------------
   3  -6   0
   1  -2   0
   0   0   1

Matrix is idempotent
/*
  C++ Program
  Program to check idempotent matrix
*/
#include<iostream>
#define ROW 3
#define COL 3
using namespace std;


class MyMatrix {
	public:

		//Display the element of given 2d matrix
		void show_data(int matrix[][COL]) {
		
			cout << "-----------------\n";
			for (int i = 0; i < ROW; ++i) {
				for (int j = 0; j < COL; ++j) {
					cout << " " << matrix[i][j];
				}
				cout << "\n";
			}
			cout << "\n";
		}
	void is_idempotent(int matrix[][COL]) {
		
		int result[ROW][COL];
		for (int i = 0; i < ROW; ++i) {
			for (int j = 0; j < COL; ++j) {
				//Set the initial value of new matrix element
				result[i][j] = 0;
				for (int k = 0; k < ROW; ++k) {
					//Multiply matrix A [i] row and [k] columns to the Matrix B [k] columns and [j] rows
					result[i][j] += matrix[i][k] *matrix[k][j];
				}
			}
		}
		//Display Matrix Elements

		cout << " Matrix \n";
		//print element of matrix
		this->show_data(matrix);
		for (int i = 0; i < ROW; ++i) {
			for (int j = 0; j < COL; ++j) {
				//Compare two matrix element    

				if (matrix[i][j] != result[i][j]) {
					cout << "Matrix is not idempotent\n";
					return;
				}
			}
		}
		cout << "Matrix is idempotent\n";
	}
};
int main() {
	MyMatrix obj ;
	int matrix[][COL] = {
		{
			3,
			-6,
			0
		},
		{
			1,
			-2,
			0
		},
		{
			0,
			0,
			1
		}
	};
	obj.is_idempotent(matrix);
	return 0;
}

Output

 Matrix
-----------------
 3 -6 0
 1 -2 0
 0 0 1

Matrix is idempotent
/*
  Java Program
  Program to check idempotent matrix
*/

public class MyMatrix {

  //Display the element of given 2d matrix
  public void show_data(int [][] matrix) {
    int row = matrix.length;
    int col = matrix[0].length; 
    System.out.print("-----------------\n");
    
    for (int i = 0; i < row; ++i) {

      for (int j = 0; j < col; ++j) {

        System.out.print("  "+ matrix[i][j]);
      }

      System.out.print("\n");
    }

    System.out.print("\n");
  }
  public void is_idempotent(int [][] matrix)
  {
    int row = matrix.length;
    int col = matrix[0].length; 
    //This matrix are store the result of multiplication 
    int [][]result= new int[row][col];

    for (int i = 0; i < row; ++i) {

      for (int j = 0; j < col; ++j) {

        //Set the initial value of new matrix element
        result[i][j] = 0;

        for (int k = 0; k < row; ++k) {

          //Multiply matrix A [i] row and [k] columns to the Matrix B [k] columns and [j] rows
          result[i][j] += matrix[i][k] * matrix[k][j];
        }
      }
    }
    //Display Matrix Elements
    System.out.print("  Matrix \n");
    //print element of matrix
    show_data(matrix);
    

    for (int i = 0; i < row; ++i) {

      for (int j = 0; j < col; ++j) 
      {
        //Compare two matrix element    
        if(matrix[i][j]!=result[i][j])
        {

          System.out.print("Matrix is not idempotent\n");
          return;
        }
      }
    }
    System.out.print("Matrix is idempotent\n");
  }
  public static void main(String[] args) {
    
    MyMatrix obj = new MyMatrix();
    
    int[][] matrix = {
      {
        3, -6, 0
      },

      {
        1, -2, 0
      },
      {
        0,  0, 1
      }
    };
    
  
    obj.is_idempotent(matrix);
   
  }
}

Output

 Matrix
-----------------
 3 -6 0
 1 -2 0
 0 0 1

Matrix is idempotent
using System;

/*
  C# Program
  Program to check idempotent matrix
*/

public class MyMatrix {
	//Display the element of given 2d matrix
	public void show_data(int[,] matrix) {
		int row = matrix.GetLength(0);
		int col = matrix.GetLength(1);
		Console.Write("-----------------\n");
		for (int i = 0; i < row; ++i) {
			for (int j = 0; j < col; ++j) {
				Console.Write(" " + matrix[i,j]);
			}
			Console.Write("\n");
		}
		Console.Write("\n");
	}
	public void is_idempotent(int[,] matrix) {
		int row = matrix.GetLength(0);
		int col = matrix.GetLength(1);
		
		//This matrix are store the result of multiplication 
		int[,] result = new int[row,col];
		for (int i = 0; i < row; ++i) {
			for (int j = 0; j < col; ++j) {
				//Set the initial value of new matrix element
				result[i,j] = 0;
				for (int k = 0; k < row; ++k) {
					//Multiply matrix A [i] row and [k] columns to the Matrix B [k] columns and [j] rows
					result[i,j] += matrix[i,k] * matrix[k,j];
				}
			}
		}
		Console.Write(" Matrix \n");
		show_data(matrix);
		for (int i = 0; i < row; ++i) {
			for (int j = 0; j < col; ++j) {
				//Compare two matrix element    

				if (matrix[i,j] != result[i,j]) {
					Console.Write("Matrix is not idempotent\n");
					return;
				}
			}
		}
		Console.Write("Matrix is idempotent\n");
	}
	public static void Main(String[] args) {
		MyMatrix obj = new MyMatrix();
		int[,] matrix = {
			{
				3,
				-6,
				0
			},
			{
				1,
				-2,
				0
			},
			{
				0,
				0,
				1
			}
		};
		obj.is_idempotent(matrix);
	}
}

Output

 Matrix
-----------------
 3 -6 0
 1 -2 0
 0 0 1

Matrix is idempotent
<?php
/*
  Php Program
  Program to check idempotent matrix
*/
class MyMatrix {
	//Display the element of given 2d matrix

	public 	function show_data($matrix) {
		$row = count($matrix);
		$col = count($matrix[0]);
		echo("-----------------\n");
		for ($i = 0; $i < $row; ++$i) {
			for ($j = 0; $j < $col; ++$j) {
				echo(" ". $matrix[$i][$j]);
			}
			echo("\n");
		}
		echo("\n");
	}
	public 	function is_idempotent($matrix) {
		$row = count($matrix);
		$col = count($matrix[0]);
		//This matrix are store the result of multiplication 
		$result = array_fill(0, $row, array_fill(0, $col, 0));
		for ($i = 0; $i < $row; ++$i) {
			for ($j = 0; $j < $col; ++$j) {
				//Set the initial value of new matrix element
				$result[$i][$j] = 0;
				for ($k = 0; $k < $row; ++$k) {
					//Multiply matrix A [i] row and [k] columns to the Matrix B [k] columns and [j] rows
					$result[$i][$j] += $matrix[$i][$k] *$matrix[$k][$j];
				}
			}
		}
		//Display Matrix Elements

		echo(" Matrix \n");
		//print element of matrix
		$this->show_data($matrix);
		for ($i = 0; $i < $row; ++$i) {
			for ($j = 0; $j < $col; ++$j) {
				//Compare two matrix element    

				if ($matrix[$i][$j] != $result[$i][$j]) {
					echo("Matrix is not idempotent\n");
					return;
				}
			}
		}
		echo("Matrix is idempotent\n");
	}
}

function main() {
	$obj = new MyMatrix();
	$matrix = array(array(3, -6, 0), array(1, -2, 0), array(0, 0, 1));
	$obj->is_idempotent($matrix);

}
main();

Output

 Matrix
-----------------
 3 -6 0
 1 -2 0
 0 0 1

Matrix is idempotent
/*
  Node Js Program
  Program to check idempotent matrix
*/
class MyMatrix {
	//Display the element of given 2d matrix
	show_data(matrix) {
		var row = matrix.length;
		var col = matrix[0].length;
		process.stdout.write("-----------------\n");
		for (var i = 0; i < row; ++i) {
			for (var j = 0; j < col; ++j) {
				process.stdout.write(" " + matrix[i][j]);
			}

			process.stdout.write("\n");
		}

		process.stdout.write("\n");
	}
	is_idempotent(matrix) {
		var row = matrix.length;
		var col = matrix[0].length;
		//This matrix are store the result of multiplication 
		var result = Array(row).fill(0).map(() => new Array(col).fill(0));
		for (var i = 0; i < row; ++i) {
			for (var j = 0; j < col; ++j) {
				//Set the initial value of new matrix element
				result[i][j] = 0;
				for (var k = 0; k < row; ++k) {
					//Multiply matrix A [i] row and [k] columns to the Matrix B [k] columns and [j] rows
					result[i][j] += matrix[i][k] *matrix[k][j];
				}
			}
		}

		//Display Matrix Elements

		process.stdout.write(" Matrix \n");
		//print element of matrix
		this.show_data(matrix);
		for (var i = 0; i < row; ++i) {
			for (var j = 0; j < col; ++j) {
				//Compare two matrix element    

				if (matrix[i][j] != result[i][j]) {
					process.stdout.write("Matrix is not idempotent\n");
					return;
				}
			}
		}

		process.stdout.write("Matrix is idempotent\n");
	}
}

function main(args) {
	var obj = new MyMatrix();
	var matrix = [
		[3, -6, 0],
		[1, -2, 0],
		[0, 0, 1]
	];
	obj.is_idempotent(matrix);
}

main();

Output

 Matrix
-----------------
 3 -6 0
 1 -2 0
 0 0 1

Matrix is idempotent
# Python 3 Program
# Program to check idempotent matrix
class MyMatrix :
	# Display the element of given 2d matrix
	def show_data(self, matrix) :
		row = len(matrix)
		col = len(matrix[0])
		print("-----------------\n", end = "")
		i = 0
		j = 0
		while (i < row) :
			j = 0
			while (j < col) :
				print(" ", matrix[i][j], end = "")
				j += 1
			
			print("\n", end = "")
			i += 1
		
		print("\n", end = "")
	
	def is_idempotent(self, matrix) :
		row = len(matrix)
		col = len(matrix[0])
		result = [
			[0] * col
			for _ in range(row)
		]
		i = 0
		j = 0
		k = 0
		while (i < row) :
			j = 0
			while (j < col) :
				# Set the initial value of new matrix element
				result[i][j] = 0
				k = 0
				while (k < row) :
					# Multiply matrix A [i] row and [k] columns to the Matrix B [k] columns and [j] rows
					result[i][j] += matrix[i][k] * matrix[k][j]
					k += 1
				
				j += 1
			
			i += 1
		
		print(" Matrix \n", end = "")
		self.show_data(matrix)
		i = 0
		while (i < row) :
			j = 0
			while (j < col) :
				# Compare two matrix element    

				if (matrix[i][j] != result[i][j]) :
					print("Matrix is not idempotent\n", end = "")
					return
				
				j += 1
			
			i += 1
		
		print("Matrix is idempotent\n", end = "")
	

def main() :
	obj = MyMatrix()
	matrix = [
		[3, -6, 0],
		[1, -2, 0],
		[0, 0, 1]
	]
	obj.is_idempotent(matrix)


if __name__ == "__main__":
	main()

Output

 Matrix
-----------------
  3  -6  0
  1  -2  0
  0  0  1

Matrix is idempotent
# Ruby Program
# Program to check idempotent matrix
class MyMatrix 
	# Display the element of given 2d matrix
	def show_data(matrix) 
		row = matrix.length
		col = matrix[0].length
		print("-----------------\n")
		i = 0
		j = 0
		while (i < row) 
			j = 0
			while (j < col) 
				print(" ", matrix[i][j])
				j += 1
			end
			print("\n")
			i += 1
		end
		print("\n")
	end
	def is_idempotent(matrix) 
		row = matrix.length
		col = matrix[0].length
		result = Array.new(row) { Array.new(col, 0) }
		i = 0
		j = 0
		k = 0
		while (i < row) 
			j = 0
			while (j < col) 
				# Set the initial value of new matrix element
				result[i][j] = 0
				k = 0
				while (k < row) 
					# Multiply matrix A [i] row and [k] columns to the Matrix B [k] columns and [j] rows
					result[i][j] += matrix[i][k] * matrix[k][j]
					k += 1
				end
				j += 1
			end
			i += 1
		end
		print(" Matrix \n")
		self.show_data(matrix)
		i = 0
		while (i < row) 
			j = 0
			while (j < col) 
				# Compare two matrix element    

				if (matrix[i][j] != result[i][j]) 
					print("Matrix is not idempotent\n")
					return
				end
				j += 1
			end
			i += 1
		end
		print("Matrix is idempotent\n")
	end
end
def main() 
	obj = MyMatrix.new()
	matrix = [
		[3, -6, 0],
		[1, -2, 0],
		[0, 0, 1]
	]
	obj.is_idempotent(matrix)
end
main()

Output

 Matrix 
-----------------
 3 -6 0
 1 -2 0
 0 0 1

Matrix is idempotent
/*
  Scala Program
  Program to check idempotent matrix
*/
class MyMatrix {
	//Display the element of given 2d matrix
	def show_data(matrix: Array[Array[Int]]): Unit = {
		val row: Int = matrix.length;
		val col: Int = matrix(0).length;
		print("-----------------\n");
		var i: Int = 0;
		var j: Int = 0;
		while (i < row) {
			j = 0;
			while (j < col) {
				print(" " + matrix(i)(j));
				j += 1;
			}
			print("\n");
			i += 1;
		}
		print("\n");
	}
	def is_idempotent(matrix: Array[Array[Int]]): Unit = {
		val row: Int = matrix.length;
		val col: Int = matrix(0).length;
		var result: Array[Array[Int]] = Array.fill[Int](row, col)(0);
		var i: Int = 0;
		var j: Int = 0;
		var k: Int = 0;
		while (i < row) {
			j = 0;
			while (j < col) {
				//Set the initial value of new matrix element
				result(i)(j) = 0;
				k = 0;
				while (k < row) {
					//Multiply matrix A [i] row and [k] columns to the Matrix B [k] columns and [j] rows
					result(i)(j) += matrix(i)(k) * matrix(k)(j);
					k += 1;
				}
				j += 1;
			}
			i += 1;
		}
		print(" Matrix \n");
		this.show_data(matrix);
		i = 0;
		while (i < row) {
			j = 0;
			while (j < col) {
				//Compare two matrix element    

				if (matrix(i)(j) != result(i)(j)) {
					print("Matrix is not idempotent\n");

					return;
				}
				j += 1;
			}
			i += 1;
		}
		print("Matrix is idempotent\n");
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		val obj: MyMatrix = new MyMatrix();
		val matrix: Array[Array[Int]] = Array(
			Array(3, -6, 0),
			Array(1, -2, 0),
			Array(0, 0, 1));
		obj.is_idempotent(matrix);
	}
}

Output

 Matrix
-----------------
 3 -6 0
 1 -2 0
 0 0 1

Matrix is idempotent
/*
  Swift Program
  Program to check idempotent matrix
*/
class MyMatrix {
	//Display the element of given 2d matrix
	func show_data(_ matrix: [
		[Int]
	]) {
		let row: Int = matrix.count;
		let col: Int = matrix[0].count;
		print("-----------------\n", terminator: "");
		var i: Int = 0;
		var j: Int = 0;
		while (i < row) {
			j = 0;
			while (j < col) {
				print(" ", matrix[i][j], terminator: "");
				j += 1;
			}
			print("\n", terminator: "");
			i += 1;
		}
		print("\n", terminator: "");
	}
	func is_idempotent(_ matrix: [
		[Int]
	]) {
		let row: Int = matrix.count;
		let col: Int = matrix[0].count;
		var result: [
			[Int]
		] = Array(repeating: Array(repeating: 0, count: col), count: row);
		var i: Int = 0;
		var j: Int = 0;
		var k: Int = 0;
		while (i < row) {
			j = 0;
			while (j < col) {
				//Set the initial value of new matrix element
				result[i][j] = 0;
				k = 0;
				while (k < row) {
					//Multiply matrix A [i] row and [k] columns to the Matrix B [k] columns and [j] rows
					result[i][j] += matrix[i][k] * matrix[k][j];
					k += 1;
				}
				j += 1;
			}
			i += 1;
		}
		print(" Matrix \n", terminator: "");
		self.show_data(matrix);
		i = 0;
		while (i < row) {
			j = 0;
			while (j < col) {
				//Compare two matrix element    

				if (matrix[i][j] != result[i][j]) {
					print("Matrix is not idempotent\n", terminator: "");
					return;
				}
				j += 1;
			}
			i += 1;
		}
		print("Matrix is idempotent\n", terminator: "");
	}
}
func main() {
	let obj: MyMatrix = MyMatrix();
	let matrix: [
		[Int]
	] = [
		[3, -6, 0],
		[1, -2, 0],
		[0, 0, 1]
	];
	obj.is_idempotent(matrix);
}
main();

Output

 Matrix
-----------------
  3  -6  0
  1  -2  0
  0  0  1

Matrix is idempotent


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