Norm¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
Norm[expr]
gives the 2-norm of a number, vector, or matrix (Frobenius norm for matrices).
Norm[expr, p]
gives the p-norm: Abs[expr] for scalars; (Sum |xi|^p)^(1/p) for vectors with 1 \<= p \< Infinity; Max[Abs[expr]] for p == Infinity; induced operator norm for matrices when p is 1, 2, or Infinity.
Examples (12)¶
Every input below was run against the current Mathilda build and its output recorded.
Basic examples (6)¶
In[1]:= Norm[{x, y, z}]
Out[1]= Sqrt[Abs[x]^2 + Abs[y]^2 + Abs[z]^2]
In[2]:= Norm[-2 + I]
Out[2]= Sqrt[5]
In[3]:= v = {1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1}; Norm[v]
Out[3]= Sqrt[5]
In[4]:= Norm[{x, y, z}, p]
Out[4]= (Abs[x]^p + Abs[y]^p + Abs[z]^p)^(1/p)
In[5]:= Norm[{x, y, z}, Infinity]
Out[5]= Max[Abs[x], Abs[y], Abs[z]]
In[6]:= Norm[{{a11, a12}, {a21, a22}}, "Frobenius"]
Out[6]= Sqrt[Abs[a11]^2 + Abs[a12]^2 + Abs[a21]^2 + Abs[a22]^2]
Applications (6)¶
In[7]:= Norm[{3, 4}]
Out[7]= 5
In[8]:= Norm[{a, b, c}]
Out[8]= Sqrt[Abs[a]^2 + Abs[b]^2 + Abs[c]^2]
In[9]:= Norm[{1, 2, 3, 4}, 1]
Out[9]= 10
In[10]:= Norm[{1, 2, 3, 4}, Infinity]
Out[10]= 4
In[11]:= Norm[{1, 2, 3, 4}, 3]
Out[11]= 10^(2/3)
In[12]:= N[Norm[{1, 2, 3}], 40]
Out[12]= 3.7416573867739413855837487323165493017559
Algorithm¶
Vector and matrix norms.
Norm[x] scalar -> Abs[x]
Norm[v] vector -> 2-norm
Norm[v, p] vector -> p-norm, also Infinity
Norm[m, "Frobenius"] -> Frobenius norm (works on any tensor rank)
Other matrix norms (SVD-based 2-norm, etc.) are not yet implemented and fall through to NULL so the call stays symbolic.
Implementation notes¶
Algorithm. builtin_norm uses get_tensor_dims to classify the argument and then builds a symbolic norm expression that it evaluates. A rank-0 scalar reduces to Abs[x] (the p argument is rejected for scalars). For a rank-1 vector (or any tensor when p is the string "Frobenius") it flattens to Expr** and assembles: Norm[v, Infinity] → Max[Abs[v_i]]; otherwise (default p = 2, or "Frobenius" → 2, or a user p) → Power[Sum[Abs[v_i]^p], 1/p]. Every Abs, Power, Plus, Max is created and run through the evaluator (eval_and_free), so exact/symbolic/complex entries produce exact symbolic norms.
Limits. Genuine matrix p-norms (e.g. the spectral 2-norm via the largest singular value) are not implemented — a rank-≥2 argument with a non-"Frobenius" p falls through to NULL and the call stays symbolic. Jagged arrays (rank < 0) also return NULL.
Protected.- For scalars,
Norm[z]isAbs[z]. - For vectors,
Norm[v]defaults to the 2-norm:Sqrt[v . Conjugate[v]]. - For vectors,
Norm[v, p]isTotal[Abs[v]^p]^(1/p). - For vectors,
Norm[v, Infinity]is the $\infty$-norm given byMax[Abs[v]]. Norm[m, "Frobenius"]gives the Frobenius norm of a matrixm.- Packed/NDArray fast path: an inexact vector or matrix routes through the
LAPACK-backed
ndla_norm(dlange/zlange, and an SVD for the induced matrix 2-norm);Normis on the packed-arrayAWARElist. A machine-precision vector (even a small unpackedList) is routed here too, so it does not hit the overflow described next; exact vectors keep their exact symbolic answer (Norm[{1,2}] == Sqrt[5]) and MPFR vectors stay on the symbolic path. - Overflow-safe 2-/p-norm: the vector 2-norm uses LAPACK
dnrm2's running-scale recurrence (and thep-norm scales by the max component), so a well-scaled vector whose true norm is representable does not overflow toinfor underflow to0—Norm[{1e200, 1e200}]gives1.414×10^200(wasinf), andNormalizeof the same vector gives{0.707…, 0.707…}(was the zero vector). Compile[]: bothNorm[v]/Norm[m]and the two-argument forms lower to a machineReal. Over a declared array argument the compiled body delegates tondla_norm(theV_NORMopcode carries the literalp), coveringNorm[v, 1],Norm[v, Infinity],Norm[v, k],Norm[v, 2.5], and the matrixNorm[m, 1]/Norm[m, Infinity]/Norm[m, "Frobenius"]. An induced matrixp-norm withp ∉ {1, 2, Infinity}has no LAPACK path and stays interpreted.- Inline vector: a
Norm[{e1, …, en}, p]over aListliteral (rather than a declared array) compiles by expanding to the scalar arithmetic above (Sqrt[Σ Abs[ei]^2],Σ Abs[ei],Max[Abs[ei]],(Σ Abs[ei]^p)^(1/p)), so aNorm[{f[x], …}]body auto-compiles insidePlot/Table/NIntegrate/FindRoot.
Attributes: Protected.
References¶
See also: List, Normalize, Real, Plot, Table, NIntegrate, FindRoot
- Source:
src/linalg/norm.c - Specification:
docs/spec/builtins/linear-algebra.md - Tests:
tests/test_basin_hopping.c - Tests:
tests/test_compile_transforms.c - Tests:
tests/test_core.c - Tests:
tests/test_diagonal.c
Notes & additional examples¶
Notes¶
Norm[expr] gives the 2-norm of a scalar or vector; Norm[expr, p] gives the
p-norm (Abs for scalars; (Sum |xi|^p)^(1/p) for vectors with 1 <= p <
Infinity; Max[Abs[expr]] for p == Infinity). Results stay exact for exact
input, and symbolic components are written with Abs so the formula is valid for
complex entries.