backuping

Signed-off-by: Ivan Bushchik <ivabus@ivabus.dev>
This commit is contained in:
Ivan Bushchik 2023-11-05 19:56:25 +03:00
parent fca3033d74
commit e759277c2d
No known key found for this signature in database
GPG key ID: 2F16FBF3262E090C
6 changed files with 257 additions and 63 deletions

View file

@ -1,4 +1,4 @@
[workspace] [workspace]
members = ["matrix", "matrix_graphics"] members = ["matrix", "matrix_graphics", "lin_sys"]
default-members = ["matrix_graphics"] default-members = ["matrix_graphics"]
resolver = "2" resolver = "2"

10
lin_sys/Cargo.toml Normal file
View file

@ -0,0 +1,10 @@
[package]
name = "lin_sys"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
matrix = {version = "0.2.0", path = "../matrix"}
smurf = "0.2.0"

6
lin_sys/src/main.rs Normal file
View file

@ -0,0 +1,6 @@
use matrix;
use smurf;
fn main() {
println!("Inp")
}

View file

@ -1,6 +1,6 @@
[package] [package]
name = "matrix" name = "matrix"
version = "0.1.2" version = "0.2.0"
edition = "2021" edition = "2021"
license = "MIT" license = "MIT"

View file

@ -1,13 +1,110 @@
use std::ops::{Add, AddAssign, Mul}; #![recursion_limit = "32768"]
use rand::{thread_rng, Rng}; use rand::{thread_rng, Rng};
use std::cmp::Ord;
use std::ops::{Add, AddAssign, Mul, Neg};
pub(crate) type Matrix<T> = Vec<Vec<T>>; #[derive(Debug)]
pub struct Matrix<
T: Mul<Output = T>
+ Copy
+ AddAssign
+ From<u8>
+ PartialEq
+ Add
+ PartialOrd
+ Ord
+ Copy
+ Neg<Output = T>,
>(Vec<Vec<T>>);
fn check_valid<T>(a: &Matrix<T>) -> bool { impl<
let len = a[0].len(); T: Mul<Output = T>
+ Copy
+ AddAssign
+ From<u8>
+ PartialEq
+ Add
+ PartialOrd
+ Ord
+ Copy
+ Neg<Output = T>,
> Matrix<T>
{
fn len(&self) -> usize {
self.0.len()
}
for i in a { fn push(&mut self, element: Vec<T>) {
self.0.push(element)
}
fn new() -> Matrix<T> {
Matrix(Vec::new())
}
pub fn dot(&self, other: &Matrix<T>) -> Option<Matrix<T>> {
if !(check_valid(self) && check_valid(other)) {
return None;
}
if self.0[0].len() != other.0.len() {
return None;
}
let m = self.0[0].len();
let mut c: Matrix<T> = Matrix::new();
for i in 0..self.len() {
c.push(vec![]);
for j in 0..other.0[0].len() {
let mut s: T = 0_u8.into();
for r in 0..m {
s += self.0[i][r] * other.0[r][j];
}
c.0[i].push(s as T);
}
}
Some(c)
}
}
impl<
T: Mul<Output = T>
+ Copy
+ AddAssign
+ From<u8>
+ PartialEq
+ Add
+ PartialOrd
+ Ord
+ Copy
+ Neg<Output = T>,
> PartialEq for Matrix<T>
{
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
fn check_valid<
T: Mul<Output = T>
+ Copy
+ AddAssign
+ From<u8>
+ PartialEq
+ Add
+ PartialOrd
+ Ord
+ Copy
+ Neg<Output = T>,
>(
a: &Matrix<T>,
) -> bool {
let len = a.0[0].len();
for i in &a.0 {
if i.len() != len { if i.len() != len {
return false; return false;
} }
@ -16,7 +113,21 @@ fn check_valid<T>(a: &Matrix<T>) -> bool {
true true
} }
pub fn sum<T: Add<Output = T> + Copy>(a: &Matrix<T>, b: &Matrix<T>) -> Option<Matrix<T>> { pub fn sum<
T: Mul<Output = T>
+ Copy
+ AddAssign
+ From<u8>
+ PartialEq
+ Add
+ PartialOrd
+ Ord
+ Copy
+ Neg<Output = T>,
>(
a: &Matrix<T>,
b: &Matrix<T>,
) -> Option<Matrix<T>> {
if !(check_valid(a) && check_valid(b)) { if !(check_valid(a) && check_valid(b)) {
return None; return None;
} }
@ -25,80 +136,114 @@ pub fn sum<T: Add<Output = T> + Copy>(a: &Matrix<T>, b: &Matrix<T>) -> Option<Ma
return None; return None;
} }
let mut c: Matrix<T> = Vec::new(); let mut c: Matrix<T> = Matrix::new();
for i in 0..a.len() { for i in 0..a.len() {
c.push(vec![]); c.push(vec![]);
for j in 0..a[0].len() { for j in 0..a.0[0].len() {
c[i].push(a[i][j] + b[i][j]); c.0[i].push(a.0[i][j] + b.0[i][j]);
} }
} }
Some(c) Some(c)
} }
pub fn mul<T: Mul<Output = T> + Copy + AddAssign + From<u8>>( fn get_minor<
a: &Matrix<T>, T: Mul<Output = T>
b: &Matrix<T>, + Copy
) -> Option<Matrix<T>> { + AddAssign
if !(check_valid(a) && check_valid(b)) { + From<u8>
return None; + PartialEq
} + Add
+ PartialOrd
if a[0].len() != b.len() { + Ord
return None; + Copy
} + Neg<Output = T>,
>(
let m = a[0].len(); base: &&Matrix<T>,
let mut c: Matrix<T> = Vec::new(); mi: &usize,
mj: &usize,
for i in 0..a.len() { ) -> Matrix<T> {
c.push(vec![]); let mut minor: Matrix<T> = Matrix::new();
for j in 0..b[0].len() { let mut offset: usize = 0;
let mut s: T = 0_u8.into(); for i in 0..base.len() {
for r in 0..m { if i == *mi {
s += a[i][r] * b[r][j]; offset += 1;
} continue;
c[i].push(s as T);
} }
} minor.push(vec![]);
for j in 0..base.0[0].len() {
Some(c) if j == *mj {
}
pub fn det<T: Mul<Output = T> + Copy + Add>(a: &Matrix<T>) -> Option<Matrix<T>> {
if ! check_valid(a) {
return None;
}
if a[0].len() != a.len() {
return None;
}
let j = 0;
for i in 1..n {
let _pre_minor: Matrix<T> = vec![&a[0..i], &a[i+1..]];
let mut pre_minor: Metrix<T> = vec![vec![]];
for m in _pre_minor {
if m == j {
continue; continue;
} }
pre_minor[m].push minor.0[i - offset].push(base.0[i][j])
} }
let minor = det(&pre_minor);
} }
minor
}
pub fn det<
T: Mul<Output = T>
+ Copy
+ AddAssign
+ From<u8>
+ PartialEq
+ Add
+ PartialOrd
+ Ord
+ Copy
+ Neg<Output = T>,
>(
a: &Matrix<T>,
) -> Option<T> {
if !check_valid(a) {
return None;
}
if a.0[0].len() != a.0.len() {
return None;
}
if a.0[0].len() == 1 {
return Some(a.0[0][0]);
}
let j = 1;
let mut determinant: T = 0.into();
for i in 0..a.len() {
let minor: T = det(&get_minor(&a, &i, &j)).unwrap();
determinant += if (i + 1) % 2 == 1 {
-Into::<T>::into(1i8)
} else {
Into::<T>::into(1i8)
} * a.0[i][1]
* minor;
}
Some(determinant)
} }
#[allow(dead_code)] #[allow(dead_code)]
fn gen_matrix<T: From<i32>>(i: usize, j: usize) -> Option<Matrix<T>> { fn gen_matrix<
T: Mul<Output = T>
+ Copy
+ AddAssign
+ From<u8>
+ PartialEq
+ Add
+ PartialOrd
+ Ord
+ Copy
+ Neg<Output = T>,
>(
i: usize,
j: usize,
) -> Option<Matrix<T>> {
if !(i > 0 && j > 0) { if !(i > 0 && j > 0) {
return None; return None;
} }
let mut rng = thread_rng(); let mut rng = thread_rng();
let mut m: Matrix<T> = Vec::new(); let mut m: Matrix<T> = Matrix::new();
for a in 0..i { for a in 0..i {
m.push(vec![]); m.push(vec![]);
for _ in 0..j { for _ in 0..j {
m[a].push(T::from(rng.gen_range(0..100))); m.0[a].push(T::from(rng.gen_range(0..100)));
} }
} }
Some(m) Some(m)
@ -114,14 +259,17 @@ mod test {
fn basic_sum() { fn basic_sum() {
let a = vec![vec![1., 2.], vec![3., 4.]]; let a = vec![vec![1., 2.], vec![3., 4.]];
let b = vec![vec![5., 6.], vec![7., 8.]]; let b = vec![vec![5., 6.], vec![7., 8.]];
assert_eq!(sum(&a, &b).unwrap(), vec![vec![6., 8.], vec![10., 12.]]) assert_eq!(sum(&Matrix(a), &Matrix(b)).unwrap(), Matrix(vec![vec![6., 8.], vec![10., 12.]]))
} }
#[test] #[test]
fn basic_mul() { fn basic_mul() {
let a = vec![vec![3., -1., 2.], vec![4., 2., 0.], vec![-5., 6., 1.]]; let a = vec![vec![3., -1., 2.], vec![4., 2., 0.], vec![-5., 6., 1.]];
let b = vec![vec![8., 1.], vec![7., 2.], vec![2., -3.]]; let b = vec![vec![8., 1.], vec![7., 2.], vec![2., -3.]];
assert_eq!(mul(&a, &b).unwrap(), vec![vec![21., -5.], vec![46., 8.], vec![4., 4.]]) assert_eq!(
mul(&Matrix(a), &Matrix(b)).unwrap(),
Matrix(vec![vec![21., -5.], vec![46., 8.], vec![4., 4.]])
)
} }
#[test] #[test]
@ -134,13 +282,13 @@ mod test {
} }
#[test] #[test]
#[ignore] #[should_panic]
fn mv_mul() { fn mv_mul() {
let a: Matrix<f64> = gen_matrix(10, 1).unwrap(); let a: Matrix<f64> = gen_matrix(10, 1).unwrap();
let b: Matrix<f64> = gen_matrix(1, 10).unwrap(); let b: Matrix<f64> = gen_matrix(1, 10).unwrap();
let ab = mul(&a, &b); let ab = mul(&a, &b);
let ba = mul(&b, &a); let ba = mul(&b, &a);
if ab != None && ba != None { if ab.is_some() && ba.is_some() {
assert_eq!(ab.unwrap(), ba.unwrap()) assert_eq!(ab.unwrap(), ba.unwrap())
} else { } else {
assert!(false) assert!(false)
@ -171,4 +319,34 @@ mod test {
let c: Matrix<f64> = gen_matrix(s3, s4).unwrap(); let c: Matrix<f64> = gen_matrix(s3, s4).unwrap();
assert_eq!(mul(&mul(&a, &b).unwrap(), &c).unwrap(), mul(&a, &mul(&b, &c).unwrap()).unwrap()) assert_eq!(mul(&mul(&a, &b).unwrap(), &c).unwrap(), mul(&a, &mul(&b, &c).unwrap()).unwrap())
} }
#[test]
fn basic_det_1() {
let matrix: Matrix<i32> = Matrix(vec![vec![1, -2, -2], vec![5, 1, 3], vec![8, 0, 4]]);
assert_eq!(det(&matrix).unwrap(), 12i32)
}
#[test]
fn basic_det_2() {
let matrix: Matrix<i32> =
Matrix(vec![vec![1, 0, 0, 0], vec![0, 3, 0, 0], vec![0, 0, 1, 0], vec![0, 0, 0, 1]]);
assert_eq!(det(&matrix).unwrap(), 3i32)
}
#[test]
fn basic_det_3() {
let matrix: Matrix<i32> =
Matrix(vec![vec![0, 0, 0, 1], vec![0, 1, 0, 0], vec![0, 0, 1, 0], vec![1, 0, 0, 0]]);
assert_eq!(det(&matrix).unwrap(), -1i32)
}
#[test]
fn basic_det_4() {
let matrix: Matrix<i32> =
Matrix(vec![vec![1, 0, 0, 7], vec![0, 1, 0, 0], vec![0, 0, 1, 0], vec![0, 0, 0, 1]]);
assert_eq!(det(&matrix).unwrap(), 1i32)
}
#[test]
fn basic_det_5() {
let matrix: Matrix<i32> =
Matrix(vec![vec![3, 0, 2, -1], vec![1, 2, 0, -2], vec![4, 0, 6, -3], vec![5, 0, 2, 0]]);
assert_eq!(det(&matrix).unwrap(), 20i32)
}
} }

View file

@ -11,7 +11,7 @@ default = ["optimize"]
[dependencies] [dependencies]
env_logger = "0.10.0" env_logger = "0.10.0"
log = "0.4.17" log = "0.4.17"
matrix = { version = "0.1.2", path = "../matrix" } matrix = { version = "0.2.0", path = "../matrix" }
pixels = "0.11.0" pixels = "0.11.0"
winit = "0.28.2" winit = "0.28.2"
winit_input_helper = "0.14.0" winit_input_helper = "0.14.0"