Compare commits

..

2 Commits

Author SHA1 Message Date
Jon Janzen
2a602f5804 wrote first bit of matrix 2021-03-26 20:01:41 -06:00
Jon Janzen
7bb7fa8ab4 added matrix init 2021-03-26 16:32:00 -06:00
2 changed files with 54 additions and 0 deletions

9
matrix/Cargo.toml Normal file
View File

@@ -0,0 +1,9 @@
[package]
name = "matrix"
version = "0.1.0"
authors = ["Jon Janzen <jonjanzen@me.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

45
matrix/src/lib.rs Normal file
View File

@@ -0,0 +1,45 @@
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]);
}
}