RunMat
GitHub

angle — Phase angle (argument) of real and complex values in MATLAB and RunMat.

angle(x) returns the phase angle (argument) of each element in x, in radians. Complex inputs use atan2(imag(x), real(x)), and real-valued arrays are handled element-wise with MATLAB/RunMat-compatible results.

Syntax

theta = angle(X)

Inputs

NameTypeRequiredDefaultDescription
XAnyYesNumeric, logical, char, or complex input.

Returns

NameTypeDescription
thetaNumericArrayPhase angle in radians.

Errors

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

How angle works

  • Complex inputs use atan2(imag(x), real(x)), yielding values in [-π, π].
  • Real inputs are treated as complex numbers with zero imaginary part. Positive numbers map to 0, negative numbers map to π, and zeros return 0.
  • Logical inputs are promoted to doubles (true → 1, false → 0) before computing the angle.
  • Results are always returned as dense double-precision arrays that preserve the shape of the input.
  • Character arrays are interpreted as their numeric code points and produce double arrays of the same size.
  • String arrays are unsupported and raise an error, mirroring MATLAB.
  • NaN propagation follows IEEE rules: angle(NaN) returns NaN.

Does RunMat run angle on the GPU?

When RunMat Accelerate is active, tensors that already reside on the GPU remain on the device. Providers that implement the unary_angle hook execute the phase computation directly on the GPU, using atan2(0, real) for real storage and atan2(imag, real) for complex-interleaved storage. If the hook is missing or an input requires host-only conversion, RunMat gathers the data, evaluates the phase angle on the CPU, and then honors any downstream fusion or residency decisions automatically.

GPU memory and residency

You usually do not need to call gpuArray explicitly. RunMat's fusion planner and Accelerate layer manage residency automatically, keeping tensors on the GPU whenever device execution is advantageous. Explicit gpuArray / gather calls remain available for MATLAB compatibility or when you need deterministic residency control.

Examples

Computing the phase of a complex scalar

z = 3 + 4i;
theta = angle(z);
disp(theta)

Expected output:

theta = 0.9273

Extracting angles from a complex vector

Z = [1+1i, -1+1i, -1-1i, 1-1i];
phases = angle(Z);
disp(phases)

Expected output:

phases = [0.7854 2.3562 -2.3562 -0.7854]

Determining angles of negative real numbers

vals = [-2 -1 0 1 2];
phi = angle(vals);
disp(phi)

Expected output:

phi = [3.1416 3.1416 0 0 0]

Working with GPU-resident arrays

G = gpuArray([1 -1; -1 1]);
theta = gather(angle(G));
disp(theta)

Expected output:

theta =

     0.0000    3.1416
     3.1416    0.0000

Angles of character code arrays

C = ['A' 'B'; 'C' 'D'];
codes = angle(C);
disp(codes)

Expected output:

codes = [0 0; 0 0]

Angles from logical masks

mask = logical([0 1; 1 0]);
phaseMask = angle(mask);
disp(phaseMask)

Expected output:

phaseMask = [0 0; 0 0]

Using angle with coding agents

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

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

FAQ

Does angle return values in radians?

Yes. Results are reported in radians within the interval [-π, π], matching MATLAB's definition.

What happens with zeros?

angle(0) returns 0. Complex zeros (0 + 0i) also yield 0.

How does angle handle NaN inputs?

NaNs propagate. angle(NaN) evaluates to NaN, honoring IEEE arithmetic.

Can I pass string arrays to angle?

No. Like MATLAB, angle supports numeric, logical, or character data. Convert strings with double(string) if you need numeric codes first.

Does angle allocate a new array?

Yes. The builtin produces a dense double array. Fusion may eliminate the allocation when the surrounding expression can be fused safely.

What about GPU execution for complex tensors?

Host complex tensors execute on the CPU. Complex-interleaved gpuArrays stay GPU-resident when the active provider implements unary_angle, computing the phase angle directly on device.

Will GPU results match CPU results exactly?

Yes for double-precision providers. Single-precision backends may exhibit minor rounding differences, but they remain within typical IEEE tolerance.

Elementwise

abs · complex · conj · 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 angle 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.