MCPcopy Create free account
hub / github.com/douchuan/algorithm / dot

Method dot

src/math/sparse_vector.rs:52–72  ·  view source on GitHub ↗

Returns the inner product of this vector with the specified vector.

(&self, that: &Self)

Source from the content-addressed store, hash-verified

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.

Callers 1

magnitudeMethod · 0.80

Calls 5

ErrEnum · 0.85
nnzMethod · 0.80
keysMethod · 0.45
iterMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected