RunMat
GitHub

hypot — Compute element-wise Euclidean norms with hypot in MATLAB and RunMat.

z = hypot(x, y) computes sqrt(|x|.^2 + |y|.^2) element-wise using overflow-resistant arithmetic. Implicit expansion, complex handling, and output shape follow MATLAB semantics.

Syntax

R = hypot(X, Y)

Inputs

NameTypeRequiredDefaultDescription
XAnyYesLeft operand.
YAnyYesRight operand.

Returns

NameTypeDescription
RNumericArrayElementwise Euclidean norm result.

Errors

IdentifierWhenMessage
RunMat:hypot:InvalidInputInput value cannot be converted to supported numeric form.hypot: invalid input
RunMat:hypot:SizeMismatchOperands are not broadcast-compatible.hypot: size mismatch
RunMat:hypot:InternalInternal gather/provider/tensor construction failed.hypot: internal error

How hypot works

  • Real inputs return non-negative doubles even when the operands are negative.
  • Complex arguments are converted to their magnitudes before forming sqrt(|x|.^2 + |y|.^2), matching MATLAB's definition since R2020b.
  • Scalars broadcast across the other operand when shapes are compatible; otherwise an error is raised.
  • Empty inputs propagate emptiness according to MATLAB's size rules.
  • Inf operands return Inf; NaN operands propagate NaN.

Does RunMat run hypot on the GPU?

Hook available and shapes match: the Euclidean norm executes in a single fused GPU kernel.

Hook missing or implicit expansion required: RunMat gathers the data to the host, computes the MATLAB-compatible result, and reuses the host tensor downstream.

Fusion-enabled expressions: the fusion planner can emit a WGSL hypot(a, b) node so long as the active provider marks the group as supported; otherwise the execution seamlessly falls back to the host path.

Providers can implement elem_hypot by emitting a single WGSL/compute shader that calls hypot(a, b); the included WGPU backend demonstrates this pattern.

GPU memory and residency

Manual gpuArray calls are typically unnecessary. RunMat's planner keeps tensors on the GPU when profitable and automatically gathers data when the active provider lacks elem_hypot. You can still force residency with explicit gpuArray/gather to mirror MATLAB workflows or when interoperating with custom kernels.

Examples

Computing the hypotenuse of two scalars

result = hypot(3, 4)

Expected output:

result = 5

Using hypot with vectors and implicit expansion

x = [-3, 0, 4];
y = 4;
norms = hypot(x, y)

Expected output:

norms = [5 4 5]

Calculating per-element distances between two matrices

X = [1 2; 3 4];
Y = [0 1; 1 0];
dist = hypot(X, Y)

Expected output:

dist = [1.0000 2.2361; 3.1623 4.0000]

Working with complex numbers

a = [1+2i, 3-4i];
b = [2-1i, -1+1i];
mag = hypot(a, b)

Expected output:

mag = [3.1623 5.1962]

Applying hypot to character data

codes = hypot('A', zeros(1, 1))

Expected output:

codes = 65.0000

Executing hypot on GPU arrays

Gx = gpuArray([3 4; 5 12]);
Gy = gpuArray([4 3; 12 5]);
distance_gpu = hypot(Gx, Gy);
distance = gather(distance_gpu)

Expected output:

distance = [5 5; 13 13]

Using hypot with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how hypot changes the result.

Run a small hypot example, explain the result, then change one input and compare the output.

FAQ

Does hypot overflow for large inputs?

No. Like MATLAB, RunMat uses a stable algorithm that scales the operands to avoid overflow and underflow. Very large inputs return finite results when mathematically possible.

What happens when the inputs are negative?

hypot always returns a non-negative result because it squares the magnitudes first. Signs on the inputs only matter when NaN or Inf is involved.

Can I mix real and complex inputs?

Yes. Complex inputs are converted to magnitudes before forming the norm, so you can freely combine real, complex, and logical data.

Does hypot accept logical or character arrays?

Yes. Logical values map to 0 and 1, while character arrays use their Unicode code points (as doubles), matching MATLAB semantics.

How does broadcasting work?

The function applies MATLAB's implicit expansion: singleton dimensions expand to match the other operand. If a non-singleton dimension differs, RunMat raises a dimension mismatch error.

Are GPU results identical to CPU results?

For double precision providers the results match bit-for-bit. Single precision providers (e.g., wgpu in F32 mode) may differ by typical floating-point rounding.

What if only one operand lives on the GPU?

The runtime gathers the GPU operand, computes the norm on the host, and continues execution. Future providers may implement scalar expansion directly on the device.

Does hypot allocate a new array?

Yes. The builtin returns a fresh tensor. Fusion may elide intermediate buffers when the expression participates in a larger GPU kernel.

How can I compute the magnitude of a vector of components?

Use hypot repeatedly: hypot(x, hypot(y, z)) computes the Euclidean norm of (x, y, z) element-wise without manual squaring.

Elementwise

abs · angle · complex · conj · double · exp · expm1 · factorial · gamma · imag · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · plus · pow2 · power · rdivide · real · sign · single · sqrt · times

Trigonometry

acos · acosh · asin · asinh · atan · atan2 · atanh · cos · cosd · cosh · deg2rad · rad2deg · sin · sind · sinh · tan · tand · tanh

Reduction

all · any · cummax · cummin · cumprod · cumsum · cumtrapz · diff · gradient · max · mean · median · min · nnz · prod · std · sum · trapz · var

Signal

blackman · butter · conv · conv2 · deconv · filter · hamming · hann · sawtooth · sinc · square

Rounding

ceil · fix · floor · mod · rem · round

Factor

chol · eig · lu · qr · svd

Solve

cond · det · inv · linsolve · norm · pinv · rank · rcond · rref

Fft

fft · fft2 · fftshift · ifft · ifft2 · ifftshift

Interpolation

interp1 · interp2 · pchip · ppval · spline

Ode

ode15s · ode23 · ode45

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how hypot is executed, line by line, in Rust.

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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.