Finding the Frobenius Norm of a given matrix - GeeksforGeeks

06 May, 2021
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
Java
Python3
C#
Javascript
// 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 matrixfloat 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 codeint main(){    int mat[row][col] = { { 1, 2 }, { 3, 4 } };     cout << frobeniusNorm(mat);     return 0;}
// Java implementation of the approachclass 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 implementation of the approachfrom math import sqrtrow = 2col = 2 # Function to return the Frobenius# Norm of the given matrixdef 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# implementation of the approachusing 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
<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>
5.47723
 
mohit kumar 29
ankthon
sravankumar8128
Arrays
C Programs
Matrix
Technical Scripter
Arrays
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Window Sliding Technique
Trapping Rain Water
Building Heap from Array
Reversal algorithm for array rotation
Program to find sum of elements in a given array
Strings in C
Arrow operator -> in C/C++ with Examples
UDP Server-Client implementation in C
C Program to read contents of Whole File
Header files in C/C++ and its uses