View all functions

CategoryMath: Linalg & Solve
GPUYes
BLAS/LAPACK

What does the norm function do in MATLAB / RunMat?

norm(X) returns the magnitude of vectors and matrices. For vectors it defaults to the Euclidean 2-norm. For matrices it defaults to the spectral norm (largest singular value). Alternate norms — such as the 1-norm, infinity norm, Frobenius norm, and nuclear norm — are selected with the second argument.

  • norm(X) and norm(X, 2) compute the Euclidean norm for vectors and the largest singular value for matrices.
  • norm(X, 1) is the maximum absolute column sum of X.
  • norm(X, Inf) is the maximum absolute row sum of X; norm(x, -Inf) returns the minimum absolute element for vectors.
  • norm(X, 'fro') is the Frobenius norm, equivalent to treating the matrix as a vector and applying the 2-norm.
  • norm(X, 'nuc') is the nuclear norm (sum of singular values) for matrices.
  • norm(x, p) accepts any real scalar p ≥ 1 for vectors (plus the special cases 0, Inf, -Inf), matching MATLAB’s behavior.
  • Inputs must be vectors or 2-D matrices. Higher-dimensional arrays raise an error.
  • Empty inputs (e.g., [] or matrices with a zero dimension) return 0.
  • Logical and integer inputs are promoted to double precision before the norm is computed.
  • Complex values use the magnitude of each entry, and the final result is always a non-negative real scalar.

norm Function GPU Execution Behavior

When an acceleration provider is active, RunMat keeps GPU inputs resident. If the provider exposes a dedicated norm hook in the future it can execute entirely on the device. Until then RunMat gathers the input tensor to the host, computes the norm with the shared CPU implementation, and returns the scalar result. This mirrors MATLAB semantics while guaranteeing correctness even in the absence of specialized kernels.

Examples of using the norm function in MATLAB / RunMat

Computing the Euclidean norm of a vector

x = [3 4];
mag = norm(x);

Expected output:

mag = 5

Calculating the Frobenius norm of a matrix

A = [1 -2 3; -4 5 -6];
f = norm(A, 'fro');

Expected output:

f = 9.5394

Using the infinity norm for robust bounds

x = [2 -7 4];
bound = norm(x, Inf);

Expected output:

bound = 7

Summing singular values with the nuclear norm

A = [2 0 0; 0 1 0];
tau = norm(A, 'nuc');

Expected output:

tau = 3

Computing the norm of complex-valued data

z = [1+2i 3-4i];
mag = norm(z);

Expected output:

mag = 5.4772

Preserving GPU residency transparently

G = gpuArray([3 4 12]);
mag = norm(G);      % falls back to CPU today; future providers can stay on device

Expected output:

mag = 13

GPU residency in RunMat (Do I need gpuArray?)

You typically do NOT need to call gpuArray manually. The fusion planner and Accelerate layer keep data on the GPU whenever a provider offers the required hooks. Today, norm gathers to the host if the provider lacks a specialized kernel and returns a host scalar, just like MATLAB. Providers can add future norm kernels without breaking user code. gpuArray and gather remain available for explicit residency control and MATLAB compatibility.

FAQ

What is the difference between norm(x) and norm(x, 2)?

No difference. Both compute the Euclidean norm for vectors (or the spectral norm for matrices).

Does norm support fractional powers?

Yes. For vectors you can pass any non-zero real scalar p, including fractional values. MATLAB’s special cases (p = 0, Inf, -Inf) are also supported.

Can I use norm on complex matrices?

Absolutely. RunMat mirrors MATLAB by computing singular values in complex arithmetic and always returning a non-negative real scalar.

When should I use 'fro' versus 'nuc'?

'fro' returns the square root of the sum of squares (robust and inexpensive). 'nuc' sums singular values and is particularly useful in low-rank optimization problems.

Why does norm of an empty matrix return 0?

That matches MATLAB’s convention: the sum over an empty set is zero, so every supported norm returns 0 for empty inputs.

Can the norm overflow or underflow?

Yes. The result obeys IEEE-754 double precision rules, just like MATLAB. Extremely large or tiny values may overflow to Inf or underflow toward zero.

Will the result stay on the GPU?

The current in-process provider gathers the tensor to host memory. Providers that implement custom norm kernels can keep the computation entirely on device without user-visible changes.

See Also

sum, svd, cond, pinv, gpuArray, gather

Source & Feedback