sign — Sign of scalars, vectors, matrices, or N-D tensors with real or complex values.
y = sign(x) returns the sign of each element of x. Real inputs become -1, 0, or 1 depending on their value, while complex inputs are normalised to unit magnitude (x ./ abs(x)).
How does the sign function behave in MATLAB / RunMat?
- 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.
GPU behavior
**Hook available:** The sign is evaluated directly on the device with no host transfers.
**Hook missing or unsupported dtype:** RunMat gathers the tensor, applies the CPU logic (including complex handling), and continues execution transparently.
GPU residency
You usually do **not** need to call gpuArray manually. RunMat's planner tracks residency and keeps tensors on the GPU whenever it is profitable. Explicit gpuArray / gather calls remain available for MATLAB compatibility or interoperability with external GPU code.
Examples of using sign in MATLAB / RunMat
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]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.
See also
abs, sin, sum, gpuArray, gather
Source & Feedback
- Source code: `crates/runmat-runtime/src/builtins/math/elementwise/sign.rs`
- Found a bug? Open an issue with a minimal reproduction.