complex — Construct complex double values from real and imaginary parts.
z = complex(a, b) constructs a + 1i*b from real-valued inputs. z = complex(a) returns a using complex storage with a zero imaginary part, and existing complex inputs pass through unchanged.
How complex works
- With two inputs,
aandbmust be real numeric values. Complex inputs raise an error. - Two non-scalar inputs must have the same size. If either input is scalar, it expands across the other input.
- Integer and logical inputs are promoted to double precision before constructing the complex result.
- String and character inputs are rejected because the constructor accepts numeric inputs only.
- Unary
complex(a)preserves existing complex scalars and complex tensors. Real inputs become complex values with zero imaginary parts. - Empty real tensors preserve their empty shape and return empty complex tensors.
How RunMat runs complex on the GPU
RunMat rejects gpuArray inputs for complex today because the active GPU providers expose real-valued tensor buffers, while this constructor creates complex storage. Native complex GPU residency can be added later without changing the public call forms.
GPU memory and residency
No. complex returns host-resident Value::Complex or Value::ComplexTensor values today. Use gather before construction if values are currently device-resident.
Examples
Constructing a complex scalar from real and imaginary parts
z = complex(3, 4)Expected output:
z = 3+4iConstructing a complex row vector from matching real arrays
re = [1 2 3];
im = [4 5 6];
z = complex(re, im)Expected output:
z = [1+4i 2+5i 3+6i]Expanding a scalar imaginary part across an array
re = [1 2 3];
z = complex(re, -1)Expected output:
z = [1-1i 2-1i 3-1i]Forcing complex storage with a zero imaginary part
z = complex(12);
tf = isreal(z)Expected output:
z = 12+0i
tf =
0Leaving an existing complex value unchanged
z = complex(1 + 2i)Expected output:
z = 1+2iRejecting non-scalar implicit expansion
z = complex([1 2 3], [10; 20])Expected output:
error: complex: real and imaginary parts must have the same size, unless one input is scalarFAQ
Does complex(a, b) use MATLAB implicit expansion?⌄
Only scalar expansion is supported for this constructor. Two non-scalar operands must have identical sizes, matching MATLAB's documented complex size rules.
Can I pass complex values to complex(a, b)?⌄
No. The two-argument constructor requires real numeric inputs for both the real and imaginary parts. Use real, imag, or arithmetic if you need to transform existing complex values.
What does unary complex(a) do?⌄
It forces real input into complex storage with a zero imaginary part. If a is already complex, RunMat returns it unchanged.
Why does isreal(complex(5)) return false?⌄
complex(5) stores the value as complex double data with an explicit zero imaginary part. MATLAB and RunMat define isreal by storage, so it returns false.
Does complex accept logical inputs?⌄
Yes. RunMat promotes logical values to doubles (true becomes 1, false becomes 0) before constructing the complex output.
Does complex accept strings or character arrays?⌄
No. Strings and character arrays are rejected because the constructor is limited to numeric inputs.
Can complex run on GPU arrays?⌄
Not yet. RunMat's complex tensor representation is currently host-resident, so this builtin does not accept gpuArray inputs.
Related Math functions
Elementwise
abs · angle · 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 complex works, line by line, in Rust.
- View complex.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.