Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Finding the Frobenius Norm of a given matrix
Next article icon

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.47723
Input: 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)


Next Article
Finding the Frobenius Norm of a given matrix

P

priyanshid1
Improve
Article Tags :
  • Matrix
  • Technical Scripter
  • C Programs
  • DSA
  • Arrays
Practice Tags :
  • Arrays
  • Matrix

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences