atan — Element-wise inverse tangent in MATLAB and RunMat.
Y = atan(X) computes the inverse tangent (in radians) of each element in X. It supports real and complex values, and "like" options control output class/residency in MATLAB- and RunMat-compatible forms.
Syntax
Y = atan(X)
Y = atan(X, "like", P)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
X | Any | Yes | — | Input scalar, array, char array, complex value, or gpuArray. |
like | StringScalar | Yes | "like" | Output template selector keyword. |
P | LikePrototype | Yes | — | Prototype determining host/gpu residency and real/complex output class. |
Returns
| Name | Type | Description |
|---|---|---|
Y | Any | Element-wise inverse tangent result. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:atan:InvalidInput | Input cannot be interpreted as supported numeric/char/complex data. | atan: invalid input |
RunMat:atan:InvalidOption | Optional arguments after X are malformed or unsupported. | atan: invalid option |
RunMat:atan:ArgCount | Too many input arguments were supplied. | atan: too many input arguments |
RunMat:atan:LikePrototype | The "like" prototype or requested output class is unsupported. | atan: invalid "like" prototype |
RunMat:atan:GpuUnavailable | GPU output was requested via "like" but no active provider is available. | atan: GPU provider unavailable |
RunMat:atan:Internal | Internal gather/conversion/allocation/provider flow failed. | atan: internal error |
How atan works
- 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.
Does RunMat run atan on the GPU?
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 memory and 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
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.5580Using atan with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how atan changes the result.
Run a small atan example, explain the result, then change one input and compare the output.
FAQ
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.
Related Math functions
Trigonometry
acos · acosh · asin · asinh · atan2 · 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
Structure
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how atan is executed, line by line, in Rust.
- View the source for atan in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
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.