numeric_array/
geometry.rs1#![allow(missing_docs)]
2
3use core::ops::Mul;
4
5use num_traits::{Signed, Zero};
6
7use super::*;
8
9pub trait Geometric<T> {
10 fn scalar_product(&self, other: &Self) -> T;
11
12 fn abs_scalar_product(&self, other: &Self) -> T
13 where
14 T: Signed;
15
16 fn norm_squared(&self) -> T;
17}
18
19impl<T, N: ArrayLength> Geometric<T> for NumericArray<T, N>
20where
21 T: Mul<Output = T> + Zero + Copy,
22{
23 #[inline(always)]
24 fn scalar_product(&self, other: &Self) -> T {
25 self.iter().zip(&other.0).fold(T::zero(), |sum, (l, r)| sum + (*l * *r))
26 }
27
28 #[inline(always)]
29 fn abs_scalar_product(&self, other: &Self) -> T
30 where
31 T: Signed,
32 {
33 self.iter().zip(&other.0).fold(T::zero(), |sum, (l, r)| sum + (*l * *r).abs())
34 }
35
36 #[inline(always)]
37 fn norm_squared(&self) -> T {
38 self.scalar_product(self)
39 }
40}