Skip to content

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] is Abs[z].
  • For vectors, Norm[v] defaults to the 2-norm: Sqrt[v . Conjugate[v]].
  • For vectors, Norm[v, p] is Total[Abs[v]^p]^(1/p).
  • For vectors, Norm[v, Infinity] is the $\infty$-norm given by Max[Abs[v]].
  • Norm[m, "Frobenius"] gives the Frobenius norm of a matrix m.
  • 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); Norm is on the packed-array AWARE list. A machine-precision vector (even a small unpacked List) 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 the p-norm scales by the max component), so a well-scaled vector whose true norm is representable does not overflow to inf or underflow to 0 — Norm[{1e200, 1e200}] gives 1.414×10^200 (was inf), and Normalize of the same vector gives {0.707…, 0.707…} (was the zero vector).
  • Compile[]: both Norm[v]/Norm[m] and the two-argument forms lower to a machine Real. Over a declared array argument the compiled body delegates to ndla_norm (the V_NORM opcode carries the literal p), covering Norm[v, 1], Norm[v, Infinity], Norm[v, k], Norm[v, 2.5], and the matrix Norm[m, 1] / Norm[m, Infinity] / Norm[m, "Frobenius"]. An induced matrix p-norm with p ∉ {1, 2, Infinity} has no LAPACK path and stays interpreted.
  • Inline vector: a Norm[{e1, …, en}, p] over a List literal (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 a Norm[{f[x], …}] body auto-compiles inside Plot/Table/NIntegrate/FindRoot.

Attributes: Protected.

References

See also: List, Normalize, Real, Plot, Table, NIntegrate, FindRoot

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.