Compare commits
2 Commits
6dafed1195
...
2a602f5804
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2a602f5804 | ||
|
|
7bb7fa8ab4 |
9
matrix/Cargo.toml
Normal file
9
matrix/Cargo.toml
Normal 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
45
matrix/src/lib.rs
Normal 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]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user