ndarray_linalg/inner.rs
1use crate::types::*;
2use ndarray::*;
3
4/// Inner Product
5///
6/// Differenct from `Dot` trait, this take complex conjugate of `self` elements
7///
8pub trait InnerProduct {
9 type Elem: Scalar;
10
11 /// Inner product `(self.conjugate, rhs)
12 fn inner(&self, rhs: &ArrayRef<Self::Elem, Ix1>) -> Self::Elem;
13}
14
15impl<A> InnerProduct for ArrayRef<A, Ix1>
16where
17 A: Scalar,
18{
19 type Elem = A;
20
21 fn inner(&self, rhs: &ArrayRef<A, Ix1>) -> A {
22 assert_eq!(self.len(), rhs.len());
23 Zip::from(self)
24 .and(rhs)
25 .fold_while(A::zero(), |acc, s, r| {
26 FoldWhile::Continue(acc + s.conj() * *r)
27 })
28 .into_inner()
29 }
30}