conj — Compute complex conjugates element-wise in MATLAB and RunMat.

conj(x) negates the imaginary component of each element in x, while real values remain unchanged. The operation is element-wise for scalars and arrays, matching MATLAB behavior.

Syntax

Y = conj(X)

Inputs

NameTypeRequiredDefaultDescription
XAnyYesNumeric, logical, char, or complex input.

Returns

NameTypeDescription
YNumericArrayComplex conjugate of X.

Errors

IdentifierWhenMessage
RunMat:conj:InvalidInputInput cannot be interpreted as numeric, logical, char, or complex data.conj: invalid input
RunMat:conj:InternalInternal tensor conversion/allocation/provider interaction failed.conj: internal error

How conj works

  • Complex scalars and arrays have their imaginary components multiplied by -1; when a result has no imaginary part it collapses to a real scalar or tensor just like MATLAB.
  • Purely real numeric inputs (double, single, integer) are returned unchanged.
  • Logical arrays are promoted to double precision with true → 1.0 and false → 0.0.
  • Character arrays are promoted to double precision containing their Unicode code points; the Conjugate operation does not alter the codes because they are real-valued.
  • String arrays are not supported and raise an error.

Does RunMat run conj on the GPU?

Hook available: Real tensors stay on the GPU and are processed in-place (both the in-process provider used for tests and the WGPU provider expose this path).

Fusion and auto-offload: Because conj is tagged as an elementwise unary builtin, the fusion planner treats it as a pass-through for real-valued kernels. Native auto-offload therefore keeps fused expressions resident on the GPU whenever the surrounding ops are profitable.

Hook missing or complex input: RunMat gathers the data to host memory, applies the CPU semantics (including complex negation and type promotion), and returns the result. The planner may re-upload tensors later if profitable.

Complex tensors are currently materialised on the host because GPU-side complex layouts are still under development. Providers can add native complex kernels later without changing this builtin.

GPU memory and residency

You usually do not need to call gpuArray explicitly. RunMat's fusion planner and Accelerate layer manage residency and offload decisions automatically, keeping tensors on the GPU whenever device execution is beneficial. Explicit gpuArray and gather remain available for MATLAB compatibility or fine-grained residency control.

Examples

Complex conjugate of a scalar value in MATLAB

z = 3 + 4i;
result = conj(z)

Expected output:

result = 3 - 4i

Apply conj to every element of a complex matrix

Z = [1+2i, 4-3i; -5+0i, 7+8i];
C = conj(Z)

Expected output:

C =
   1 - 2i   4 + 3i
  -5 + 0i   7 - 8i

Ensure conj leaves real inputs unchanged

data = [-2.5 0 9.75];
unchanged = conj(data)

Expected output:

unchanged = [-2.5 0 9.75]

Use conj on logical masks converted to doubles

mask = logical([0 1 0; 1 1 0]);
numeric = conj(mask)

Expected output:

numeric =
     0     1     0
     1     1     0

Convert MATLAB char arrays to numeric codes with conj

chars = 'RunMat';
codes = conj(chars)
outputClass = class(codes)

Expected output:

codes = [82 117 110 77 97 116];
outputClass = 'double'

Compute conjugate on GPU-resident arrays

G = rand(4096, 256, "gpuArray");
H = conj(G)

Using conj with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how conj changes the result.

Run a small conj example, explain the result, then change one input and compare the output.

FAQ

Does conj change purely real inputs?

No. Real values (including logical and character data) are returned unchanged, although logical and character inputs become double precision arrays just like in MATLAB.

How does conj handle complex zeros?

conj(0 + 0i) returns 0. Imaginary zeros remain zero after negation.

Can I call conj on string arrays?

No. The builtin only accepts numeric, logical, or character inputs. Convert strings with double(string) if you need numeric codes.

Does conj allocate a new array?

Yes. The builtin materialises a new tensor (or scalar). Fusion may eliminate the allocation when the surrounding expression can be fused safely, especially when the data stays on the GPU.

What happens on the GPU without unary_conj?

RunMat gathers the tensor to host memory, applies the CPU semantics (including complex negation), and allows later operations to re-upload data if advantageous.

Is GPU execution numerically identical to CPU?

Yes. For real tensors the result is an exact copy; the conjugate matches CPU results bit-for-bit for supported precisions.

Does conj participate in fusion?

Yes. The fusion planner can fold conj into neighbouring elementwise kernels, letting providers keep tensors on the GPU whenever possible.

Elementwise

abs · angle · complex · double · exp · expm1 · factorial · gamma · heaviside · 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

Rounding

ceil · fix · floor · mod · rem · round

Factor

chol · eig · lu · qr · svd

Solve

cond · det · inv · linsolve · norm · null · pinv · rank · rcond · rref

Symbolic

digits · int · limit · sym · syms · vpa

Fft

fft · fft2 · fftshift · ifft · ifft2 · ifftshift

Interpolation

interp1 · interp2 · pchip · ppval · spline

Ode

ode15s · ode23 · ode45

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how conj is executed, line by line, in Rust.

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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.