norm — Compute vector and matrix norms in MATLAB and RunMat.
norm(X) returns vector or matrix magnitudes. Default norms, alternate p/keyword options (including Frobenius and nuclear), and domain restrictions follow MATLAB semantics.
Syntax
n = norm(A)
n = norm(A, p)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | Any | Yes | — | Input vector or matrix. |
p | Any | No | — | Norm order selector. |
Returns
| Name | Type | Description |
|---|---|---|
n | NumericScalar | Norm value of A. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:norm:InvalidArgument | Norm-order argument is malformed or unsupported for the requested domain. | norm: invalid argument |
RunMat:norm:InvalidInput | Input shape/type cannot be processed for norm evaluation. | norm: invalid input |
RunMat:norm:Internal | Runtime fails while computing norm or executing fallback/upload paths. | norm: internal runtime failure |
Does RunMat run norm on the GPU?
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.
GPU memory and residency
G = gpuArray([3 4 12]);
mag = norm(G); % falls back to CPU today; future providers can stay on deviceExpected output:
mag = 13Examples
Computing the Euclidean norm of a vector
x = [3 4];
mag = norm(x)Expected output:
mag = 5Calculating the Frobenius norm of a matrix
A = [1 -2 3; -4 5 -6];
f = norm(A, 'fro')Expected output:
f = 9.5394Using the infinity norm for robust bounds
x = [2 -7 4];
bound = norm(x, Inf)Expected output:
bound = 7Summing singular values with the nuclear norm
A = [2 0 0; 0 1 0];
tau = norm(A, 'nuc')Expected output:
tau = 3Computing the norm of complex-valued data
z = [1+2i 3-4i];
mag = norm(z)Expected output:
mag = 5.4772Preserving GPU residency transparently
G = gpuArray([3 4 12]);
mag = norm(G); % falls back to CPU today; future providers can stay on deviceExpected output:
mag = 13Using norm with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how norm changes the result.
Run a small norm example, explain the result, then change one input and compare the output.
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.
Related Linalg functions
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how norm is executed, line by line, in Rust.
- View the source for norm in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.