RunMat
GitHub

real — Extract the real part of numeric, logical, character, or complex arrays.

real(x) returns the real component of each element in x. Real-valued inputs pass through unchanged, while complex inputs drop their imaginary component with MATLAB-compatible typing rules.

Syntax

Y = real(X)

Inputs

NameTypeRequiredDefaultDescription
XAnyYesNumeric, logical, char, or complex input.

Returns

NameTypeDescription
YNumericArrayReal component of X.

Errors

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

How real works

  • Complex scalars, vectors, matrices, and higher-dimensional tensors return only their real parts.
  • Real inputs (double, single, logical) are preserved exactly, but results are materialised as double precision when necessary (e.g., logical masks become doubles).
  • Character arrays are converted to their numeric code points and returned as doubles, matching MATLAB semantics.
  • String arrays are not supported and raise an error (real expects numeric, logical, or character data).
  • Sparse arrays are currently densified (sparse support is planned).

Does RunMat run real on the GPU?

Hook available: The real part is taken directly on the device with no host transfers (the WGPU provider and the in-process provider both implement this hook).

Hook missing or unsupported dtype: RunMat gathers the tensor to host memory, applies the CPU semantics (including logical and character promotion), and hands control back to the caller. Downstream fusion can still re-upload if the planner decides it is profitable.

When a provider supports complex-interleaved storage, real extracts the real lane on the device and returns a real GPU tensor with the original shape.

GPU memory and residency

You usually do not need to call gpuArray explicitly. RunMat's fusion planner and Accelerate layer track residency automatically, keeping tensors on the GPU whenever device execution is beneficial. Explicit gpuArray / gather calls remain available for MATLAB compatibility or when you need deterministic residency control (e.g., integrating with third-party GPU kernels).

Examples

Extract the real part of a complex scalar

z = 3 + 4i;
r = real(z)

Expected output:

r = 3

Take the real part of a complex matrix

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

Expected output:

R =
     1     4
    -5     7

Confirm that real inputs remain unchanged

data = [-2.5 0 9.75];
result = real(data)

Expected output:

result = [-2.5 0 9.75]

Convert logical masks to doubles

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

Expected output:

numeric =
     0     1     0
     1     1     0

Convert characters to numeric codes

chars = 'RunMat';
codes = real(chars)

Expected output:

codes = [82 117 110 77 97 116]

Keep GPU tensors device-resident when possible

G = rand(4096, 512, "gpuArray");
R = real(G)

Using real with coding agents

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

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

FAQ

Does real change purely real inputs?

No. Real, logical, and character inputs are preserved numerically, though logical and character values are returned as doubles to match MATLAB.

How does real handle complex zeros?

real(0 + 0i) returns exactly 0. Imaginary zeros are simply discarded.

Can I call real on string arrays?

No. Like MATLAB, real only accepts numeric, logical, or character arrays. Converting strings with double(string) first will produce numeric codes if required.

Does real allocate a new array?

Yes. The builtin returns a new tensor (or scalar). Fusion may combine kernels to avoid materialising intermediates when safe.

What happens on the GPU without unary_real?

RunMat gathers the tensor to host memory, applies the CPU semantics, and allows subsequent operations to re-upload if profitable. Providers are encouraged to implement unary_real for zero-copy behaviour.

Is GPU execution numerically identical to CPU?

Yes—real is an identity for real tensors. Results match within the provider's precision (single or double).

Does real participate in fusion?

Yes. The fusion planner can fold real into neighbouring elementwise kernels, keeping data on the GPU when possible.

Elementwise

abs · angle · complex · conj · double · exp · expm1 · factorial · gamma · heaviside · hypot · imag · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · plus · pow2 · power · rdivide · 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 real 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.