array_matrix/vector/
dot.rs

1use std::ops::{Mul, Add};
2
3use num_traits::Zero;
4
5use crate::Vector;
6
7pub trait Dot<Rhs: Vector>: Vector
8{
9    type Output;
10
11    /// Returns the scalar dot product of two vector-arrays
12    /// 
13    /// u ⋅ v
14    /// 
15    /// # Arguments
16    /// 
17    /// * `rhs` - A vector of same length
18    /// 
19    /// # Examples
20    /// 
21    /// ```rust
22    /// let u = [1.0, 2.0, 3.0];
23    /// let v = [1.0, 2.0, 3.0];
24    /// let uv = 1.0 + 4.0 + 9.0;
25    /// 
26    /// assert_eq!(u.dot(v), uv);
27    /// ```
28    fn dot(self, rhs: Rhs) -> Self::Output;
29}
30
31impl<F, const L: usize> Dot<[F; L]> for [F; L]
32where
33    Self: Vector,
34    F: Mul<F, Output = F> + Add<F, Output = F> + Zero + Clone
35{
36    type Output = F;
37    fn dot(self, rhs: [F; L]) -> Self::Output
38    {
39        (0..L).map(|i| self[i].clone()*rhs[i].clone()).reduce(|a, b| a + b).unwrap_or(F::zero())
40    }
41}