RunMat
GitHub

atan2 — Quadrant-aware inverse tangent atan2(y, x) in MATLAB and RunMat.

theta = atan2(y, x) computes the four-quadrant inverse tangent of point (x, y). The result is in radians over (-pi, pi], with element-wise scalar expansion behavior that matches MATLAB and RunMat.

Syntax

Z = atan2(Y, X)

Inputs

NameTypeRequiredDefaultDescription
YAnyYesNumerator operand.
XAnyYesDenominator operand.

Returns

NameTypeDescription
ZNumericArrayQuadrant-aware inverse tangent result.

Errors

IdentifierWhenMessage
RunMat:atan2:InvalidInputAn input cannot be interpreted as supported numeric/logical/char data.atan2: invalid input
RunMat:atan2:ComplexUnsupportedAt least one operand is complex.atan2: complex inputs are not supported
RunMat:atan2:SizeMismatchInput operands are not broadcast-compatible.atan2: size mismatch
RunMat:atan2:InternalInternal gather/conversion/allocation/provider flow failed.atan2: internal error

How atan2 works

  • Inputs can be scalars, vectors, matrices, or N-D tensors of real numeric, logical, or character data. MATLAB-style implicit expansion (broadcasting) applies when shapes are compatible.
  • atan2 treats y as the numerator and x as the denominator: atan2(Y, X) equals atan(Y ./ X) but keeps the correct quadrant and handles zero denominators.
  • Logical inputs are cast to double, character arrays use their Unicode code points, and integer inputs are promoted to double, mirroring MATLAB promotion rules.
  • Complex inputs are not supported; MATLAB raises an error and RunMat matches that behaviour.
  • atan2(0, 0) returns 0. atan2(±0, -0) follows IEEE-754 semantics, matching MATLAB for signed zeros and infinities.
  • atan2(NaN, x) or atan2(y, NaN) returns NaN; inputs containing Inf combinations follow IEEE-754 quadrant semantics exactly like MATLAB.
  • The output always has double precision and the same size as the broadcasted inputs.

Does RunMat run atan2 on the GPU?

When both operands already reside on the GPU and the active provider implements the elem_atan2 hook, RunMat executes the operation entirely on the device without reformatting buffers.

If shapes require implicit expansion or the provider lacks elem_atan2, RunMat transparently gathers both tensors to the host, computes the result with the reference CPU implementation, and continues execution.

When GPU work completes in single precision (because the provider only exposes 32-bit buffers), RunMat promotes the results to double precision whenever the data is materialised on the host so the observable behaviour still matches MATLAB.

Fusion-aware expressions (for example, sin(atan2(y, x))) can still emit a combined WGSL kernel; the fusion planner recognises atan2 as a binary elementwise primitive.

GPU memory and residency

RunMat's planner keeps tensors on the GPU whenever profitable. Explicit gpuArray calls are optional—use them only when you need to control residency for interoperability. When elem_atan2 is unavailable, RunMat automatically gathers data to the CPU, performs the computation, and re-uploads results only when downstream consumers demand GPU residency.

Examples

Computing the polar angle of a point

theta = atan2(4, 3)

Expected output:

theta = 0.9273

Determining quadrants for a vector of coordinates

Y = [-1 0 1];
X = [-1 -1 -1];
angles = atan2(Y, X)

Expected output:

angles = [-2.3562 3.1416 2.3562]

Broadcasting a scalar denominator across a matrix

A = [1 2 3; 4 5 6];
angles = atan2(A, 2)

Expected output:

angles =
    0.4636    0.7854    0.9828
    1.1071    1.1903    1.2490

Handling zero numerators and signed zeros

theta = atan2([0 -0], [-2 0])

Expected output:

theta = [pi 0]

Executing atan2 on the GPU

Gy = gpuArray([1 1; -1 -1]);
Gx = gpuArray([1 -1; 1 -1]);
angles_gpu = atan2(Gy, Gx);
angles = gather(angles_gpu)

Expected output:

angles =
    0.7854    2.3562
   -0.7854   -2.3562

Converting character data to angles

theta = atan2('A', 100)

Expected output:

theta = 0.5764

Using atan2 with coding agents

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

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

FAQ

What is the range of values returned by atan2?

Angles are given in radians and span the open/closed interval (-pi, pi]. Use rad2deg if you prefer degrees.

Can I supply complex inputs?

No. MATLAB raises an error for complex inputs, and so does RunMat. Convert complex data to magnitude/phase first if needed.

Does atan2 preserve the shape of the inputs?

Yes. After implicit expansion, the output shape matches the broadcasted size of Y and X.

How are logical or character inputs handled?

Logical values map to 0 and 1, and character arrays use their Unicode code point values (as doubles) before computing the angle.

What happens when x is zero?

atan2 still returns a finite result using the sign of y. For example, atan2(1, 0) returns pi/2, and atan2(-1, 0) returns -pi/2.

Are GPU and CPU results identical?

Double-precision providers match CPU results exactly. Single-precision providers can differ by routine IEEE rounding. RunMat automatically promotes to double when materialising host tensors to mirror MATLAB.

How can I compute angles in degrees?

Call rad2deg(atan2(y, x)) or multiply the result by 180/pi.

Trigonometry

acos · acosh · asin · asinh · atan · atanh · cos · cosd · cosh · deg2rad · rad2deg · sin · sind · sinh · tan · tand · tanh

Elementwise

abs · angle · 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

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 atan2 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.