Finding the Frobenius Norm of a given matrix Last Updated : 24 Jun, 2022 Comments Improve Suggest changes Like Article Like Report Given an M * N matrix, the task is to find the Frobenius Norm of the matrix. The Frobenius Norm of a matrix is defined as the square root of the sum of the squares of the elements of the matrix.Example: Input: mat[][] = {{1, 2}, {3, 4}} Output: 5.47723 sqrt(12 + 22 + 32 + 42) = sqrt(30) = 5.47723Input: mat[][] = {{1, 4, 6}, {7, 9, 10}} Output: 16.8226 Approach: Find the sum of squares of the elements of the matrix and then print the square root of the calculated value.Below is the implementation of the above approach: CPP // C++ implementation of the approach #include <bits/stdc++.h> using namespace std; const int row = 2, col = 2; // Function to return the Frobenius // Norm of the given matrix float frobeniusNorm(int mat[row][col]) { // To store the sum of squares of the // elements of the given matrix int sumSq = 0; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { sumSq += pow(mat[i][j], 2); } } // Return the square root of // the sum of squares float res = sqrt(sumSq); return res; } // Driver code int main() { int mat[row][col] = { { 1, 2 }, { 3, 4 } }; cout << frobeniusNorm(mat); return 0; } Java // Java implementation of the approach class GFG { final static int row = 2, col = 2; // Function to return the Frobenius // Norm of the given matrix static float frobeniusNorm(int mat[][]) { // To store the sum of squares of the // elements of the given matrix int sumSq = 0; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { sumSq += (int)Math.pow(mat[i][j], 2); } } // Return the square root of // the sum of squares float res = (float)Math.sqrt(sumSq); return res; } // Driver code public static void main (String[] args) { int mat[][] = { { 1, 2 }, { 3, 4 } }; System.out.println(frobeniusNorm(mat)); } } // This code is contributed by AnkitRai01 Python3 # Python3 implementation of the approach from math import sqrt row = 2 col = 2 # Function to return the Frobenius # Norm of the given matrix def frobeniusNorm(mat): # To store the sum of squares of the # elements of the given matrix sumSq = 0 for i in range(row): for j in range(col): sumSq += pow(mat[i][j], 2) # Return the square root of # the sum of squares res = sqrt(sumSq) return round(res, 5) # Driver code mat = [ [ 1, 2 ], [ 3, 4 ] ] print(frobeniusNorm(mat)) # This code is contributed by mohit kumar 29 C# // C# implementation of the approach using System; class GFG { static int row = 2, col = 2; // Function to return the Frobenius // Norm of the given matrix static float frobeniusNorm(int [,]mat) { // To store the sum of squares of the // elements of the given matrix int sumSq = 0; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { sumSq += (int)Math.Pow(mat[i, j], 2); } } // Return the square root of // the sum of squares float res = (float)Math.Sqrt(sumSq); return res; } // Driver code public static void Main () { int [,]mat = { { 1, 2 }, { 3, 4 } }; Console.WriteLine(frobeniusNorm(mat)); } } // This code is contributed by AnkitRai01 JavaScript <script> // JavaScript implementation of the approach let row = 2, col = 2; // Function to return the Frobenius // Norm of the given matrix function frobeniusNorm(mat) { // To store the sum of squares of the // elements of the given matrix let sumSq = 0; for (let i = 0; i < row; i++) { for (let j = 0; j < col; j++) { sumSq += parseInt(Math.pow(mat[i][j], 2)); } } // Return the square root of // the sum of squares let res = parseFloat(Math.sqrt(sumSq)); return res; } // Driver code let mat = [[ 1, 2 ], [ 3, 4 ]]; document.write(frobeniusNorm(mat).toFixed(5)); // This code is contributed by sravan kumar </script> Output: 5.47723 Time Complexity: O(M*N) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Finding the Frobenius Norm of a given matrix P priyanshid1 Follow Improve Article Tags : Matrix Technical Scripter C Programs DSA Arrays +1 More Practice Tags : ArraysMatrix Similar Reads C Program To Find Normal and Trace of Matrix Here, we will see how to write a C program to find the normal and trace of a matrix. Below are the examples: Input: mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};Output: Normal = 16Trace = 15 Explanation: Normal = sqrt(1*1+ 2*2 + 3*3 + 4*4 + 5*5 + 6*6 + 7*7 + 8*8 + 9*9) = 16Trace = 1+5+9 = 15 Input: m 2 min read Efficient method to store a Lower Triangular Matrix using Column-major mapping Given a lower triangular matrix Mat[][], the task is to store the matrix using column-major mapping. Lower Triangular Matrix: A Lower Triangular Matrix is a square matrix in which the lower triangular part of a matrix consists of non-zero elements and the upper triangular part consists of 0s. The Lo 10 min read Program to convert given Matrix to a Diagonal Matrix Given a N*N matrix. The task is to convert the matrix to a diagonal matrix. That is to change the values of the non-diagonal elements of a matrix to 0.Diagonal-Matrix: A matrix is called a Diagonal Matrix if all the non-diagonal elements of the matrix are zero.Examples: Input : mat[][] = {{ 2, 1, 7 8 min read Program to find Normal and Trace of a matrix Given a 2D matrix, the task is to find Trace and Normal of matrix.Normal of a matrix is defined as square root of sum of squares of matrix elements.Trace of a n x n square matrix is sum of diagonal elements. Examples : Input : mat[][] = {{7, 8, 9}, {6, 1, 2}, {5, 4, 3}}; Output : Normal = 16 Trace = 6 min read Minimum Distance from a given Cell to all other Cells of a Matrix Given two integers R and C, denoting the number of rows and columns in a matrix, and two integers X and Y, the task is to find the minimum distance from the given cell to all other cells of the matrix. Examples: Input: R = 5, C = 5, X = 2, Y = 2 Output: 2 2 2 2 2 2 1 1 1 2 2 1 0 1 2 2 1 1 1 2 2 2 2 9 min read Like