#![allow(unused_imports)]
use crate::{UtilsError, UtilsResult};
use scirs2_core::ndarray::{Array1, ArrayView1};
use scirs2_core::simd_ops::SimdUnifiedOps;
#[inline]
pub fn simd_sum_f64(data: &[f64]) -> f64 {
data.iter().sum()
}
#[inline]
pub fn simd_sum_f32(data: &[f32]) -> f32 {
data.iter().sum()
}
#[inline]
pub fn simd_dot_product_f64(a: &[f64], b: &[f64]) -> f64 {
if a.len() != b.len() {
return 0.0;
}
a.iter().zip(b.iter()).map(|(x, y)| x * y).sum()
}
#[inline]
pub fn simd_dot_product_f32(a: &[f32], b: &[f32]) -> f32 {
if a.len() != b.len() {
return 0.0;
}
a.iter().zip(b.iter()).map(|(x, y)| x * y).sum()
}
pub fn simd_add_arrays_f64(a: &ArrayView1<f64>, b: &ArrayView1<f64>) -> UtilsResult<Array1<f64>> {
if a.len() != b.len() {
return Err(UtilsError::ShapeMismatch {
expected: vec![a.len()],
actual: vec![b.len()],
});
}
let len = a.len();
let mut result = vec![0.0; len];
for ((a_val, b_val), result_val) in a.iter().zip(b.iter()).zip(result.iter_mut()) {
*result_val = a_val + b_val;
}
Ok(Array1::from_vec(result))
}
pub fn simd_add_arrays_f32(a: &ArrayView1<f32>, b: &ArrayView1<f32>) -> UtilsResult<Array1<f32>> {
if a.len() != b.len() {
return Err(UtilsError::ShapeMismatch {
expected: vec![a.len()],
actual: vec![b.len()],
});
}
let len = a.len();
let mut result = vec![0.0; len];
for ((a_val, b_val), result_val) in a.iter().zip(b.iter()).zip(result.iter_mut()) {
*result_val = a_val + b_val;
}
Ok(Array1::from_vec(result))
}
pub fn simd_multiply_arrays_f64(
a: &ArrayView1<f64>,
b: &ArrayView1<f64>,
) -> UtilsResult<Array1<f64>> {
if a.len() != b.len() {
return Err(UtilsError::ShapeMismatch {
expected: vec![a.len()],
actual: vec![b.len()],
});
}
let len = a.len();
let mut result = vec![0.0; len];
for ((a_val, b_val), result_val) in a.iter().zip(b.iter()).zip(result.iter_mut()) {
*result_val = a_val * b_val;
}
Ok(Array1::from_vec(result))
}
pub fn simd_multiply_arrays_f32(
a: &ArrayView1<f32>,
b: &ArrayView1<f32>,
) -> UtilsResult<Array1<f32>> {
if a.len() != b.len() {
return Err(UtilsError::ShapeMismatch {
expected: vec![a.len()],
actual: vec![b.len()],
});
}
let len = a.len();
let mut result = vec![0.0; len];
for ((a_val, b_val), result_val) in a.iter().zip(b.iter()).zip(result.iter_mut()) {
*result_val = a_val * b_val;
}
Ok(Array1::from_vec(result))
}
pub fn simd_scale_array_f64(array: &mut Array1<f64>, scalar: f64) -> UtilsResult<()> {
array.par_mapv_inplace(|x| x * scalar);
Ok(())
}
pub fn simd_scale_array_f32(array: &mut Array1<f32>, scalar: f32) -> UtilsResult<()> {
array.par_mapv_inplace(|x| x * scalar);
Ok(())
}
pub fn fast_dot_product_f64(a: &[f64], b: &[f64]) -> f64 {
simd_dot_product_f64(a, b)
}
pub fn fast_dot_product_f32(a: &[f32], b: &[f32]) -> f32 {
simd_dot_product_f32(a, b)
}
pub fn fast_sum_f64(data: &[f64]) -> f64 {
simd_sum_f64(data)
}
pub fn fast_sum_f32(data: &[f32]) -> f32 {
simd_sum_f32(data)
}