I am writing an implementation of a neural network in Rust and am trying to calculate the dot product of two matrices. I have the following code:
fn dot_product(a: Vec<f64>, b: Vec<f64>) -> f64 {
// Calculate the dot product of two vectors.
assert_eq!(a.len(), b.len());
let mut product: f64 = 0.0;
for i in 0..a.len() {
product += a[i] * b[i];
}
product
}
This takes two vectors, a
and b
(of the same length) and carries out element-wise multiplication (multiplying value 1 of vector a
with value 1 of vector b
and adding this to value 2 of vector a
and with value 2 of vector b
and so on...).
Is there a more efficient way of doing this, and if so, how?