📜  查找给定矩阵的Frobenius范数

📅  最后修改于: 2021-05-28 03:59:23             🧑  作者: Mango

给定一个M * N矩阵,任务是找到矩阵的Frobenius范数。矩阵的Frobenius范数定义为矩阵元素平方和的平方根。
例子:

方法:找到矩阵元素的平方和,然后打印计算值的平方根。
下面是上述方法的实现:

CPP
// C++ implementation of the approach
#include 
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


输出:
5.47723

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。