Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java Program To Find the Trace and Normal of a given Matrix

In this tutorial, we will learn how to find the trace and normal of a matrix. Trace in a matrix is defined as the sum of diagonal elements and Normal is defined as the square root of the sum of squares of matrix elements. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java.

Below is the pictorial representation of how to find the trace of a matrix.

Below is the pictorial representation of how to find the normal of a matrix.

Input: Enter the Matrix Elements : 5 4 3 1 2 6 9 8 7

Output: The Trace of the Matrix is: 14.0

The Normal of the Matrix is: 16.88

Program 1: To Find the Trace and Normal of a Matrix

In this program, we will see how to find the trace and normal of a matrix when the values are user-defined.

Algorithm

  1. Start
  2. Declare variables for rows and columns.
  3. Ask the user to initialize the rows and columns.
  4. Declare a matrix.
  5. Ask the user to initialize the matrix elements.
  6. Print the original matrix.
  7. Declare two variables to calculate the trace and normal of the matrix.
  8. Initialize these variables to zero.
  9. Use two for loops to calculate the trace of the matrix.
  10. Use the first for loop to iterate through the rows.
  11. Use the second for loop to iterate through the columns.
  12. Use an if condition to check whether the row number and column number are the same or not.
  13. If the same then calculate the trace in each iteration.
  14. Print the trace value of the matrix.
  15. Now, to calculate the normal of the matrix again use two for loops.
  16. Use the first for loop to iterate through the rows.
  17. Use the second for loop to iterate through the columns.
  18. Calculate the square of each number and update the square variable in each iteration.
  19. Now, find the square root of the above-calculated square.
  20. Print the result.
  21. Stop

The below program demonstrates how to find the trace and normal of a matrix.

/*JAVA PROGRAM TO FIND THE TRACE AND NORMAL OF A MATRIX*/
import java.util.*;
public class Main
{
     public static void main(String []args)
     {
         ///Take input from the user
         Scanner sc=new Scanner(System.in);        
         int m,n;                 //Matrix Row and Column Declaration        
         System.out.println("Enter the number of rows: \n");
         m=sc.nextInt();  //Matrix Row Initialization        
         System.out.println("Enter the number of column: \n");
         n=sc.nextInt();  //Matrix Column Initialization        
         int arr[][]=new int[10][10];        //Matrix Size Declaration        
         System.out.println("Enter the elements of the matrix: ");
         for(int i=0;i<m;i++)    //Matrix Elements Initialization
         {
            for(int j=0;j<n;j++)
            {
                 arr[i][j]=sc.nextInt();
            }
         }        
         //Print the original Matrix
         System.out.println("The elements in the original matrix are: ");
         for(int i=0;i<m;i++)     
         {
             for(int j=0;j<n;j++)
             {
                  System.out.print(arr[i][j]+" "); //Print the matrix elements
             }
            System.out.println("");
        }       
        double sum=0;        //Declare and initialize the trace variable
        double square=0;     //Declare and initialize the normal variable       
        //Find the trace of the matrix
        System.out.println("The Trace of the above matrix is ");
  	    for(int i = 0; i < m; i++)
  	    {  
    	    for(int j = 0; j < n; j++)
       	    {
                if(i == j)
            	 {
               	     sum = sum + (arr[i][j]);      //Calculate the trace in each iteration
               	 }
            }
        }
        System.out.println(sum);  //Print the trace of the matrix       
        //Find the normal of the matrix
        System.out.println("The Normal of the above matrix is "); 
   	    for(int i = 0; i < m; i++)
   	    {
    	    for(int j = 0; j < n; j++)
       	    {
       	        square = square + (arr[i][j])*(arr[i][j]);     //Calculate the normal in each iteration
            }
    	}
        double result = Math.sqrt(square);
        System.out.println(result);     //Print the normal       
     }
}


Enter the number of rows: 3
Enter the number of columns: 3
Enter the elements of the matrix: 1 2 3 4 5 6 7 8 9
The elements in the original matrix are:
1 2 3
4 5 6
7 8 9
The Trace of the above matrix is
15.0
The Normal of the above matrix is
16.881943016134134

Program 2: To Find the Trace and Normal of a Matrix

In this program, we will see how to find the trace and normal of a matrix when the values are pre-defined.

Algorithm

  1. Start
  2. Declare and initialize a matrix.
  3. Print the original matrix.
  4. Call a method to calculate the trace of the matrix.
  5. Declare a variable sum in that method and initialize it to 0.
  6. Increment the sum when a diagonal value encounters.
  7. Display the sum.
  8. Now, call a method to calculate the normal of the matrix.
  9. Declare a variable square and initialize it to 0.
  10. Calculate the square of each number and update the square variable in each iteration.
  11. Now, find the square root of the above-calculated square.
  12. Print the result.
  13. Stop

The below program demonstrates how to find the trace and normal of a matrix.

/*Java Program to find the trace and normal of a matrix*/
import java.io.*; 
public class Main 
{   
    //To Find the normal of a matrix 
    public static void findNormal(int[][] arr) 
    { 
         double square = 0, result = 0;
        System.out.println("The Normal of the above matrix is "); 
   	for(int i = 0; i < arr.length; i++)
   	{
    	    for(int j = 0; j < arr[0].length; j++)
       	    {
       	        square = square + (arr[i][j])*(arr[i][j]);
            }
    	}
        result = Math.sqrt(square);
        System.out.println(result);
    }     
    //To Find the trace of a matrix 
    public static void findTrace(int[][] arr) 
    { 
        double sum = 0;
        System.out.println("The Trace of the above matrix is ");
  	for(int i = 0; i < arr.length; i++)
  	{  
    	    for(int j = 0; j < arr[0].length; j++)
       	    {
                if(i == j)
            	 {
               	     sum = sum + (arr[i][j]);
               	 }
            }
        }
        System.out.println(sum);          
    }    
    // Driver code 
    public static void main(String args[]) throws IOException 
    { 
        int arr[][] 
            = { { 2, 9, 8 }, { 7, 6, 4 }, { 3, 9, 2 } };  //Matrix Declaration and Initialization
            System.out.println("Original Matrix");
       for(int i = 0; i < arr.length; i++)
  	   {  
    	    for(int j = 0; j < arr[0].length; j++)
       	    {
                System.out.print(arr[i][j]+ " ");
            }
            System.out.println();
        }
        System.out.println();
        findTrace(arr);    //Find the Trace of the Matrix
        System.out.println();
        findNormal(arr);   //Find the Normal of the Matrix                  
    } 
} 


Original Matrix
2 9 8
7 6 4
3 9 2

The Trace of the above matrix is
10.0

The Normal of the above matrix is
18.547236990991408