Question
Program to check whether the matrix is an orthogonal matrix
(To determine if a matrix is orthogonal, we need to multiply the matrix by it’s transpose, and see if we get the identity matrix.)
Matrix:
[1,0,0]
[0,1,0]
[0,0,1]
TRANSPOSE:
[1,0,0]
[0,1,0]
[0,0,1]
PRODUCT OF TRANSPOSE MATRIX AND ORIGINAL MATRIX:
[1,0,0]
[0,1,0]
[0,0,1]
MATRIX IS ORTHOGONAL MATRIX
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class OrthogonalMatrix
{
public static void main(String[] args)
{
int row=0, col=0,i=0,j=0,k=0,sum=0;
int a[][],t[][],product[][];
boolean flag=true;
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE ROW OF MATRIX");
row=sc.nextInt();
System.out.println("ENTER THE COLUMN OF MATRIX");
col=sc.nextInt();
if(row!=col)
{
System.out.println("MATRIX FIRST NEED TO BE SQUARE MATRIX");
}
else
{
a=new int[row][col];
t=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
a[i][j]=sc.nextInt();
}
}
/*transpose of matrix starts here*/
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
t[i][j]=a[j][i];
}
}
/*transpose of matrix ends here*/
/*printing of transpose matrix starts here*/
System.out.println("Transpose of a matrix");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
System.out.print(t[i][j]+" ");
}
System.out.println();
}
/*printing of transpose matrix ends here*/
/*product of original matrix and transpose matrix starts here*/
product=new int[row][col];
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
sum = 0;
for (k = 0; k < row; k++)
{
sum = sum + (a[i][k] * t[k][j]);
}
product[i][j] = sum;
}
}
/*product of original matrix and transpose matrix ends here*/
System.out.println("Product of original matrix and transpose of original matrix matrix");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
System.out.print(product[i][j]+" ");
}
System.out.println();
}
/*checking whether the product is identity matrix.In identity matrix, all elements except on principal diagonal are zero and elements on principal diagonal are 1*/
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
if((i==j) &&product[i][j]!=1)
{
flag=false;
break;
}
if(i!=j && product[i][j]!=0)
{
flag=false;
break;
}
}
}
if(flag==false)
{
System.out.println("MATRIX IS NOT ORTHOGONAL MATRIX");
}
else
{
System.out.println("MATRIX IS AN ORTHOGONAL MATRIX");
}
}
}
}