The following code calculates a matrix of distances between each pair of elements in the corpus. However, for large corpora the process becomes quite lengthy. How can I parallelize it with rayon?
use std::iter::Iterator;
use ndarray::Array2;
type DistanceFn = dyn Fn(&f64, &f64) -> f64 + Sync;
fn calculate(corpus: &[f64], distance_fn: Box<DistanceFn>) -> Array2<f64> {
let mut distances: Array2<f64> = Array2::zeros((corpus.len(), corpus.len()));
for (i, m1) in corpus.iter().enumerate() {
for (j, m2) in corpus.iter().enumerate() {
distances[[i, j]] = distance_fn(m1, m2);
}
}
distances
}
fn diff_distance(m1:&f64, m2:&f64) -> f64 {
m1-m2
}
fn main() {
let corpus = &vec![5.0,6.0,7.0];
let matrix = calculate(corpus, Box::new(diff_distance));
print!("{}", matrix)
}