RunMat
GitHub

abs — Absolute value and complex magnitude for scalars and arrays in MATLAB and RunMat.

y = abs(x) returns absolute values for real inputs and magnitudes for complex inputs. For arrays, abs is applied element-wise and preserves the input shape.

Syntax

Y = abs(X)

Inputs

NameTypeRequiredDefaultDescription
XAnyYesNumeric, logical, char, or complex input.

Returns

NameTypeDescription
YNumericArrayAbsolute value or magnitude.

Errors

IdentifierWhenMessage
RunMat:abs:InvalidInputInput cannot be interpreted as numeric, logical, char, or complex data.abs: invalid input
RunMat:abs:InternalInternal tensor conversion/allocation/provider interaction failed.abs: internal error

How abs works

  • Real scalars, vectors, matrices, and N-D tensors are mapped to their element-wise absolute values.
  • Complex inputs return the magnitude sqrt(real(x).^2 + imag(x).^2), matching MATLAB semantics.
  • Logical inputs are promoted to doubles (true → 1.0, false → 0.0) before taking the absolute value.
  • Character arrays are converted to double arrays of code point magnitudes, just like MATLAB.
  • String arrays are not supported and raise an error (abs only accepts numeric, logical, or char inputs).
  • NaN values remain NaN; the function does not change IEEE NaN propagation rules.

Does RunMat run abs on the GPU?

Hook available: The absolute value is computed directly on the device with no host transfers.

Hook missing or unsupported dtype: RunMat gathers the tensor to host memory, performs the CPU absolute value logic (including complex magnitudes), and optionally re-uploads downstream.

When a provider supports complex-interleaved storage, abs computes magnitudes on the device and returns a real GPU tensor with the original shape.

GPU memory and residency

You usually do not need to call gpuArray explicitly. RunMat's fusion planner and Accelerate layer track residency automatically, keeping tensors on the GPU whenever device execution is beneficial. Explicit gpuArray/gather calls remain available for MATLAB compatibility or when you need deterministic residency control (e.g., integrating with third-party GPU kernels).

Examples

Getting the absolute value of a scalar

y = abs(-42)

Expected output:

y = 42

Taking the absolute value of a vector

v = [-2 -1 0 1 2];
result = abs(v)

Expected output:

result = [2 1 0 1 2]

Measuring complex magnitudes

z = [3+4i, 1-1i];
magnitudes = abs(z)

Expected output:

magnitudes = [5 1.4142]

Working with matrices on the GPU

G = randn(2048, 2048, "gpuArray");
positive = abs(G)

Using abs with logical arrays

mask = logical([0 1 0; 1 0 1]);
numeric = abs(mask)

Expected output:

numeric = [0 1 0; 1 0 1]

Converting characters to numeric codes

c = 'ABC';
codes = abs(c)

Expected output:

codes = [65 66 67]

Chaining abs inside fused expressions

x = linspace(-2, 2, 5);
y = abs(x) + x.^2

Using abs with coding agents

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

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

FAQ

Does abs change NaN values?

No. abs(NaN) returns NaN, consistent with IEEE arithmetic and MATLAB behaviour.

What happens to complex numbers?

RunMat returns the magnitude sqrt(real(x).^2 + imag(x).^2), identical to MATLAB.

Can I call abs on string arrays?

No. Like MATLAB, abs only accepts numeric, logical, or character inputs. Use double(string) if you need code points.

Does abs work with sparse arrays?

Sparse support is planned but not yet implemented; inputs are densified today.

Is GPU execution exact?

Device execution follows IEEE semantics for the provider's precision (single or double). F32 backends may incur small rounding differences compared to CPU double.

How do I keep results on the GPU?

Avoid calling gather unless you need host data. The planner keeps device tensors resident whenever possible.

Does abs allocate new memory?

Yes. The builtin returns a new tensor; fusion may in-place combine kernels to reduce allocations when safe.

Can I use abs with logical masks?

Yes. Logical inputs are promoted to doubles (0 or 1) before applying abs, just like MATLAB.

Elementwise

angle · complex · conj · double · exp · expm1 · factorial · gamma · heaviside · hypot · 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

Rounding

ceil · fix · floor · mod · rem · round

Factor

chol · eig · lu · qr · svd

Solve

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

Symbolic

digits · int · limit · sym · syms · vpa

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 abs 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.