can create from arrays
This commit is contained in:
@@ -8,3 +8,4 @@ edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
approx = "0.4"
|
||||
bencher = "0.1.5"
|
||||
|
||||
@@ -14,6 +14,20 @@ impl Matrix {
|
||||
matrix: matrix,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_array(matrix: [[f32; 4]; 4]) -> Matrix {
|
||||
let mut m = Vec::with_capacity(4);
|
||||
for row in 0..matrix.len() {
|
||||
let mut r = Vec::with_capacity(4);
|
||||
for col in 0..matrix[row].len() {
|
||||
r.push(matrix[row][col]);
|
||||
}
|
||||
m.push(r);
|
||||
}
|
||||
Matrix {
|
||||
matrix: m,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Index<usize> for Matrix {
|
||||
@@ -66,6 +80,25 @@ mod tests {
|
||||
assert_eq!(15.5, matrix[3][2]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn matrix_4x4_array() {
|
||||
let m = [
|
||||
[1.0, 2.0, 3.0, 4.0],
|
||||
[5.5, 6.5, 7.5, 8.5],
|
||||
[9.0, 10.0, 11.0, 12.0],
|
||||
[13.5, 14.5, 15.5, 16.5],
|
||||
];
|
||||
|
||||
let matrix = Matrix::from_array(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]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn matrix_2x2() {
|
||||
let m = vec![
|
||||
|
||||
Reference in New Issue
Block a user