where ∣xi∣ is the absolute value of xi. For p=2 this is Euclidean norm; for p=1 it is Manhattan norm. For p=0 it is defined as the number of non-zero elements in x; for p=∞ it is the maximum of the absolute values of x.
The norm of x equals 0 if and only if c("mathbfx=\n", "mathbf0").
lp_norm(x, p =2)
Arguments
x: n-dimensional vector (possibly complex values)
p: which norm? Allowed values p≥0 including Inf. Default: 2 (Euclidean norm).
Returns
Non-negative float, the norm of x.
Description
Computes the ℓp norm of an n-dimensional (real/complex) vector x∈Cn
∣∣x∣∣p=(i=1∑n∣xi∣p)1/p,p∈[0,∞],
where ∣xi∣ is the absolute value of xi. For p=2 this is Euclidean norm; for p=1 it is Manhattan norm. For p=0 it is defined as the number of non-zero elements in x; for p=∞ it is the maximum of the absolute values of x.
The norm of x equals 0 if and only if c("mathbfx=\n", "mathbf0").
Examples
kRealVec <- c(3,4)# Pythagoraslp_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)