commit 93579e28f0db04f565bac7c2a854774fc9514312 Author: erius Date: Sat May 11 01:59:56 2024 +0300 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..ac37eb0 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,89 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "autocfg" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" + +[[package]] +name = "mathematical" +version = "0.1.0" +dependencies = [ + "num", +] + +[[package]] +name = "num" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c165a9ab64cf766f73521c0dd2cfdff64f488b8f0b3e621face3462d3db536d7" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..dd7d7b7 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "mathematical" +version = "0.1.0" +edition = "2021" + +[dependencies] +num = "0.4.3" diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..b8135c3 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1 @@ +pub mod math; diff --git a/src/math/matrix.rs b/src/math/matrix.rs new file mode 100644 index 0000000..bb9fad9 --- /dev/null +++ b/src/math/matrix.rs @@ -0,0 +1,63 @@ +use num::{Integer, Num}; + +pub struct Matrix { + width: usize, height: usize, + data: Box> +} + +impl Matrix { + pub fn new(width: usize, data: Vec) -> Self { + if width == 0 { + panic!("Matrix width must not be 0") + } + let (height, append_zeroes) = data.len().div_rem(&width); + let mut matrix = Self { width, height, data: Box::new(data) }; + if append_zeroes == 0 { return matrix; } + matrix.height += 1; + for _ in 0..append_zeroes { + matrix.data.push(T::zero()); + } + return matrix; + } + + pub fn new_filled(val: T, width: usize, height: usize) -> Self { + let Some(size) = width.checked_mul(height) + else { + panic!("Total matrix size for width {} and height {} exceeds usize limit", width, height) + }; + if size < 2 { + panic!("Total matrix size must be greater than 1, but got {}", size) + } + let data = Box::new(vec![val; size]); + Self { width, height, data } + } + + pub fn new_zeroes(width: usize, height: usize) -> Self { + Self::new_filled(T::zero(), width, height) + } + + pub fn width(&self) -> usize { + self.width + } + + pub fn height(&self) -> usize { + self.height + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + #[should_panic] + fn size_too_big() { + Matrix::::new_zeroes(usize::MAX, 2); + } + + #[test] + #[should_panic] + fn size_too_small() { + Matrix::::new_zeroes(1, 1); + } +} diff --git a/src/math/mod.rs b/src/math/mod.rs new file mode 100644 index 0000000..3bb3506 --- /dev/null +++ b/src/math/mod.rs @@ -0,0 +1 @@ +pub mod matrix;