vecnorm
vecnorm(A, [p])
For any iterable container A (including arrays of any dimension) of numbers (or any element type for which norm is defined), compute the p-norm (defaulting to p=2) as if A were a vector of the corresponding length.
For example, if A is a matrix and p=2, then this is equivalent to the Frobenius norm.
Examples
-
Compute the Euclidean norm of a vector:
julia> vecnorm([3, 4]) 5.0This example calculates the Euclidean norm (p=2 norm) of the vector [3, 4].
-
Calculate the 1-norm of a matrix:
julia> mat = [1 2; -3 4]; julia> vecnorm(mat, 1) 5.0It computes the 1-norm (sum of absolute values) of the matrix
mattreating it as a vector. -
Calculate the infinity norm of a matrix:
julia> mat = [1 2; -3 4]; julia> vecnorm(mat, Inf) 7.0This example computes the infinity norm (maximum absolute row sum) of the matrix
matas if it were a vector. - Compute the Frobenius norm of a matrix:
julia> mat = [1 2; -3 4]; julia> vecnorm(mat) 5.477225575051661It calculates the Frobenius norm (p=2 norm) of the matrix
mat.
Common mistake example:
julia> vecnorm([1, 2, 3], -1)
ERROR: ArgumentError: Invalid value for p-norm: -1
In this example, -1 is provided as the p value, which is not a valid input. The p value should be a positive number or Inf.
See Also
countfrom, cycle, done, drop, eachindex, enumerate, first, repeated, rest, start, svds, take, vecdot, vecnorm, zip,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.