diff --git a/src/math/matrix/matrix.rs b/src/math/matrix.rs similarity index 82% rename from src/math/matrix/matrix.rs rename to src/math/matrix.rs index 5ba39a8..ad0a60f 100644 --- a/src/math/matrix/matrix.rs +++ b/src/math/matrix.rs @@ -3,24 +3,11 @@ use std::ops::{Index, IndexMut}; use num::Num; pub struct Matrix { - pub(super) width: usize, - pub(super) data: Box> + width: usize, + data: Box> } impl Matrix { - pub fn new(mut data: Vec, width: usize) -> Self { - if width <= 0 { - panic!("Matrix width must be greater than 0, but got {}", width) - } - let height = data.len() / width; - let size = width * height; - if size < 2 { - panic!("Total matrix size must be greater than 1, but got {}", size) - } - data.truncate(size); - Self { width, data: Box::new(data) } - } - pub fn new_filled(val: T, width: usize, height: usize) -> Self { let Some(size) = width.checked_mul(height) else { @@ -36,6 +23,25 @@ impl Matrix { pub fn new_zeroes(width: usize, height: usize) -> Self { Self::new_filled(T::zero(), width, height) } +} + +impl Matrix { + pub fn new(mut data: Vec, width: usize) -> Self { + if width <= 0 { + panic!("Matrix width must be greater than 0, but got {}", width) + } + let height = data.len() / width; + let size = width * height; + if size < 2 { + panic!("Total matrix size must be greater than 1, but got {}", size) + } + data.truncate(size); + Self { width, data: Box::new(data) } + } + + pub(super) fn new_unchecked(data: Vec, width: usize) -> Self { + Self { width, data: Box::new(data) } + } pub fn width(&self) -> usize { self.width @@ -45,6 +51,10 @@ impl Matrix { self.data.len() / self.width } + pub fn size(&self) -> usize { + self.data.len() + } + pub fn minor(&self, row_index: usize, column_index: usize) -> Self { if self.width < 2 { panic!("Matrix width must be greater than 1 to form its minor, but got {}", self.width) @@ -54,17 +64,26 @@ impl Matrix { panic!("Matrix height must be greater than 1 to form its minor, but got {}", height) } let minor_width = self.width - 1; - let minor_size = minor_width * (height - 1); - if minor_size < 2 { - panic!("Minor total size must be greater than 2, but got {}", minor_size) - } - let mut minor_data = Vec::with_capacity(minor_size); + let mut minor_data = Vec::with_capacity(minor_width * (height - 1)); for i in 0..row_index { + if i == row_index { continue; } let row = &self[i]; todo!() } Self { width: minor_width, data: Box::new(minor_data) } } + + pub fn into_iter(self) -> std::vec::IntoIter { + self.data.into_iter() + } + + pub fn iter(&self) -> std::slice::Iter { + self.data.iter() + } + + pub fn iter_mut(&mut self) -> std::slice::IterMut { + self.data.iter_mut() + } } impl Index for Matrix { @@ -109,6 +128,7 @@ mod tests { #[should_panic] fn size_too_big() { Matrix::::new_zeroes(usize::MAX, 2); + let a = vec![0].into_iter(); } #[test] diff --git a/src/math/matrix/mod.rs b/src/math/matrix/mod.rs deleted file mode 100644 index 74ad4bc..0000000 --- a/src/math/matrix/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod matrix; -pub mod sq_matrix; \ No newline at end of file diff --git a/src/math/matrix/sq_matrix.rs b/src/math/matrix/sq_matrix.rs deleted file mode 100644 index f3677bd..0000000 --- a/src/math/matrix/sq_matrix.rs +++ /dev/null @@ -1,54 +0,0 @@ -use num::Num; -use super::matrix::Matrix; - -pub struct SquareMatrix { - matrix: Matrix -} - -impl SquareMatrix { - pub fn new(mut data: Vec, order: usize) -> Self { - let size = Self::check_size(order); - data.truncate(size); - Self { - matrix: Matrix { - width: order, - data: Box::new(data) - } - } - } - - pub fn new_filled(val: T, order: usize) -> Self { - let size = Self::check_size(order); - let data = Box::new(vec![val; size]); - Self { - matrix: Matrix { - width: order, data - } - } - } - - pub fn new_zeroes(order: usize) -> Self { - Self::new_filled(T::zero(), order) - } - - fn check_size(order: usize) -> usize { - if order < 2 { - panic!("Square matrix must have at least order of 2, but got {}", order) - } - let Some(size) = order.checked_mul(order) - else { - panic!("Total matrix size for order {} exceeds usize limit", order) - }; - return size; - } - - pub fn order(&self) -> usize { - self.matrix.width - } -} - -macro_rules! sq_matrix { - [ $o:expr; $( $x:expr ),+ ] => { - SquareMatrix::new(vec![$( $x, )+], $o) - }; -} diff --git a/src/math/mod.rs b/src/math/mod.rs index 3bb3506..4de312a 100644 --- a/src/math/mod.rs +++ b/src/math/mod.rs @@ -1 +1,5 @@ -pub mod matrix; +mod matrix; +pub use matrix::Matrix; + +mod sq_matrix; +pub use sq_matrix::SquareMatrix; diff --git a/src/math/sq_matrix.rs b/src/math/sq_matrix.rs new file mode 100644 index 0000000..d42e563 --- /dev/null +++ b/src/math/sq_matrix.rs @@ -0,0 +1,51 @@ +use num::Num; +use super::matrix::Matrix; + +pub struct SquareMatrix { + matrix: Matrix +} + +impl SquareMatrix { + pub fn new_filled(val: T, order: usize) -> Self { + let size = check_size(order); + let data = vec![val; size]; + Self { + matrix: Matrix::new_unchecked(data, order) + } + } + + pub fn new_zeroes(order: usize) -> Self { + Self::new_filled(T::zero(), order) + } +} + +impl SquareMatrix { + pub fn new(mut data: Vec, order: usize) -> Self { + let size = check_size(order); + data.truncate(size); + Self { + matrix: Matrix::new_unchecked(data, order) + } + } + + pub fn order(&self) -> usize { + self.matrix.width() + } +} + +fn check_size(order: usize) -> usize { + if order < 2 { + panic!("Square matrix must have at least order of 2, but got {}", order) + } + let Some(size) = order.checked_mul(order) + else { + panic!("Total matrix size for order {} exceeds usize limit", order) + }; + return size; +} + +macro_rules! sq_matrix { + [ $o:expr; $( $x:expr ),+ ] => { + SquareMatrix::new(vec![$( $x, )+], $o) + }; +}