×

Normal and Trace of a Matrix in Java

Normal of a matrix

The square root of the total squares of each element in a matrix is known as the matrix's normal. Think of the following matrix as an example.

Example:

[[1,2,3]

  [4,5,6]

  [7,8,9]]

sumofSqures=1*1+2*2+3*3+4*4+5*5+6*6+7*7+8*8+9*9=

sumofSqures=385

Now find the square root of the number of sumofSqures.

Normal=sqrt(385)

Normal=16.8

A Matrix trace

All the components that make up a matrix's major diagonal are added to form the trace (upper left to lower right). You should be aware that matrices must be square (Matrix should consist of an equal number of rows and columns). Solutions in linear algebra are useful to prove.

[[1,2,3]

  [4,5,6]

  [7,8,9]]

TraceofMatrix=1+5+9

TraceofMatrix=15

Java Program for normal and a trace of the matrix

NormalandTrace.java

//this program is for finding the normal and the trace of the matrix
//importing the packages required
import java.util.*;  
public class NormalandTrace  
{  
    public static void main(String args[])  
    {  
        int arr[][] = new int[5][5];  
        int i, j;  
        double total = 0, sq = 0, res = 0;  
    Scanner sc = new Scanner(System.in);  
    System.out.print("Please enter the num of rows: ");  
    // reading the integer value as rows from the user 
    int r= sc.nextInt();  
    System.out.print("Please enter the num of columns: ");  
    // reading the integer value as columns from the user
    int c = sc.nextInt();  
    System.out.println("Enter matrix:");  
    //loop for iterating the rows 
    for(i = 0; i < r; i++)  
    {  
        //loop for iterating columns  
        for(j = 0; j < c; j++)   
            {  
                //reading the elements in the matrix   
                arr[i][j] = sc.nextInt();  
                // the space is printed  
                System.out.print(" ");  
            }  
        }  
    System.out.println("Displaying the entered matrix: ");  
    // displaying the entered integers as the matrix
    for(i = 0; i < r; i++)  
        {  
            for(j = 0; j < c; j++)  
            {  
            System.out.print(arr[i][j]+" ");  
            }  
            System.out.println(" ");  
        }  
        System.out.println("Displaying the trace of the matrix:");
    for(i = 0; i < r; i++)  
    {    
            for(j = 0; j < c; j++)  
            {  
                //the condition for checking the element has i==j 
                if(i == j)  
                 {  
                     // the sum of the elements of the principal diagonal  
                     total = total + (arr[i][j]);  
                 }  
            }  
        }  
        //printing the trace of the matrix  
        System.out.println(total);    
        System.out.println("The result of the normal is: ");   
          
    for(i = 0; i < r; i++)  
    {  
            for(j = 0; j < c; j++)  
            {  
                //th sum of the squares of the elements in the matrix   
                sq = sq + (arr[i][j])*(arr[i][j]);  
            }  
        }  
        // finding the square root of the number(sq) 
        res = Math.sqrt(sq);  
        // printing the result as the normal of the matrix 
        System.out.println(res);  
    }  
}  

Output

Please enter the num of rows: 3
Please enter the num of columns: 3
Enter matrix:
1 
2
 3
 4
 5
 6
 7
 8
 9
 Displaying the entered matrix: 
1 2 3  
4 5 6  
7 8 9  
Displaying the trace of the matrix:
15.0
The result of the normal is: 
16.881943016134134

NormalTrace2.java

//this program is for finding the normal and the trace of the matrix
//importing the packages required
import java.io.*;  
public class NormalTrace2  
{  
// declaring the maximum size of the matrix
static int MAX = 10;  
// the method can calculate and return the normal of the matrix 
static int findNormal(int m[][], int num)  
{  
    //storing the sumofsquares in the total variable
    int total = 0;  
    for (int i=0; i<num; i++)  
        for (int j=0; j<num; j++)  
            total = total + m[i][j]*m[i][j];  
    return (int)Math.sqrt(total);  
}  
// method for finding the trace of the matrix
static int findTrace(int m[][], int num)  
{  
    //storing the result in total variable
    int total = 0;  
    for (int i=0; i<num; i++)  
        total = total + m[i][i];  
    return total;  
}  
//main section of the program  
public static void main (String args[])   
    {  
        // static way of representing elements in the matrix
        int m[][] = {{5, 3, 24, 1, 45},  
                          {27, 8, 33, 15, 9},  
                          {1, 11, 10, 5, 5},  
                          {45, 18, 3, 9, 14},  
                          {19, 3, 3, 13, 4},  
                         };  
        //printing the normal and trace of the matrix m[][]
        
        System.out.println ("The Trace for the given matrix is: "+ findTrace(m, 5));  
        System.out.println ("The result of Normal is: "+ findNormal(m, 5));  
    }  
}  

Output

The Trace for the given matrix is: 36
The result of Normal is: 91

Related Topics

How to Calculate Week Number From Current Date in Java?

The WeekFields class's weekOfMonth() method is utilized to return the field for access the week of a month based on this WeekFields. If the first day of the month is a...

3 minutes read.

Java file Reader

File Reader: It is used to read the data from files. This class inherits the properties from Input Stream Reader Class. File Reader is for reading characters from the file. Input Stream: Java.io...

4 minutes read.

How to Split the String in Java with Delimiter

In Java, splitting strings is a significant and typically used activity while coding. Java gives different ways of dividing the String. The most widely recognized way is to use the...

3 minutes read.

Java time local date

Java: Java is one of the programming language which is object oriented. It consists of many features such as robust, simple, architecture neutral, dynamic, distributed, multi threaded, portable etc. The main feature...

3 minutes read.

Java Constant

A constant is an unchangeable entity in coding, as its title implies. The value which cannot be altered, in other terms. We shall understand about Java constants and exactly how...

3 minutes read.

Implementing Queue Using Array in Java

We can implement queue by using array in Java. The queue is a linear data structure, and the array is one of the simplest data structures. Before implementing a queue...

4 minutes read.

Java Enumeration

In a computer language, enumerations express a set of named constants. For instance, the four suits in a deck of playing cards could be represented by the enumerators Club, Diamond,...

4 minutes read.

Sleep Time in Java

Sleep is amethod in java that is related to thread class and it is a concept of multithreading and is used to stop the execution of the current thread a...

4 minutes read.

Get year from date in Java

The getYear() method of the Java date class returns a number calculated by deducting 1900 from the year that contains or starts with the instant in time represented by this...

4 minutes read.

Synchronized Keyword in Java

Synchronization is the process of limiting access to a shared resource or data to a single thread at a given moment in time. This aids in shielding the data from...

6 minutes read.

How to check data type in Java

In this section, we will learn about the methodology to verify the data type in Java. There are mainly two methods in java called as getClass() and getName(). They are...

3 minutes read.

How to Send SMS in Java with Example

Sending SMS messages in Java is a fairly common task, and there are a number of libraries and APIs available to help you do it. One popular option is to...

2 minutes read.

Java Transient Keyword

An object in Java can be turned into a stream of bytes using serialization. The data of the instance and the kind of data saved in that instance are both...

3 minutes read.

Java String equalsIgnoreCase() method

equalsIgnoreCase() method compares two Strings based on the their content but ignore the case. Syntax public boolean equalsIgnoreCase(Objects anObject) Parameter anObject: Object to be compared with the current String without case consideration. Returns It returns true...

1 minute read.

Difference between JIT and JVM in Java

In this tutorial, we will discuss the difference between JIT (Just In Time Compiler) and JVM (Java Virtual Machine) in Java. Before we move to the differences, let's understand what...

4 minutes read.

How to Reduce Time Complexity in Java

What is time complexity?  The time complexity in java is given as the amount of time a program requires to run or execute it Calculating the time complexity of the program The time...

4 minutes read.

Equidigital in Java

In this section, we will understand what is an equidigital number and how to write Java programs to locate them. It is commonly asked in academic settings and Java coding...

4 minutes read.

Salesman Problem in Java

The Traveling Salesman Problem determines the shortest path that visits each city approximately once and loops back to the starting location. Another Java problem that is most like the Traveling...

5 minutes read.

Java Extends vs Implements

Java: We known that the java is a pure object oriented programming language. Java programming language consists of many features such as portable, plat form independence, secured, robust, simple, architecture neutral,...

4 minutes read.

IdentityHashMap in Java

The IdentityHashMap class is comparable to the HashMap class and is an AbstractMap implementation. However, when comparing the key, it uses reference equality rather than object equality (or values). Identity HashMap...

6 minutes read.