What does the abs function do in MATLAB / RunMat?
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 does the abs function behave in MATLAB / 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.
abs Function GPU Execution Behaviour
When RunMat Accelerate is active, tensors that reside on the GPU stay on the device. The runtime
checks whether the active provider implements the unary_abs hook:
- 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.
Examples of using the abs function in MATLAB / RunMat
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);
positive stays on the GPU when the provider implements unary_abs; otherwise RunMat gathers the data,
applies the CPU path, and continues execution transparently.
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;
RunMat's fusion planner keeps tensors on the GPU when profitable, so this expression can
stay device-resident without manual gpuArray calls.
GPU residency in RunMat (Do I need gpuArray?)
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).
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.
See Also
Source & Feedback
- The full source code for the implementation of the
absfunction is available at:crates/runmat-runtime/src/builtins/math/elementwise/abs.rs - Found a bug or behavioral difference? Please open an issue with details and a minimal repro.