MATRIX_NORM
Calculates various matrix or vector norms based on the ‘ord’ parameter. By default, it computes the Frobenius norm for matrices and the L2 norm for vectors.
Excel Usage
=MATRIX_NORM(matrix, ord_type, ord_val)
matrix(list[list], required): 2D array of numeric values.ord_type(str, optional, default: “fro”): Type of norm to compute. Choose from common types or provide a numeric value.ord_val(float, optional, default: 2): Numeric order of the norm (if ord_type is set to ‘numeric’).
Returns (float): The calculated norm.
Example 1: Frobenius norm of 2x2 matrix
Inputs:
| matrix | ord_type | |
|---|---|---|
| 1 | 2 | fro |
| 3 | 4 |
Excel formula:
=MATRIX_NORM({1,2;3,4}, "fro")
Expected output:
5.47723
Example 2: L2 norm of a vector (as 1xN matrix)
Inputs:
| matrix | ord_type | |
|---|---|---|
| 3 | 4 | 2 |
Excel formula:
=MATRIX_NORM({3,4}, 2)
Expected output:
5
Example 3: Nuclear norm
Inputs:
| matrix | ord_type | |
|---|---|---|
| 1 | 0 | nuc |
| 0 | 1 |
Excel formula:
=MATRIX_NORM({1,0;0,1}, "nuc")
Expected output:
2
Example 4: L3 norm of a vector
Inputs:
| matrix | ord_type | ord_val | ||
|---|---|---|---|---|
| 1 | 2 | 2 | numeric | 3 |
Excel formula:
=MATRIX_NORM({1,2,2}, "numeric", 3)
Expected output:
2.57128
Python Code
import numpy as np
from scipy.linalg import norm as scipy_norm
def matrix_norm(matrix, ord_type='fro', ord_val=2):
"""
Compute matrix or vector norm.
See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.norm.html
This example function is provided as-is without any representation of accuracy.
Args:
matrix (list[list]): 2D array of numeric values.
ord_type (str, optional): Type of norm to compute. Choose from common types or provide a numeric value. Valid options: Frobenius (Matrix default), Nuclear, Max absolute (inf), Min absolute (-inf), 1-norm, 2-norm (Spectral), Numeric. Default is 'fro'.
ord_val (float, optional): Numeric order of the norm (if ord_type is set to 'numeric'). Default is 2.
Returns:
float: The calculated norm.
"""
try:
def to2d(x):
return [[x]] if not isinstance(x, list) else x
matrix = to2d(matrix)
try:
a = np.array(matrix, dtype=float)
except (ValueError, TypeError):
return "Error: matrix entries must contain numeric values"
if not np.all(np.isfinite(a)):
return "Error: matrix must contain only finite numbers"
# Handle ord values supplied either as strings or numeric YAML scalars.
ot = ord_type
if isinstance(ot, str):
ot = ot.lower()
if ot == "numeric":
order = ord_val
elif ot in (1, 2, "1", "2"):
order = int(ot)
elif ot == "inf":
order = np.inf
elif ot == "-inf":
order = -np.inf
else:
order = ot
# Fix for vectors: if order is numeric (not fro or nuc) and input is 1D, flatten it
if order not in ("fro", "nuc") and (a.shape[0] == 1 or a.shape[1] == 1):
a = a.flatten()
try:
res = scipy_norm(a, ord=order)
except Exception as e:
return f"Error: {str(e)}"
return float(res)
except Exception as e:
return f"Error: {str(e)}"Online Calculator
2D array of numeric values.
Type of norm to compute. Choose from common types or provide a numeric value.
Numeric order of the norm (if ord_type is set to 'numeric').