sklears_utils/array_utils/
simd_ops.rs1#![allow(unused_imports)]
9
10use crate::{UtilsError, UtilsResult};
11use scirs2_core::ndarray::{Array1, ArrayView1};
12use scirs2_core::simd_ops::SimdUnifiedOps;
13
14#[inline]
17pub fn simd_sum_f64(data: &[f64]) -> f64 {
18 data.iter().sum()
19}
20
21#[inline]
24pub fn simd_sum_f32(data: &[f32]) -> f32 {
25 data.iter().sum()
26}
27
28#[inline]
31pub fn simd_dot_product_f64(a: &[f64], b: &[f64]) -> f64 {
32 if a.len() != b.len() {
33 return 0.0;
34 }
35 a.iter().zip(b.iter()).map(|(x, y)| x * y).sum()
36}
37
38#[inline]
41pub fn simd_dot_product_f32(a: &[f32], b: &[f32]) -> f32 {
42 if a.len() != b.len() {
43 return 0.0;
44 }
45 a.iter().zip(b.iter()).map(|(x, y)| x * y).sum()
46}
47
48pub fn simd_add_arrays_f64(a: &ArrayView1<f64>, b: &ArrayView1<f64>) -> UtilsResult<Array1<f64>> {
51 if a.len() != b.len() {
52 return Err(UtilsError::ShapeMismatch {
53 expected: vec![a.len()],
54 actual: vec![b.len()],
55 });
56 }
57
58 let len = a.len();
59 let mut result = vec![0.0; len];
60
61 for ((a_val, b_val), result_val) in a.iter().zip(b.iter()).zip(result.iter_mut()) {
62 *result_val = a_val + b_val;
63 }
64
65 Ok(Array1::from_vec(result))
66}
67
68pub fn simd_add_arrays_f32(a: &ArrayView1<f32>, b: &ArrayView1<f32>) -> UtilsResult<Array1<f32>> {
71 if a.len() != b.len() {
72 return Err(UtilsError::ShapeMismatch {
73 expected: vec![a.len()],
74 actual: vec![b.len()],
75 });
76 }
77
78 let len = a.len();
79 let mut result = vec![0.0; len];
80
81 for ((a_val, b_val), result_val) in a.iter().zip(b.iter()).zip(result.iter_mut()) {
82 *result_val = a_val + b_val;
83 }
84
85 Ok(Array1::from_vec(result))
86}
87
88pub fn simd_multiply_arrays_f64(
91 a: &ArrayView1<f64>,
92 b: &ArrayView1<f64>,
93) -> UtilsResult<Array1<f64>> {
94 if a.len() != b.len() {
95 return Err(UtilsError::ShapeMismatch {
96 expected: vec![a.len()],
97 actual: vec![b.len()],
98 });
99 }
100
101 let len = a.len();
102 let mut result = vec![0.0; len];
103
104 for ((a_val, b_val), result_val) in a.iter().zip(b.iter()).zip(result.iter_mut()) {
105 *result_val = a_val * b_val;
106 }
107
108 Ok(Array1::from_vec(result))
109}
110
111pub fn simd_multiply_arrays_f32(
114 a: &ArrayView1<f32>,
115 b: &ArrayView1<f32>,
116) -> UtilsResult<Array1<f32>> {
117 if a.len() != b.len() {
118 return Err(UtilsError::ShapeMismatch {
119 expected: vec![a.len()],
120 actual: vec![b.len()],
121 });
122 }
123
124 let len = a.len();
125 let mut result = vec![0.0; len];
126
127 for ((a_val, b_val), result_val) in a.iter().zip(b.iter()).zip(result.iter_mut()) {
128 *result_val = a_val * b_val;
129 }
130
131 Ok(Array1::from_vec(result))
132}
133
134pub fn simd_scale_array_f64(array: &mut Array1<f64>, scalar: f64) -> UtilsResult<()> {
137 array.par_mapv_inplace(|x| x * scalar);
138 Ok(())
139}
140
141pub fn simd_scale_array_f32(array: &mut Array1<f32>, scalar: f32) -> UtilsResult<()> {
144 array.par_mapv_inplace(|x| x * scalar);
145 Ok(())
146}
147
148pub fn fast_dot_product_f64(a: &[f64], b: &[f64]) -> f64 {
150 simd_dot_product_f64(a, b)
151}
152
153pub fn fast_dot_product_f32(a: &[f32], b: &[f32]) -> f32 {
155 simd_dot_product_f32(a, b)
156}
157
158pub fn fast_sum_f64(data: &[f64]) -> f64 {
160 simd_sum_f64(data)
161}
162
163pub fn fast_sum_f32(data: &[f32]) -> f32 {
165 simd_sum_f32(data)
166}