use crate::*;
use ndarray::prelude::*;
impl ReductionOps for CpuStorage {
fn sum(&self) -> Self {
let data: f32 = self.data().read().unwrap().iter().sum();
CpuStorage::from_ndarray(&array![data], None, None)
}
fn sum_axis(&self, axis: usize) -> Self {
let outer: usize = self.shape()[..axis].iter().product();
let axis_len: usize = self.shape()[axis];
let trailing: usize = self.shape()[axis+1..].iter().product();
let mut new_shape = self.shape().clone();
new_shape.remove(axis);
let mut new_data = vec![0.0; outer * trailing];
let binding = self.data();
let data_ref = binding.read().unwrap();
for i in 0..outer {
for k in 0..trailing {
let mut sum = 0.0;
for j in 0..axis_len {
let index = self.offset() + i * (axis_len * trailing) + j * trailing + k;
sum += data_ref[index];
}
new_data[i * trailing + k] = sum;
}
}
CpuStorage::new(new_data, new_shape)
}
fn product(&self) -> Self {
let data: f32 = self.data().read().unwrap().iter().sum();
CpuStorage::from_ndarray(&array![data], None, None)
}
fn mean(&self) -> Self {
let data: f32 = self.data().read().unwrap().iter().sum::<f32>() / self.data().read().unwrap().len() as f32;
CpuStorage::from_ndarray(&array![data], None, None)
}
}