
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Check if a Matrix is Involutory or not in Java?
Matrices are nothing but a collection of data elements arranged in a rectangular layout that is two-dimensional. In Java, an array with two dimensions can be considered as a matrix.
Involutory matrix is a square matrix which when multiplied by itself, gives back an identity matrix. Identity matrix is a matrix in which all elements are zero except the diagonals which are one.
Let’s deep dive into this article, to know how it can be done by using Java programming language.
To show you some instances
Instance-1
Suppose we have a matrix | 1 0 0 | A = | 0 1 0 | | 0 0 1 | A2 = A X A | 1 0 0 | | 1 0 0 | = | 0 1 0 | X | 0 1 0 | | 0 0 1 | | 0 0 1 | | 1 0 0 |
A2 = | 0 1 0 | | 0 0 1 |
It is an involutory matrix.
Instance-2
Suppose we have a matrix
Suppose we have a matrix | 1 0 0 | A = | 0 -1 0 | | 0 0 -1 | A2 = A X A | 1 0 0 | | 1 0 0 | = | 0 -1 0 | X | 0 -1 0| | 0 0 -1 | | 0 0 -1| | 1 0 0 | A2 = | 0 1 0 | | 0 0 1 |
It is an involutory matrix.
Algorithm
Step 1 − Initialise and declare the matrix
Step 2 − Multiply the matrix with itself and store the resultant
Step 3 − Compare the product matrix with an identity matrix and check if both matrices are the same or not
Step 4 − If both the matrices are the same then the matrix is an involutory matrix
Multiple Approaches
We have provided the solution in different approaches.
By Using Static Initialization of Matrix
By Using Dynamic Initialization of Matrix
Let’s see the program along with its output one by one.
Approach-1: By Using Static Initialization of Matrix
In this approach, matrix elements will be initialized in the program. Then as per the algorithm check if the matrix is an involutory matrix or not.
Example
import java.util.Scanner; public class Main { public static void main(String[] args) { // Matrix to be checked int mat[][] = {{ 1, 0, 0 },{ 0, -1, 0 },{ 0, 0, -1 },}; // Print results if (invoCheck(mat)) System.out.println("The matrix is an involutory matrix."); else System.out.println("The matrix is not an involutory matrix."); } // Matrix multiplication static void mul(int mat[][], int prod[][]) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { prod[i][j] = 0; // Resultant product is stored in prod for (int k = 0; k < 3; k++) { prod[i][j] += mat[i][k] * mat[k][j]; } } } } // Check if the matrix is involutory static boolean invoCheck(int mat[][]) { // 3X3 Identity Matrix int identityMat[][] = { { 1, 0, 0 },{ 0, 1, 0 },{ 0, 0, 1 } }; int prod[][] = new int[3][3]; // Calls the matrix multiplication mul(mat, prod); // Checks if the product matrix is an identity matrix for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (identityMat[i][j] != prod[i][j]) return false; } } return true; } }
Output
The matrix is an involutory matrix.
Approach-2: By Using Dynamic Initialization of Matrix
In this approach, matrix elements will be taken as user input in the program. Then as per the algorithm check if the matrix is an involutory matrix or not.
Example
import java.util.Scanner; public class Main { public static void main(String[] args) { //Matrix to be checked int mat[][] = new int[3][3]; //Take matrix as user input Scanner sc = new Scanner(System.in); System.out.println("Enter matrix elements:-"); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { mat[i][j] = sc.nextInt(); }} // Print results if (invoCheck(mat)) System.out.println("The matrix is an involutory matrix."); else System.out.println("The matrix is not an involutory matrix."); } // Matrix multiplication static void mul(int mat[][], int prod[][]) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { prod[i][j] = 0; // Resultant product is stored in prod for (int k = 0; k < 3; k++) { prod[i][j] += mat[i][k] * mat[k][j]; } } } } // Check if the matrix is involutory static boolean invoCheck(int mat[][]) { // 3X3 Identity Matrix int identityMat[][] = { { 1, 0, 0 },{ 0, 1, 0 },{ 0, 0, 1 } }; int prod[][] = new int[3][3]; // Calls the matrix multiplication mul(mat, prod); // Checks if the product matrix is an identity matrix for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (identityMat[i][j] != prod[i][j]) return false; } } return true; } }
Output
Enter matrix elements:- 1 0 0 0 1 0 0 0 1 The matrix is an involutory matrix
In this article, we explored different approaches to check if the matrix is an involutory matrix or not by using Java programming language.
- Related Articles
- Check if a Matrix is Identity Matrix or not in Java?
- Check if a Matrix is Markov Matrix or not in Java
- Program to check if a matrix is Binary matrix or not in C++
- Program to check Involutory Matrix in C++
- Check if a given matrix is sparse or not in C++
- Check if a given matrix is Hankel or not in C++
- Python Program to check Involutory Matrix
- How to check if a matrix is invertible or not in R?
- C Program to check if matrix is singular or not
- JavaScript program to check if a given matrix is sparse or not
- Java Program to check if a string is empty or not
- Check if a large number is divisible by 11 or not in java
- Check if a large number is divisible by 3 or not in java
- Check given matrix is magic square or not in C++
- Program to check whether given matrix is Toeplitz Matrix or not in Python
