×

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

Java Extends keyword

Extends Extends is a keyword which is completely depended on the concept of the inheritance of the java programming language.To understand about of the keyword, we need to learn the concept...

3 minutes read.

Minimum XOR value pair in Java

In this section, you will discuss about minimum XOR value pair in Java. The objective is to enforce a value that indicates the least XOR values of the two numbers from...

4 minutes read.

Java Boolean Class

The Boolean class wraps a value of the Boolean primitive in an object. Its object contains only a single field whose type is Boolean. Boolean Methods: This class contains several different methods...

2 minutes read.

Three Partition Problems in Java

In talks with leading IT organizations like Google, Amazon, TCS, Accenture, etc., this extremely intriguing subject is constantly brought up. The goal of the problem-solving exercise is to evaluate the...

8 minutes read.

Java Xml Parser

In this article, we acknowledge you about what is a XML parser in Java, how is it going to work and what is the purpose of it. Also, the article discusses...

6 minutes read.

Anonymous Function in Java

A function defined as being unbound from an identifier is called an anonymous function. Because they permit access to variables within the scope of the contained function, these are a...

4 minutes read.

Java String compareTo() Method

compareTo() method is used to compare the two specified Strings based on the alphabetical order(lexicographical order) of their characters.It returns positive number ,negative number or 0 Syntax: public int compareTo(String anotherString) Parameters: anotherString: the...

2 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.

Method Overloading In Java

If the same class consists of different methods with the same name and the methods can vary by different number of parameters then it is known as Method Overloading. Method...

2 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.

How to Convert String to long in Java

How to Convert String to long in java It is used when you want to perform the mathematical operation on the String which contains long number then the conversion from String...

4 minutes read.

Bubble Sort in Java

Bubble Sort in Java Bubble sort isalso known as sinking sort. It is one of the simplest sorting algorithms. In the bubble sort algorithm, the given array is traversed from left...

5 minutes read.

Java Date add Days

In order to operate with the time and the Date in Java, we used the abstract Calendar class. It has several helpful interfaces that enable us to convert dates between...

4 minutes read.

Java Array Generic

Creating Generic Array in Java A collection of comparable sorts of data is kept in an array. In Java, making a generic array is challenging. The type information of an array's...

4 minutes read.

Stack Program in Java

Stack Program in Java The Stack class is part of the collection framework that inherits the Vector class. Thus, the Stack class can also be called a subclass of the Vector...

5 minutes read.

Matrix Multiplication Program in Java

Matrix Multiplication Program in Java The matrix multiplication program in Java is the continuation of the matrix program in Java that we have already discussed earlier. In this section, we will...

3 minutes read.

How to find length of integer in Java

We can find the length of the integer in many ways. The length of an integer is defined as the count of the number of digits for the given integer. These...

5 minutes read.

Tetris Game in Java

The Tetris game is among the most well-known video games ever produced for computers. Today, we may engage in this game on a mobile device as well. Alexey Pajitnov conceptualized...

12 minutes read.

Java Garbage Collection Interview Questions

One of the key areas of Java is garbage collection. Garbage collection enables apps to manage memory automatically. Interviewers frequently ask inquiries about garbage collection. Q1: What is the purpose of...

6 minutes read.

Check if the given array is mirror inverse in Java

The primary objective is to check whether the given array is mirror inverse or not. The values and position of the specified array are switched, and a duplicate array is created....

3 minutes read.