Computes the ell^p norm of an n-dimensional (real/complex)
vector mathbf{x} in mathbf{C}^n
≤ft|≤ft| mathbf{x}
ight|
ight|_p = ≤ft( ∑_{i=1}^n
≤ft| x_i
ight|^p
ight)^{1/p}, p in [0, ∞],
where ≤ft| x_i
ight| is the absolute value of x_i. 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
mathbf{x}; for p = ∞ it is the maximum of the absolute
values of mathbf{x}.
The norm of mathbf{x} equals 0 if and only if mathbf{x} =
mathbf{0}.
Usage
lp_norm(x, p = 2)
Arguments
x
n-dimensional vector (possibly complex values)
p
which norm? Allowed values p ≥q 0 including Inf.
Default: 2 (Euclidean norm).
Value
Non-negative float, the norm of mathbf{x}.
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)
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(LambertW)
Loading required package: MASS
Loading required package: ggplot2
This is 'LambertW' version 0.6.4. Please see the NEWS file and citation("LambertW").
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/LambertW/lp_norm.Rd_%03d_medium.png", width=480, height=480)
> ### Name: lp_norm
> ### Title: lp norm of a vector
> ### Aliases: lp_norm
> ### Keywords: math
>
> ### ** Examples
>
>
> kRealVec <- c(3, 4)
> # Pythagoras
> lp_norm(kRealVec)
[1] 5
> # did not know Manhattan,
> lp_norm(kRealVec, p = 1)
[1] 7
>
> # so he just imagined running in circles.
> kComplexVec <- exp(1i * runif(20, -pi, pi))
> plot(kComplexVec)
> sapply(kComplexVec, lp_norm)
[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
>
>
>
>
>
>
> dev.off()
null device
1
>