norm
result = norm(x, [p])
Input: x, p · Output: result
What it is
Euclidean (L2) norm; for a matrix this is the Frobenius norm (every element treated as one long vector), not a matrix operator norm. x is a scalar, Vec, or Mat (real, or CVec/CMat, which norm by magnitude).
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = norm(x, [p]) | x scalar, Vec, Mat, CVec, or CMatx is a scalar, Vec, or Mat (real, or CVec/CMat, which norm by magnitude).p optionalTwo-argument form added in v0.4.14: norm(x, p). | 1 —largest column sum2 —largest singular value, the operator norminf —largest row suminf —largest magnitude0 numberthe number of nonzero entriesp matrix, vector, or stringa matrix p of 0 or 3, a vector p between 0 and 1 or negative, an unknown string | Returns a scalar Num. Two-argument form added in v0.4.14: norm(x, p). For a matrix p is 1 (largest column sum), 2 (largest singular value, the operator norm), inf (largest row sum) or "fro" (the Frobenius norm, same as norm(A)); for [1 2; 3 4] these give 6, 5.464985704219043, 7 and 5.477225575051661. For a vector (a Vec, a row or column Mat, or a number) p is 1, 2, inf (largest magnitude), -inf (smallest magnitude), 0 (the number of nonzero entries), any p >= 1, or "fro" (same as 2); the powers are scaled so they do not overflow. The one-argument form is unchanged. Real input only. Errors: another p (a matrix p of 0 or 3, a vector p between 0 and 1 or negative, an unknown string), NaN in x, complex input. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
a = [1, 2, 2]
print("dot(a,a) = {dot(a,a)}") # 9
print("norm(a) = {norm(a)}") # 3
output
dot(a,a) = 9 norm(a) = 3
from numerics-dsp
A = randn(60, 200, seed = 7) / sqrt(60)
x = zeros(200)
x[7] = 1.5
x[33] = -2.0
x[91] = 0.75
y = A * x
r = cs_recover(A, y, method = "omp", sparsity = 3)
print("{r.iterations} iterations, converged {r.converged}, error {norm(r.x - x):.2e}")
output
3 iterations, converged true, error 3.19e-12
from compressed-sensing
A = randn(60, 200, seed = 7) / sqrt(60)
x = zeros(200)
x[7] = 1.5
x[33] = -2.0
x[91] = 0.75
y = A * x
r = cs_recover(A, y, method = "fista", lambda = 1e-3)
print("{r.iterations} iterations, converged {r.converged}, error {norm(r.x - x):.2e}")
output
563 iterations, converged true, error 1.75e-03
from compressed-sensing
A = randn(60, 200, seed = 7) / sqrt(60)
x = zeros(200)
x[7] = 1.5
x[33] = -2.0
x[91] = 0.75
y = A * x
r = cs_recover(A, y, method = "cosamp", sparsity = 3)
print("{r.iterations} iterations, converged {r.converged}, error {norm(r.x - x):.2e}")
output
2 iterations, converged true, error 4.87e-12
from compressed-sensing