atan2_simd

Function atan2_simd 

Source
pub fn atan2_simd<F>(y: &ArrayView1<'_, F>, x: &ArrayView1<'_, F>) -> Array1<F>
where F: Float + SimdUnifiedOps,
Expand description

Compute the two-argument arctangent element-wise (SIMD-accelerated).

Computes atan2(y, x) for each pair of elements, returning values in the range (-π, π]. This function correctly handles the signs of both arguments to determine the quadrant.

§Arguments

  • y - Y-coordinates (sine component)
  • x - X-coordinates (cosine component)

§Returns

Array1<F> with the same length as inputs, where each element is the angle in radians from the positive x-axis to the point (x, y).

§Performance

  • SIMD: Automatically used for large arrays (1000+ elements)
  • Scalar: Used for small arrays or when SIMD is unavailable

§Mathematical Properties

  • Range: (-π, π]
  • atan2(0, 0) = 0 (by convention)
  • atan2(y, x) = atan(y/x) when x > 0
  • atan2(y, 0) = π/2 * sign(y) when x = 0
  • atan2(-y, x) = -atan2(y, x)
  • atan2(y, -x) = π - atan2(y, x) when y >= 0
  • atan2(y, -x) = -π + atan2(y, x) when y < 0

§Quadrants

  • Quadrant I (x > 0, y > 0): (0, π/2)
  • Quadrant II (x < 0, y > 0): (π/2, π)
  • Quadrant III (x < 0, y < 0): (-π, -π/2)
  • Quadrant IV (x > 0, y < 0): (-π/2, 0)

§Examples

use scirs2_core::ndarray::array;
use scirs2_core::ndarray_ext::elementwise::atan2_simd;

let y = array![1.0, 1.0, -1.0, -1.0];
let x = array![1.0_f64, -1.0, -1.0, 1.0];
let angles = atan2_simd(&y.view(), &x.view());
// Result: [π/4, 3π/4, -3π/4, -π/4]

§Applications

  • Robotics: Joint angle calculations, path planning
  • Computer Vision: Feature orientation, optical flow
  • Navigation: Heading and bearing calculations
  • Spatial Computing: Coordinate transformations
  • Physics: Vector angle calculations, force analysis
  • Computer Graphics: Rotation angles, sprite orientation
  • Signal Processing: Phase calculations, complex number arguments

§See Also