Frobenius Norm of a given matrix in Java
In this tutorial, I will show how to calculate the Frobenius Norm of a given matrix in Java. It is very basic and just involves a beginner level knowledge of how matrices work in programming. Let us get started.
Calculating the Frobenius Norm of a matrix in Java
I will first explain what is the Frobenius norm of a matrix. It is the square root of the sum of squares of each element in our matrix. I will show an example to make it more clear:
Matrix:
A = 1 2 3
4 5 6
7 8 9
Sum of squares = 285
Frobenius Norm = Square root of Sum of squares
= √285 = 16.88
I will be using the OOPs concepts of Java to implement my approach to solve this problem. There are two functions in my class: calculate() and main() function.
calculate() function takes 3 user inputs passed as parameters in function calling in main() function. The three inputs will be the number of rows and columns of the matrix. Then a 2-D array will be having the elements of the matrix. Using a nested loop, I have calculated the sum of the square of each element. I have used the Math.sqrt() pre-defined function to calculate the square root of the sum we obtained. Note here that I have made my function static. I have done this because we need to pass an array to the function as a parameter. We cannot do this to a non-static method.
In the main() function, I have made an object of the Scanner class. After that, I have taken user input of the dimensions of the matrix. Following this, I have entered the elements of the matrix. I have made an object of our class and called our calculate() function by passing the parameters.
I have shown my code below:
import java.util.*; public class Frobenius_Norm { static void calculate(int a[][], int m, int n) { int i,j; double sumofsquares = 0.0; double result; //Printing the matrix for(i=0;i<m;i++) { for(j=0;j<n;j++) { System.out.print(a[i][j] + " "); } System.out.println(); } //Calculating sum of squares of each number for(i=0;i<m;i++) { for(j=0;j<n;j++) { sumofsquares = sumofsquares + a[i][j]*a[i][j]; } } //Taking square root to get final result result = Math.sqrt(sumofsquares); System.out.println("Sum of Squares of all elements: " + sumofsquares); System.out.println("Frobenius Norm: " + result); } public static void main(String[] args) { Scanner ob = new Scanner(System.in); System.out.println("Enter no. of rows for matrix"); int a = ob.nextInt(); System.out.println("Enter no. of columns for matrix"); int b = ob.nextInt(); int A[][] = new int[a][b]; System.out.println("Enter matrix"); for(int i=0;i<a;i++) { for(int j=0;j<b;j++) { A[i][j] = ob.nextInt(); } } System.out.println("The matrix is: "); Frobenius_Norm fn = new Frobenius_Norm(); fn.calculate(A, a, b); } }
Input:
I have entered the number of rows and number of columns. Once the dimensions are set, I input the elements of the matrix.
Output:
Enter no. of rows for matrix 3 Enter no. of columns for matrix 3 The matrix is: 1 2 3 4 5 6 7 8 9 Sum of Squares of all elements: 285.0 Frobenius Norm: 16.881943016134134
Also read: Find whether an array is subset of another array in Java
Leave a Reply