wrote first bit of matrix

This commit is contained in:
Jon Janzen
2021-03-26 20:01:41 -06:00
parent 7bb7fa8ab4
commit 2a602f5804

View File

@@ -1,7 +1,45 @@
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
use std::ops::Index;
pub struct Matrix {
matrix: Vec<Vec<f32>>,
}
impl Matrix {
pub fn new(matrix: Vec<Vec<f32>>) -> Matrix {
Matrix {
matrix: matrix,
}
}
}
impl Index<usize> for Matrix {
type Output = Vec<f32>;
fn index(&self, index: usize) -> &Self::Output {
&self.matrix[index]
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let m = vec![
vec![1.0, 2.0, 3.0, 4.0],
vec![5.5, 6.5, 7.5, 8.5],
vec![9.0, 10.0, 11.0, 12.0],
vec![13.5, 14.5, 15.5, 16.5],
];
let matrix = Matrix::new(m);
assert_eq!(1.0, matrix[0][0]);
assert_eq!(4.0, matrix[0][3]);
assert_eq!(5.5, matrix[1][0]);
assert_eq!(7.5, matrix[1][2]);
assert_eq!(11.0, matrix[2][2]);
assert_eq!(13.5, matrix[3][0]);
assert_eq!(15.5, matrix[3][2]);
}
}