tensorite_math_tensor 0.2.2

Tensorite Math Tensor | Tensor Implementation
Documentation
/// The utilities found in this module for `std::Vec<T>` are experimental and may or may not be kept depending on whether or not the community finds them useful or not. A poll will be held on GitHub in the form of emote-reacting to the post (Y/N).

use std::ops::{Add, Mul};

use crate::tensor::*;

impl<T: Copy> Broadcast<T, 1> for Vec<T> {
    fn broadcast(&self, other: &Tensor<T, 1>) -> Result<Tensor<T, 1>, TensorBroadcastError> {
        Tensor::<T, 1>::new_vec(self).broadcast(other)
    }

    /// Resizes the tensor in-place to the given shape, modifying the data as needed.
    /// Panics if the new shape's product doesn't match the data length (for non-broadcasting resizes).
    fn broadcast_inplace(&mut self, new_shape: [usize; 1]) {
        Tensor::<T, 1>::new_vec(self).broadcast_inplace(new_shape)
    }

    fn broadcast_shape(&self, shape: [usize; 1]) -> Tensor<T, 1> {
        Tensor::<T, 1>::new_vec(&self).broadcast_shape(shape)
    }

    fn get_broadcast_shape(&self, other: &Tensor<T, 1>) -> Result<[usize; 1], TensorBroadcastError> {
        Tensor::<T, 1>::new_vec(self).get_broadcast_shape(other)
    }
}

/// Implement dot product for `Vec<T>`s as `vec.dot()`.
impl<T: Copy + Mul<Output = T> + Add<Output = T> + Default> DotProduct for Vec<T> {
    type Output = T;
    fn dot(&self, rhs: &Vec<T>) -> Self::Output {
        assert_eq!(self.len(), rhs.len(), "Shapes must match for dot product");
        self.iter().zip(rhs.iter()).map(|(&a, &b)| a * b).fold(T::default(), |acc, x| acc + x)
    }
}