1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
use russell_lab::Matrix;
/// Defines a trait to handle Matrix-like data
///
/// # Example
///
/// ```
/// use plotpy::AsMatrix;
/// use russell_lab::Matrix;
///
/// fn sum<'a, T, U>(array: &'a T) -> f64
/// where
/// T: AsMatrix<'a, U>,
/// U: 'a + Into<f64>,
/// {
/// let mut res = 0.0;
/// let (m, n) = array.mat_size();
/// for i in 0..m {
/// for j in 0..n {
/// res += array.mat_at(i, j).into();
/// }
/// }
/// res
/// }
///
/// // heap-allocated 2D array (vector of vectors)
/// const IGNORED: f64 = 123.456;
/// let a = vec![
/// vec![1.0, 2.0],
/// vec![3.0, 4.0, IGNORED, IGNORED, IGNORED],
/// vec![5.0, 6.0],
/// ];
/// assert_eq!(sum(&a), 21.0);
///
/// // heap-allocated 2D array (aka slice of slices)
/// let b: &[&[f64]] = &[
/// &[10.0, 20.0],
/// &[30.0, 40.0, IGNORED],
/// &[50.0, 60.0, IGNORED, IGNORED],
/// ];
/// assert_eq!(sum(&b), 210.0);
///
/// // stack-allocated (fixed-size) 2D array
/// let c = [
/// [100.0, 200.0],
/// [300.0, 400.0],
/// [500.0, 600.0],
/// ];
/// assert_eq!(sum(&c), 2100.0);
///
/// // using Matrix
/// let d = Matrix::from(&[
/// [0.1, 0.2],
/// [0.3, 0.4],
/// [0.5, 0.6],
/// ]);
/// assert_eq!(sum(&d), 2.1);
/// ```
pub trait AsMatrix<'a, U: 'a> {
/// Returns the size of the matrix
fn mat_size(&self) -> (usize, usize);
/// Returns the value at index i
fn mat_at(&self, i: usize, j: usize) -> U;
}
/// Defines a heap-allocated 2D array (vector of vectors)
///
/// # Notes
///
/// * The number of columns is defined by the first row
/// * The next rows must have at least the same number of columns as the first row
impl<'a, U: 'a> AsMatrix<'a, U> for Vec<Vec<U>>
where
U: 'a + Copy,
{
fn mat_size(&self) -> (usize, usize) {
(self.len(), self[0].len())
}
fn mat_at(&self, i: usize, j: usize) -> U {
self[i][j]
}
}
/// Defines a heap-allocated 2D array (slice of slices)
///
/// # Notes
///
/// * The number of columns is defined by the first row
/// * The next rows must have at least the same number of columns as the first row
impl<'a, U> AsMatrix<'a, U> for &'a [&'a [U]]
where
U: 'a + Copy,
{
fn mat_size(&self) -> (usize, usize) {
(self.len(), self[0].len())
}
fn mat_at(&self, i: usize, j: usize) -> U {
self[i][j]
}
}
/// Defines a stack-allocated (fixed-size) 2D array
impl<'a, U, const M: usize, const N: usize> AsMatrix<'a, U> for [[U; N]; M]
where
U: 'a + Copy,
{
fn mat_size(&self) -> (usize, usize) {
(self.len(), self[0].len())
}
fn mat_at(&self, i: usize, j: usize) -> U {
self[i][j]
}
}
/// Handles Matrix
impl<'a> AsMatrix<'a, f64> for Matrix {
fn mat_size(&self) -> (usize, usize) {
self.dims()
}
fn mat_at(&self, i: usize, j: usize) -> f64 {
self.get(i, j)
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#[cfg(test)]
mod tests {
use super::AsMatrix;
use russell_lab::Matrix;
use std::fmt::Write;
fn matrix_str<'a, T, U>(array: &'a T) -> String
where
T: AsMatrix<'a, U>,
U: 'a + std::fmt::Display,
{
let mut buf = String::new();
let (m, n) = array.mat_size();
for i in 0..m {
for j in 0..n {
write!(&mut buf, "{},", array.mat_at(i, j)).unwrap();
}
write!(&mut buf, "\n").unwrap();
}
buf
}
#[test]
fn as_matrix_works() {
// heap-allocated 2D array (vector of vectors)
const IGNORED: f64 = 123.456;
let a = vec![
vec![1.0, 2.0],
vec![3.0, 4.0, IGNORED, IGNORED, IGNORED],
vec![5.0, 6.0],
];
assert_eq!(
matrix_str(&a),
"1,2,\n\
3,4,\n\
5,6,\n"
);
// heap-allocated 2D array (aka slice of slices)
let b: &[&[f64]] = &[&[10.0, 20.0], &[30.0, 40.0, IGNORED], &[50.0, 60.0, IGNORED, IGNORED]];
assert_eq!(
matrix_str(&b),
"10,20,\n\
30,40,\n\
50,60,\n"
);
// stack-allocated (fixed-size) 2D array
let c = [[100.0, 200.0], [300.0, 400.0], [500.0, 600.0]];
assert_eq!(
matrix_str(&c),
"100,200,\n\
300,400,\n\
500,600,\n"
);
// matrix
let d = Matrix::from(&[[0.1, 0.2], [0.3, 0.4], [0.5, 0.6]]);
assert_eq!(
matrix_str(&d),
"0.1,0.2,\n\
0.3,0.4,\n\
0.5,0.6,\n"
);
}
}