Dada una array M * N , la tarea es encontrar la Norma de Frobenius de la array. La Norma de Frobenius de una array se define como la raíz cuadrada de la suma de los cuadrados de los elementos de la array.
Ejemplo:
Entrada: mat[][] = {{1, 2}, {3, 4}}
Salida: 5,47723
sqrt(1 2 + 2 2 + 3 2 + 4 2 ) = sqrt(30) = 5,47723
Entrada: mat[] [] = {{1, 4, 6}, {7, 9, 10}}
Salida: 16,8226
Planteamiento: Encuentra la suma de los cuadrados de los elementos de la array y luego imprime la raíz cuadrada del valor calculado.
A continuación se muestra la implementación del enfoque anterior:
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>
5.47723
Complejidad de tiempo: O(M*N)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por priyanshid1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA