In the previous article, we have seen Java Program to Check Idempotent Matrix
In this article we are going to see how we can write a program to check whether matrix is Involutory Matrix or not.
Java Program to Check Involutory Matrix
A 3*3 Matrix is having 3 rows and 3 columns where this 3*3 represents the dimension of the matrix. Means there are 3*3 i.e. total 9 elements in a 3*3 Matrix.
Let’s understand it in more simpler way.
| A00 A01 A02 | Matrix A = | A10 A11 A12 | | A20 A21 A22 | 3*3
Matrix A
represents a 3*3 matrix.- ‘
A
‘ represents the matrix element - ‘
Aij
‘ represents the matrix element at it’s matrix position/index. - ‘
i
‘ represents the row index - ‘
j
‘ represents the column index - Means
A00=Aij
wherei=0
andj=0
,A01=aij
wherei=0
andj=1
and like this. - Here we have started
row
value from 0 andcolumn
value from 0.
Note: A matrix whose product of matrix is inverse to itself is to that matrix is called Involutory matrix .
Let’s see different ways to check whether matrix is Involutory matrix or not.
Method-1: Java Program to Check Involutory Matrix By Static Initialization of Array Elements
Approach:
- Declare and initialize a matrix.
- Calculate the product to itself .
- Check the product of the matrix is inverse to the entered matrix or not .
Program:
import java.util.*; public class Main { public static void main(String args[]) { Scanner s = new Scanner(System.in); // Initializing the 3X3 matrix i.e. 2D array int mat[][]={{1,0,0},{0,1,0},{0,0,1}}; int res[][]=new int[3][3];; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { res[i][j] = 0; for (int k = 0; k < 3; k++) res[i][j] += mat[i][k] * mat[k][j]; } } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (i == j && res[i][j] != 1) { System.out.println("Not a Involutory Matrix"); System.exit(0); } if (i != j && res[i][j] != 0) { System.out.println("Not a Involutory Matrix"); System.exit(0); } } } System.out.println("Involutory Matrix"); } }
Output: Involutory Matrix
Method-2: Java Program to Check Involutory Matrix By Dynamic Initialization of Array Elements
Approach:
- Take user input of a matrix.
- Calculate the product to itself .
- Check the product of the matrix is inverse to the entered matrix or not .
Program:
import java.util.*; public class Main { public static void main(String args[]) { Scanner s = new Scanner(System.in); // Initializing the 3X3 matrix i.e. 2D array int mat[][] = new int[3][3]; int row, col ; // Taking matrix input System.out.println("Enter matrix elements"); for(row=0;row<3;row++) for(col=0;col<3;col++) mat[row][col] = s.nextInt(); int res[][]=new int[3][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { res[i][j] = 0; for (int k = 0; k < 3; k++) res[i][j] += mat[i][k] * mat[k][j]; } } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (i == j && res[i][j] != 1) { System.out.println("Not a Involutory Matrix"); System.exit(0); } if (i != j && res[i][j] != 0) { System.out.println("Not a Involutory Matrix"); System.exit(0); } } } System.out.println("Involutory Matrix"); } }
Output: Enter matrix elements 1 0 0 0 1 0 0 0 1 Involutory Matrix
Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output
Related Java Programs: