atan — Element-wise inverse tangent matching MATLAB semantics for real, logical, character, and complex inputs with GPU fallbacks.
Y = atan(X) computes the inverse tangent (in radians) of every element of X. The operation runs element-wise, supports real and complex numbers, and honors MATLAB's broadcasting semantics.
How does the atan function behave in MATLAB / RunMat?
- Works on scalars, vectors, matrices, and N-D tensors, including logical and integer inputs (which are promoted to double precision before evaluation).
- Character arrays are interpreted as their Unicode code points and return dense double arrays of identical shape.
- Complex inputs follow MATLAB's analytic extension
atan(z) = (1/(2i)) * (log(1 + i z) - log(1 - i z)), ensuring identical branch behavior and NaN/Inf propagation. - Returns results in radians for real inputs and complex numbers for complex inputs.
- Empty arrays and already scalar values pass straight through while preserving shape.
GPU behavior
When RunMat Accelerate is active and the selected provider implements unary_atan, the runtime keeps tensors on the GPU and executes the kernel entirely on-device.
If the provider lacks unary_atan (or declines the request), RunMat gathers the data to the host, computes the result with the reference implementation, and then reapplies residency requests such as 'like' prototypes automatically.
Fusion planning groups neighboring elementwise operations so downstream kernels can stay on the GPU even if atan itself fell back to the CPU.
GPU residency
Usually you do **not** need to call gpuArray. When a provider exposes unary_atan, the runtime keeps data on the GPU automatically. If the hook is missing (or if you supply a host 'like' prototype), RunMat gathers and computes on the CPU, then honors the requested residency so you do not have to manage transfers manually.
Examples of using atan in MATLAB / RunMat
Arctangent of a scalar
y = atan(1)Expected output:
y = 0.7854Element-wise arctangent of a vector
x = linspace(-2, 2, 5);
angles = atan(x)Expected output:
angles = [-1.1071 -0.7854 0 0.7854 1.1071]Computing inverse tangent of every entry in a matrix
A = [-2 -1 0; 1 2 3];
Y = atan(A)Expected output:
Y =
-1.1071 -0.7854 0
0.7854 1.1071 1.2490Evaluating atan on a complex number
z = 1 + 2i;
w = atan(z)Expected output:
w = 1.3390 + 0.4024iKeeping the result on the GPU with 'like'
proto = gpuArray.zeros(1, 1);
G = gpuArray([-3 -1 0 1 3]);
deviceResult = atan(G, 'like', proto);
result = gather(deviceResult)Expected output:
result =
-1.2490 -0.7854 0 0.7854 1.2490Inspecting character codes via inverse tangent
codes = atan('RUN')Expected output:
codes =
1.5586 1.5590 1.5580FAQ
When should I use atan?
Use atan whenever you need the element-wise inverse tangent of data expressed in radians—common in signal processing, control systems, and geometric computations.
How is atan different from atan2?
atan accepts a single input and returns results in (-π/2, π/2). atan2(y, x) takes two inputs, uses the signs of both arguments to determine the correct quadrant, and returns values in (-π, π].
Does atan support complex numbers?
Yes. Results match MATLAB's analytic continuation of the inverse tangent and may return complex values even for real inputs when the computation requires it.
Can I keep results on the GPU?
Yes. Passing 'like', prototype with a GPU tensor keeps outputs on-device whenever a provider is registered. When the provider lacks unary_atan, RunMat computes on the host and returns host data unless you explicitly request GPU residency via 'like'.
What happens with NaN or Inf inputs?
NaNs propagate, and infinities map to the appropriate asymptotic limits (atan(±Inf) = ±π/2), matching MATLAB's IEEE behavior.
See also
atan2, tan, asin, acos, gpuArray, gather
Source & Feedback
- Source code: `crates/runmat-runtime/src/builtins/math/trigonometry/atan.rs`
- Found a bug? Open an issue with a minimal reproduction.