abs — Absolute value or magnitude of scalars, vectors, matrices, or N-D tensors.
y = abs(x) returns the absolute value of real inputs and the magnitude (modulus) of complex inputs. For tensors, the operation is applied element-wise following MATLAB's broadcasting rules.
How abs works in RunMat
- 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 (
absonly accepts numeric, logical, or char inputs). - NaN values remain NaN; the function does not change IEEE NaN propagation rules.
How abs runs 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.
Complex tensors are currently handled on the host because the in-process and WGPU providers emit real-valued magnitudes. Device providers are encouraged to add fused complex support.
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 = 42Taking 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.^2FAQ
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.
Related functions to explore
These functions work well alongside abs. Each page has runnable examples you can try in the browser.
sin, sum, gpuArray, gather, angle, conj, double, exp, expm1, factorial, gamma, hypot, imag, ldivide, log, log10, log1p, log2, minus, plus, pow2, power, rdivide, real, sign, single, sqrt, times
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how abs works, line by line, in Rust.
- View abs.rs on GitHub
- Learn how the 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 — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.