sign — Return element-wise sign values for real and complex numeric inputs.
y = sign(x) returns the sign of each element of x. Real values map to -1, 0, or 1, while complex values are normalized as x ./ abs(x), matching MATLAB behavior.
Syntax
Y = sign(X)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
X | Any | Yes | — | Real or complex numeric input. |
Returns
| Name | Type | Description |
|---|---|---|
Y | NumericArray | Elementwise sign result. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:sign:InvalidInput | Input is not a supported numeric/logical/character value. | sign: invalid input |
RunMat:sign:Internal | Internal gather/provider/tensor construction failed. | sign: internal error |
How sign works
- Real scalars, vectors, matrices, and higher-dimensional tensors produce
-1,0, or1for each element. - Complex inputs return
x ./ abs(x); zero-valued elements remain exactly0 + 0i. - Logical inputs are promoted to doubles before applying the sign function.
- Character arrays are treated as their numeric code points and return doubles of the same shape.
- NaN inputs propagate (
sign(NaN)isNaN), matching MATLAB semantics. Infand-Infmap to1and-1respectively; complex numbers with infinite parts normalise accordingly.
Does RunMat run sign on the GPU?
Hook available: The sign is evaluated directly on the device with no host transfers, including complex normalization as x ./ abs(x).
Hook missing or unsupported dtype: RunMat gathers the tensor, applies the CPU logic (including complex handling), and continues execution transparently.
GPU memory and residency
sign keeps real and complex-interleaved GPU tensors resident when the active provider exposes unary_sign. Complex gpuArray inputs return complex-interleaved gpuArray outputs.
Examples
Determining the sign of a scalar
result = sign(-42)Expected output:
result = -1Applying sign to a vector of mixed values
v = [-3 -0.0 0 2 5];
s = sign(v)Expected output:
s = [-1 0 0 1 1]Normalising complex numbers to unit magnitude
z = [3+4i, -1+1i, 0+0i];
u = sign(z)Expected output:
u = [0.6+0.8i, -0.7071+0.7071i, 0]Using sign with character data
codes = sign('RunMat')Expected output:
codes = [1 1 1 1 1 1]Working with logical masks
mask = [false true false; true false true];
numeric = sign(mask)Expected output:
numeric = [0 1 0; 1 0 1]Executing sign on a GPU-resident tensor
G = randn(4096, 4096, 'gpuArray');
S = sign(G)Handling infinities and NaNs
values = [Inf, -Inf, NaN, 0];
out = sign(values)Expected output:
out = [1 -1 NaN 0]Using sign with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how sign changes the result.
Run a small sign example, explain the result, then change one input and compare the output.
FAQ
Does sign modify NaN values?⌄
No. NaN inputs remain NaN, matching IEEE behaviour and MATLAB semantics.
How does sign handle complex zeros?⌄
0 + 0i stays 0 + 0i. Other complex values are scaled to lie on the unit circle (x ./ abs(x)).
What happens for infinite complex components?⌄
If either component is infinite, RunMat returns a direction vector with unit magnitude (e.g., 1 + 0i or ±1/√2 ± 1/√2 i), mirroring MATLAB.
Can I call sign on string arrays?⌄
No. sign accepts numeric, logical, or character arrays. Use double(string) followed by sign if needed.
Does sign allocate a new array?⌄
Yes. The builtin returns a fresh array; downstream fusion may combine operations to reduce allocations.
Is GPU execution numerically identical to CPU?⌄
Results match within the provider's precision (single or double). NaN propagation and zero handling remain consistent between CPU and GPU paths.
Will sign participate in fusion?⌄
Yes. The fusion planner can fold sign into neighbouring elementwise kernels, keeping data on the GPU when possible.
How do I keep results on the GPU?⌄
Avoid gather unless host data is required. RunMat keeps the outputs of fused expressions device-resident when beneficial.
Related Math functions
Elementwise
abs · angle · complex · conj · double · exp · expm1 · factorial · gamma · heaviside · hypot · imag · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · plus · pow2 · power · rdivide · real · 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 sign is executed, line by line, in Rust.
- View the source for sign 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.