heaviside — Compute the element-wise Heaviside step function.
Y = heaviside(X) returns the Heaviside step function applied element by element. Negative real values map to 0, zero maps to 0.5, and positive values map to 1.
Syntax
Y = heaviside(X)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
X | Any | Yes | — | Real numeric, logical, or character input. |
Returns
| Name | Type | Description |
|---|---|---|
Y | NumericArray | Elementwise Heaviside step result. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:heaviside:InvalidInput | Input is not a supported real numeric/logical/character value. | heaviside: invalid input |
RunMat:heaviside:ProviderFailed | Provider unary_heaviside dispatch fails with a non-unsupported error. | heaviside: GPU provider unary_heaviside failed |
RunMat:heaviside:Internal | Internal gather/provider/tensor construction failed. | heaviside: internal error |
How heaviside works
- Real scalars, vectors, matrices, and higher-dimensional tensors are evaluated elementwise.
- Both
0.0and-0.0map to0.5. Infmaps to1,-Infmaps to0, andNaNremainsNaN.- CPU, provider, WGPU, and fusion paths all use the same real scalar mapping.
- Logical inputs are promoted to doubles, so
heaviside(false)returns0.5andheaviside(true)returns1. - Character arrays are treated as their numeric code points and return doubles of the same shape.
- Symbolic scalar inputs return symbolic
heaviside(...)expressions. - Complex and string inputs are rejected with a stable invalid-input identifier.
GPU memory and residency
heaviside keeps real GPU tensors resident when the active provider exposes unary_heaviside. If that hook is unavailable, RunMat gathers the tensor and evaluates the same scalar mapping on the host.
Examples
Evaluate the step at representative scalar values
values = heaviside([-2 0 3])Expected output:
values = [0 0.5 1]Gate a sinusoid using a unit step
t = -1:0.01:1;
x = sin(2*pi*t).*heaviside(t)Preserve shape for matrix inputs
A = [-1 0; 2 NaN];
Y = heaviside(A)Expected output:
Y = [0 0.5; 1 NaN]Use logical inputs
mask = [false true];
y = heaviside(mask)Expected output:
y = [0.5 1]Preserve symbolic expressions
syms x;
y = heaviside(x)Expected output:
y = heaviside(x)Execute on a GPU-resident tensor
G = gpuArray([-1 0 1]);
Y = heaviside(G);
host = gather(Y)Using heaviside with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how heaviside changes the result.
Run a small heaviside example, explain the result, then change one input and compare the output.
FAQ
What value does RunMat use at zero?⌄
RunMat returns 0.5 for both positive and negative zero, matching MATLAB's convention for numeric Heaviside step values.
Does heaviside support complex numbers?⌄
No. RunMat implements the real-valued Heaviside step function. Complex inputs raise RunMat:heaviside:InvalidInput instead of silently choosing a branch.
Does heaviside support symbolic expressions?⌄
Yes. Symbolic scalar inputs remain symbolic and format as heaviside(expr).
Does heaviside preserve NaN values?⌄
Yes. NaN inputs remain NaN on CPU, GPU provider, and fused elementwise paths.
Will heaviside(gpuArray(...)) stay on the GPU?⌄
Yes when the active provider implements unary_heaviside. Providers without that hook fall back to host evaluation for correctness.
Can heaviside participate in fusion?⌄
Yes. The fusion planner emits an elementwise step expression with explicit zero and NaN handling.
Related Math functions
Elementwise
abs · angle · complex · conj · double · exp · expm1 · factorial · gamma · 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
Structure
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how heaviside is executed, line by line, in Rust.
- View the source for heaviside in Rust on GitHub
- Learn how the RunMat 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 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.