abs_simd

Function abs_simd 

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

Compute the absolute value of each element (SIMD-accelerated).

Computes |x| for each element in the array.

§Arguments

  • x - Input 1D array

§Returns

Array1<F> with the same length as input, with absolute values.

§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

abs(x) = |x| = {
    x   if x >= 0
    -x  if x < 0
}

§Examples

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

let x = array![-3.0, -1.5, 0.0, 1.5, 3.0];
let result = abs_simd(&x.view());

assert_eq!(result[0], 3.0);
assert_eq!(result[1], 1.5);
assert_eq!(result[2], 0.0);
assert_eq!(result[3], 1.5);
assert_eq!(result[4], 3.0);

§Edge Cases

  • Empty array: Returns empty array
  • Zero: Returns zero
  • NaN: Returns NaN (preserves NaN)
  • Infinity: Returns positive infinity

§Applications

  • Statistics: Absolute deviation, MAE (Mean Absolute Error)
  • Signal Processing: Envelope detection, magnitude calculation
  • Optimization: L1 norm computation, absolute loss functions
  • Numerical Analysis: Error analysis, residual computation
  • Image Processing: Gradient magnitude, difference images