Learn R Programming

grpss (version 3.0.1)

norm_vec: Compute the norm of a vector

Description

Computes the norm of a vector with size adjustment.

Usage

norm_vec(x, norm = c("L1", "L2", "Linf"))

Arguments

x
A numeric vector.
norm
The type of norm. See details.

Value

  • A non-negative number.

Details

This function calculates the norm of a vector. It is slightly different from the ordinary norm function in that L1-norm and L2-norm are adjusted by the length of vector, $n$. To be specific, define a vector $x = (x_1,x_2,...,x_n)$. The L1-norm is defined as $$||x||_1 = (|x_1| + |x_2| + ... + |x_n|)/n.$$ The L2-norm is defined as $$||x||_2 = (\sqrt{(x_1)^2 + (x_2)^2 + ... + (x_n)^2)/n.}$$ The Linf-norm is defined as $$||x||_{\infty} = \max(|x_1|,|x_2|,...,|x_n|).$$ Note that a matrix X will be coerced to a vector.

Examples

Run this code
x <- 1:10
# "L1" norm, same as norm(as.matrix(x), "1")*length(x)
norm_vec(x)

# "L2" norm
norm_vec(x, "L2")

# "Linf" norm, same as norm(as.matrix(x),"I")
norm_vec(x, "Linf")

Run the code above in your browser using DataLab