1use crate::types::*;
2use ndarray::*;
3
4pub trait InnerProduct {
9 type Elem: Scalar;
10
11 fn inner<S>(&self, rhs: &ArrayBase<S, Ix1>) -> Self::Elem
13 where
14 S: Data<Elem = Self::Elem>;
15}
16
17impl<A, S> InnerProduct for ArrayBase<S, Ix1>
18where
19 A: Scalar,
20 S: Data<Elem = A>,
21{
22 type Elem = A;
23 fn inner<St: Data<Elem = A>>(&self, rhs: &ArrayBase<St, Ix1>) -> A {
24 assert_eq!(self.len(), rhs.len());
25 Zip::from(self)
26 .and(rhs)
27 .fold_while(A::zero(), |acc, s, r| {
28 FoldWhile::Continue(acc + s.conj() * *r)
29 })
30 .into_inner()
31 }
32}