lp_norm function

lp norm of a vector

lp norm of a vector

Computes the p\ell^p norm of an n-dimensional (real/complex) vector xCn\mathbf{x} \in \mathbf{C}^n

[REMOVE_ME]xp=(i=1nxip)1/p,p[0,],[REMOVEME2] \left|\left| \mathbf{x} \right|\right|_p = \left( \sum_{i=1}^n\left| x_i \right|^p \right)^{1/p}, p \in [0, \infty], [REMOVE_ME_2]

where xi\left| x_i \right| is the absolute value of xix_i. For p=2p=2 this is Euclidean norm; for p=1p=1 it is Manhattan norm. For p=0p=0 it is defined as the number of non-zero elements in x\mathbf{x}; for p=p = \infty it is the maximum of the absolute values of x\mathbf{x}.

The norm of x\mathbf{x} equals 00 if and only if c("mathbfx=\n\\mathbf{x} =\n", "mathbf0 \\mathbf{0}").

lp_norm(x, p = 2)

Arguments

  • x: n-dimensional vector (possibly complex values)
  • p: which norm? Allowed values p0p \geq 0 including Inf. Default: 2 (Euclidean norm).

Returns

Non-negative float, the norm of x\mathbf{x}.

Description

Computes the p\ell^p norm of an n-dimensional (real/complex) vector xCn\mathbf{x} \in \mathbf{C}^n

xp=(i=1nxip)1/p,p[0,], \left|\left| \mathbf{x} \right|\right|_p = \left( \sum_{i=1}^n\left| x_i \right|^p \right)^{1/p}, p \in [0, \infty],

where xi\left| x_i \right| is the absolute value of xix_i. For p=2p=2 this is Euclidean norm; for p=1p=1 it is Manhattan norm. For p=0p=0 it is defined as the number of non-zero elements in x\mathbf{x}; for p=p = \infty it is the maximum of the absolute values of x\mathbf{x}.

The norm of x\mathbf{x} equals 00 if and only if c("mathbfx=\n\\mathbf{x} =\n", "mathbf0 \\mathbf{0}").

Examples

kRealVec <- c(3, 4) # Pythagoras lp_norm(kRealVec) # did not know Manhattan, lp_norm(kRealVec, p = 1) # so he just imagined running in circles. kComplexVec <- exp(1i * runif(20, -pi, pi)) plot(kComplexVec) sapply(kComplexVec, lp_norm)
  • Maintainer: Georg M. Goerg
  • License: GPL (>= 2)
  • Last published: 2023-11-30