sqrt_simd

Function sqrt_simd 

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

Compute the square root of each element (SIMD-accelerated).

Computes √x for each element in the array.

§Arguments

  • x - Input 1D array (must contain non-negative values)

§Returns

Array1<F> with the same length as input, with square roots.

§Performance

  • SIMD: Automatically used for large arrays (1000+ elements)
  • Scalar: Used for small arrays or when SIMD unavailable
  • Speedup: 2-4x for large f32 arrays on AVX2 systems

§Mathematical Definition

sqrt(x) = √x = y such that y² = x

§Examples

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

let x = array![1.0_f64, 4.0, 9.0, 16.0, 25.0];
let result = sqrt_simd(&x.view());

assert_eq!(result[0], 1.0);
assert_eq!(result[1], 2.0);
assert_eq!(result[2], 3.0);
assert_eq!(result[3], 4.0);
assert_eq!(result[4], 5.0);

§Edge Cases

  • Empty array: Returns empty array
  • Zero: Returns zero
  • Negative numbers: Returns NaN (undefined in real numbers)
  • NaN: Returns NaN (preserves NaN)
  • Positive infinity: Returns positive infinity

§Applications

  • Statistics: Standard deviation, RMS (Root Mean Square)
  • Geometry: Distance calculations, Euclidean metrics
  • Physics: Velocity from kinetic energy, magnitude calculations
  • Machine Learning: Gradient scaling, learning rate schedules
  • Image Processing: Gaussian blur, distance transforms

§Note on Negative Values

Square root of negative numbers is undefined in real arithmetic. The function will return NaN for negative inputs, following IEEE 754 standard.

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

let x = array![-1.0_f64];
let result = sqrt_simd(&x.view());
assert!(result[0].is_nan());