norm — Compute vector and matrix norms in MATLAB and RunMat.
norm(X) returns vector or matrix magnitudes. MATLAB-compatible data is single or double; all eight native integer classes and logical data are independently gated RunMat extensions.
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 |
How norm works
- Single input returns a single scalar tensor; double input returns double. RunMat integer and logical extensions return double.
- Typed-integer data remains authoritative until one checked binary64 norm boundary; values outside the exact binary64 integer range reject rather than round. If an owner cannot represent the required double output, automatic residency may finish on host while explicit gpuArray intent errors.
- Positive real scalar orders and the documented norm keywords are accepted subject to vector-versus-matrix restrictions. Logical order selectors, zero norm, and nuclear norm are separately gated RunMat extensions.
Does RunMat run norm on the GPU?
Floating inputs use the exact owner's norm hook when it returns a fresh, correctly shaped and typed result. Unsupported hooks fall back to a non-consuming gather and class-preserving restoration. Integer and logical resident inputs are admitted only in RunMat mode and always take the checked host boundary instead of being mislabeled for a floating provider hook.
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?⌄
Positive real scalar orders, including supported fractional vector orders, use the compatibility surface. Zero norm is available only when RunMat extensions are enabled; Inf and -Inf retain their documented meanings.
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' is part of the compatibility surface and returns the square root of the sum of squares. 'nuc' sums singular values but is a separately gated RunMat extension.
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?⌄
RunMat uses a validated provider norm hook for supported floating inputs. Otherwise it gathers without consuming the source handle and restores the same-precision scalar to the exact owner when that owner can represent it.
Related Linalg functions
Structure
bandwidth · isdiag · ishermitian · issymmetric · istril · istriu · symrcm
Ops
cross · ctranspose · dot · mldivide · mpower · mrdivide · mtimes · pagemtimes · pagetranspose · trace · transpose
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.