acme_tensor/impls/
iter.rs

1/*
2    Appellation: iter <impls>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5use crate::shape::Axis;
6use crate::tensor::TensorBase;
7use core::iter::{Product, Sum};
8
9impl<T> TensorBase<T>
10where
11    T: Copy,
12{
13    /// Compute the product of all elements in the tensor
14    pub fn product(&self) -> T
15    where
16        T: Product,
17    {
18        self.data().iter().copied().product()
19    }
20    #[doc(hidden)]
21    pub fn product_axis(&self, _axis: Axis) -> T {
22        unimplemented!("product_axis")
23    }
24    /// Compute the sum of all elements in the tensor
25    pub fn sum(&self) -> T
26    where
27        T: Sum,
28    {
29        self.data().iter().copied().sum()
30    }
31    #[doc(hidden)]
32    /// Compute the sum of all elements along the given axis
33    pub fn sum_axis(&self, _axis: Axis) -> T {
34        unimplemented!("sum_axis")
35    }
36}
37
38impl<T> IntoIterator for TensorBase<T> {
39    type Item = T;
40    type IntoIter = std::vec::IntoIter<T>;
41
42    fn into_iter(self) -> Self::IntoIter {
43        self.data.into_iter()
44    }
45}
46
47impl<T> FromIterator<T> for TensorBase<T> {
48    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
49        Self::from_vec(Vec::from_iter(iter))
50    }
51}