use rayon::prelude::*;
use std::fmt;
pub trait Matrix {
fn nrows(&self)->usize;
fn ncols(&self)->usize;
fn eval(&self, row_index: usize, col_index: usize)->f64;
fn flattened_array(&self) -> Vec<f64> where Self:Sync {
(0..self.nrows())
.into_par_iter()
.map(move |row_index|
(0..self.ncols())
.into_par_iter()
.map(move |col_index|
self.eval(row_index, col_index)
).collect::<Vec<f64>>()
)
.flatten()
.collect()
}
fn matrix(&self) -> Vec<Vec<f64>> where Self:Sync {
(0..self.nrows())
.into_par_iter()
.map(|row_index|
(0..self.ncols())
.into_par_iter()
.map(|col_index|
self.eval(row_index, col_index)
).collect::<Vec<f64>>()
).collect()
}
fn format(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
for row_index in 0..self.nrows() {
match row_index {
0 => write!(f, "[[")?,
_ => write!(f, "\n [")?,
}
for col_index in 0..self.ncols() {
write!(f, " {:5.2}", self.eval(row_index, col_index))?;
}
write!(f, " ]")?;
}
write!(f, "]")?;
Ok(())
}
fn samples(&self, indices: Vec<(usize, usize)>) -> Vec<f64> where Self:Sync {
indices.into_par_iter()
.map(|(row_index, col_index)|
self.eval(row_index, col_index)
).collect()
}
}