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
A- Type
Any- Required
- Yes
- Description
- Input vector or matrix.
p- Type
Any- Required
- No
- Description
- Norm order selector.
Returns
n- Type
NumericScalar- Description
- Norm value of A.
Errors
RunMat:norm:InvalidArgument- When
- Norm-order argument is malformed or unsupported for the requested domain.
- Message
norm: invalid argument
RunMat:norm:InvalidInput- When
- Input shape/type cannot be processed for norm evaluation.
- Message
norm: invalid input
RunMat:norm:Internal- When
- Runtime fails while computing norm or executing fallback/upload paths.
- Message
norm: internal runtime failure
Behavior
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.
Acceleration and residency
Floating gpuArray input uses the exact owning provider's norm hook when available; validated host fallback restores a same-precision scalar to that owner. Integer and logical resident data are gated RunMat extensions and bypass floating provider hooks.
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.
`matlab:runnable
G = gpuArray([3 4 12]);
mag = norm(G); % falls back to CPU today; future providers can stay on device
`
Expected output:
`matlab
mag = 13
`
Implementation and validation
Original GPU declarations: precisions f32, f64; elementwise false; reduction true; broadcasting none.
Original fusion declarations: elementwise false; reduction false; max inputs 1; constants inline.
Examples
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
Factor
chol · decomposition · decomposition.ctranspose · decomposition.mldivide · decomposition.mrdivide · decomposition.mtimes · decomposition.rdivide · decomposition.subsref · decomposition.times · decomposition.uminus · decomposition.uplus · eig · eigs · gmres · ichol · ilu · lu · ordeig · ordqz · ordschur · pcg · qr · qz · rsf2csf · schur · svd
Ops
cross · ctranspose · dot · mldivide · mpower · mrdivide · mtimes · pagemtimes · pagetranspose · trace · transpose
Source you can read
Every RunMat function's source is public. 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 a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/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.