Initial commit
This commit is contained in:
commit
93579e28f0
6 changed files with 162 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/target
|
89
Cargo.lock
generated
Normal file
89
Cargo.lock
generated
Normal file
|
@ -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",
|
||||
]
|
7
Cargo.toml
Normal file
7
Cargo.toml
Normal file
|
@ -0,0 +1,7 @@
|
|||
[package]
|
||||
name = "mathematical"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
num = "0.4.3"
|
1
src/lib.rs
Normal file
1
src/lib.rs
Normal file
|
@ -0,0 +1 @@
|
|||
pub mod math;
|
63
src/math/matrix.rs
Normal file
63
src/math/matrix.rs
Normal file
|
@ -0,0 +1,63 @@
|
|||
use num::{Integer, Num};
|
||||
|
||||
pub struct Matrix<T: Num + Copy> {
|
||||
width: usize, height: usize,
|
||||
data: Box<Vec<T>>
|
||||
}
|
||||
|
||||
impl<T: Num + Copy> Matrix<T> {
|
||||
pub fn new(width: usize, data: Vec<T>) -> 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::<i32>::new_zeroes(usize::MAX, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn size_too_small() {
|
||||
Matrix::<i32>::new_zeroes(1, 1);
|
||||
}
|
||||
}
|
1
src/math/mod.rs
Normal file
1
src/math/mod.rs
Normal file
|
@ -0,0 +1 @@
|
|||
pub mod matrix;
|
Loading…
Reference in a new issue