1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#![allow(missing_docs)]

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

use num_traits::{Signed, Zero};

use super::*;

pub trait Geometric<T> {
    fn scalar_product(&self, other: &Self) -> T;

    fn abs_scalar_product(&self, other: &Self) -> T
    where
        T: Signed;

    fn norm_squared(&self) -> T;
}

impl<T, N: ArrayLength<T>> Geometric<T> for NumericArray<T, N>
where
    T: Add<Output = T> + Mul<Output = T> + Zero + Copy,
{
    #[inline(always)]
    fn scalar_product(&self, other: &Self) -> T {
        self.iter().zip(&other.0).fold(T::zero(), |sum, (l, r)| {
            sum + (*l * *r)
        })
    }

    #[inline(always)]
    fn abs_scalar_product(&self, other: &Self) -> T
    where
        T: Signed,
    {
        self.iter().zip(&other.0).fold(T::zero(), |sum, (l, r)| {
            sum + (*l * *r).abs()
        })
    }

    #[inline(always)]
    fn norm_squared(&self) -> T {
        self.scalar_product(self)
    }
}