What does the tan function do in MATLAB / RunMat?
y = tan(x) computes the tangent of every element in x, interpreting each input value in radians.
How does the tan function behave in MATLAB / RunMat?
- Operates on scalars, vectors, matrices, and N-D tensors while respecting MATLAB's implicit-expansion (broadcasting) rules.
- Logical and integer inputs are promoted to double precision (
true → 1.0,false → 0.0) before the tangent is applied so downstream arithmetic mirrors MATLAB. - Character arrays operate on their Unicode code points and return dense double arrays of identical shape.
- Complex inputs follow MATLAB’s analytic extension
tan(a + bi) = sin(2a)/(cos(2a) + cosh(2b)) + i·sinh(2b)/(cos(2a) + cosh(2b)), propagatingNaN/Infcomponents independently. - Appending
'like', prototypemirrors the prototype’s class and residency for real numeric prototypes; complex prototypes currently raise a descriptive error so you can fall back to the default output rules. tanaccepts both host tensors andgpuArrayinputs. GPU residency is preserved whenever the active provider exposes theunary_tankernel or a fused elementwise implementation.- Strings and string arrays are rejected to match MATLAB’s numeric-only contract for the trigonometric family.
- Empty inputs, singleton dimensions, and already-reduced shapes pass through unchanged to avoid unnecessary allocations.
tan Function GPU Execution Behaviour
- When RunMat Accelerate is active and the selected provider implements
unary_tan, the operation executes entirely on the GPU, keeping inputs and outputs resident in device memory. - If the provider declines the request, RunMat gathers the data to the host, evaluates the tangent with the reference implementation, and reapplies any residency requests (including
'like'prototypes that expect GPU outputs) before returning. - Fusion planning groups neighbouring elementwise operators, so even when a fallback occurs, subsequent GPU-capable steps can resume on-device without redundant transfers.
Examples of using the tan function in MATLAB / RunMat
Getting the tangent of 45 degrees expressed in radians
y = tan(pi/4);
Expected output:
y = 1
Applying tangent to a vector of sample angles
theta = linspace(-pi/2 + 0.1, pi/2 - 0.1, 5);
wave = tan(theta);
Expected output (approximate):
wave = [-9.9666 -0.9047 0 0.9047 9.9666]
Evaluating the tangent of a matrix on the GPU
G = gpuArray([0 pi/6; pi/4 pi/3]);
T = tan(G);
result = gather(T);
Expected output:
result =
0 0.5774
1.0000 1.7321
Computing the tangent of complex angles
z = 1 + 0.5i;
tz = tan(z);
Expected output (approximate):
tz = 0.9654 + 0.2718i
Using tangent to linearise small-angle approximations
eps = [-1e-6 0 1e-6];
approx = tan(eps);
Expected output:
approx = [-1.0000e-06 0 1.0000e-06]
Converting degrees to radians before using tan
angles_in_deg = [0 30 60 89];
radians = deg2rad(angles_in_deg);
result = tan(radians);
Expected output (approximate):
result = [0 0.5774 1.7321 57.2899]
Keeping the result on the GPU with 'like'
proto = gpuArray.zeros(1, 1, 'single');
angles = gpuArray([0 pi/6 pi/4]);
deviceResult = tan(angles, 'like', proto);
gathered = gather(deviceResult);
Expected output:
gathered =
1x3 single
0 0.5774 1.0000
Inspecting character codes with tangent
codes = tan('ABC');
Expected output (approximate):
codes =
-1.4700 0.0266 1.6523
GPU residency in RunMat (Do I need gpuArray?)
You usually do not need to call gpuArray explicitly. The fusion planner keeps tensors on the GPU whenever the active provider exposes the necessary kernels (such as unary_tan). Manual gpuArray / gather calls remain supported for MATLAB compatibility or when you need to pin residency before interacting with external code.
FAQ
When should I use the tan function?
Use tan whenever you need the tangent of angles expressed in radians—whether you are modelling waves, evaluating transfer functions, or analysing geometric relationships element-wise across large arrays.
What happens near odd multiples of pi/2?
Values that approach (2k + 1)·pi/2 grow rapidly toward ±Inf, just like in MATLAB. RunMat preserves IEEE semantics, so you may observe very large magnitudes or Inf/-Inf where poles occur.
Does tan support complex numbers?
Yes. RunMat evaluates the analytic extension described above, handling real and imaginary components independently and propagating NaN/Inf values exactly as MATLAB does.
Can I keep the result on the GPU?
Yes. When a provider implements unary_tan, the result never leaves the device. If a fallback is required, RunMat gathers to the host, computes the result, and reapplies residency requests—including 'like' outputs—before handing the value back.
How do I control units?
tan operates in radians. Convert degrees with deg2rad (or multiply by pi/180) before calling tan, or use MATLAB’s degree-specific builtins (tand, etc.) when available.
Does tan change the input type?
By default results are double precision. Passing 'like', prototype preserves the prototype’s real numeric type (host or GPU). Complex prototypes are currently unsupported and raise a clear error.
Are logical arrays supported?
Yes. Logical arrays are promoted to 0.0/1.0 doubles before evaluation so downstream workflows remain compatible with MATLAB semantics.
Can I fuse tan with other elementwise operations?
Definitely. The fusion planner emits WGSL tan calls for GPU execution, and providers may specialise fused kernels for additional throughput.
See Also
Source & Feedback
- The full source code for the implementation of the
tanfunction is available at:crates/runmat-runtime/src/builtins/math/trigonometry/tan.rs - Found a bug or behavioural difference? Please open an issue with details and a minimal repro.