Returns the inner product of this vector with the specified vector.
(&self, that: &Self)
| 50 | |
| 51 | /// Returns the inner product of this vector with the specified vector. |
| 52 | pub fn dot(&self, that: &Self) -> Result<f64, Err> { |
| 53 | if self.d != that.d { |
| 54 | Err(Err::Dimension) |
| 55 | } else { |
| 56 | let keys = if self.nnz() <= that.nnz() { |
| 57 | self.st.keys() |
| 58 | } else { |
| 59 | that.st.keys() |
| 60 | }; |
| 61 | |
| 62 | let sum = keys.iter().fold(0.0, |acc, &i| { |
| 63 | let delta = match (self.st.get(i), that.st.get(i)) { |
| 64 | (Some(a), Some(b)) => a * b, |
| 65 | _ => 0.0, |
| 66 | }; |
| 67 | acc + delta |
| 68 | }); |
| 69 | |
| 70 | Ok(sum) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /// Returns the magnitude of this vector. |
| 75 | /// This is also known as the L2 norm or the Euclidean norm. |