Computes the p-norm of a vector

vecnorm(x, p=2)

Arguments

x

the vector whose norm is sought (either numeric or complex).

p

a number or character string indicating the type of norm desired. Possible values include real number greater or equal to 1, Inf, or character strings "euclidean" or "maximum". Default: 2.

Value

requested p-norm of input vector.

See also

Examples

## compare 2-norm calculations 
x <- rnorm(100)
sqrt(sum(x*x))
#> [1] 10.59982
vecnorm(x)
#> [1] 10.59982

## compare 2-norm of series which sums to Inf. The 
## vecnorm returns a finite value in this case. 
x <- rep(sqrt(.Machine$double.xmax), 4)
sqrt(sum(x*x))
#> [1] Inf
vecnorm(x)
#> [1] 2.681562e+154

## 1-norm comparison 
sum(abs(x))
#> [1] 5.363123e+154
vecnorm(x, p=1)
#> [1] 5.363123e+154

## L-infinity norm comparison 
max(abs(x))
#> [1] 1.340781e+154
vecnorm(x, p=Inf)
#> [1] 1.340781e+154