mirror of
https://github.com/ivabus/matrix
synced 2024-11-10 02:25:18 +03:00
Add generics
Signed-off-by: Ivan Bushchik <ivabus@ivabus.dev>
This commit is contained in:
parent
98613eff8f
commit
e43529b29f
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "matrix"
|
name = "matrix"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
// AUGGGGHHHHHHHHHHHHH
|
// use rand::{thread_rng, Rng};
|
||||||
// I WANT TO USE GENERICS
|
use std::ops::{Add, AddAssign, Mul, MulAssign};
|
||||||
// BUT I DON'T KNOW HOW TO USE THEM PROPERLY
|
|
||||||
|
|
||||||
use rand::{Rng, thread_rng};
|
pub(crate) type Matrix<T> = Vec<Vec<T>>;
|
||||||
|
|
||||||
fn check_valid(a: &Vec<Vec<f64>>) -> bool {
|
fn check_valid<T>(a: &Matrix<T>) -> bool {
|
||||||
let len = a[0].len();
|
let len = a[0].len();
|
||||||
|
|
||||||
for i in a {
|
for i in a {
|
||||||
|
@ -16,7 +15,7 @@ fn check_valid(a: &Vec<Vec<f64>>) -> bool {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sum(a: &Vec<Vec<f64>>, b: &Vec<Vec<f64>>) -> Option<Vec<Vec<f64>>> {
|
pub fn sum<T: Add<Output = T> + Copy>(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,7 +24,7 @@ pub fn sum(a: &Vec<Vec<f64>>, b: &Vec<Vec<f64>>) -> Option<Vec<Vec<f64>>> {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut c: Vec<Vec<f64>> = Vec::new();
|
let mut c: Matrix<T> = Vec::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].len() {
|
||||||
|
@ -36,7 +35,10 @@ pub fn sum(a: &Vec<Vec<f64>>, b: &Vec<Vec<f64>>) -> Option<Vec<Vec<f64>>> {
|
||||||
Some(c)
|
Some(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn mult(a: &Vec<Vec<f64>>, b: &Vec<Vec<f64>>) -> Option<Vec<Vec<f64>>> {
|
pub fn mul<T: Mul<Output = T> + Copy + AddAssign + MulAssign + std::fmt::Display + From<u8>>(
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
@ -46,35 +48,35 @@ pub fn mult(a: &Vec<Vec<f64>>, b: &Vec<Vec<f64>>) -> Option<Vec<Vec<f64>>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
let m = a[0].len();
|
let m = a[0].len();
|
||||||
let mut c: Vec<Vec<f64>> = Vec::new();
|
let mut c: Matrix<T> = Vec::new();
|
||||||
|
|
||||||
for i in 0..a.len() {
|
for i in 0..a.len() {
|
||||||
c.push(vec![]);
|
c.push(vec![]);
|
||||||
for j in 0..b[0].len() {
|
for j in 0..b[0].len() {
|
||||||
let mut s = 0.;
|
let mut s: T = 0_u8.into();
|
||||||
for r in 0..m {
|
for r in 0..m {
|
||||||
s += a[i][r] * b[r][j];
|
s += a[i][r] * b[r][j];
|
||||||
}
|
}
|
||||||
c[i].push(s as f64);
|
c[i].push(s as T);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(c)
|
Some(c)
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
fn gen_matrix(i: usize, j: usize) -> Option<Vec<Vec<f64>>> {
|
fn gen_matrix<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: Vec<Vec<f64>> = Vec::new();
|
let mut m: Matrix<T> = Vec::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(rng.gen_range(-100..100) as f64);
|
m[a].push(rng.gen_range(0..100) as T);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(m)
|
Some(m)
|
||||||
|
@ -82,38 +84,38 @@ fn gen_matrix(i: usize, j: usize) -> Option<Vec<Vec<f64>>> {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use rand::{Rng, thread_rng};
|
use super::*;
|
||||||
|
use rand::{thread_rng, Rng};
|
||||||
#[test]
|
#[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!(crate::sum(&a, &b).unwrap(), vec![vec![6., 8.], vec![10., 12.]])
|
assert_eq!(sum(&a, &b).unwrap(), vec![vec![6., 8.], vec![10., 12.]])
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn basic_mult() {
|
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!(crate::mult(&a, &b).unwrap(), vec![vec![21., -5.], vec![46., 8.], vec![4., 4.]])
|
assert_eq!(mul(&a, &b).unwrap(), vec![vec![21., -5.], vec![46., 8.], vec![4., 4.]])
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn mv_sum() {
|
fn mv_sum() {
|
||||||
let mut rng = thread_rng();
|
let mut rng = thread_rng();
|
||||||
let (s1, s2) = (rng.gen_range(2..500), rng.gen_range(2..500));
|
let (s1, s2) = (rng.gen_range(2..500), rng.gen_range(2..500));
|
||||||
let a = crate::gen_matrix(s1, s2).unwrap();
|
let a: Matrix<f64> = gen_matrix(s1, s2).unwrap();
|
||||||
let b = crate::gen_matrix(s1, s2).unwrap();
|
let b: Matrix<f64> = gen_matrix(s1, s2).unwrap();
|
||||||
assert_eq!(crate::sum(&a, &b).unwrap(), crate::sum(&b, &a).unwrap())
|
assert_eq!(sum(&a, &b).unwrap(), sum(&b, &a).unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
#[ignore]
|
||||||
fn mv_mult() {
|
fn mv_mul() {
|
||||||
let a = crate::gen_matrix(10, 1).unwrap();
|
let a: Matrix<f64> = gen_matrix(10, 1).unwrap();
|
||||||
let b = crate::gen_matrix(1, 10).unwrap();
|
let b: Matrix<f64> = gen_matrix(1, 10).unwrap();
|
||||||
let ab = crate::mult(&a, &b);
|
let ab = mul(&a, &b);
|
||||||
let ba = crate::mult(&b, &a);
|
let ba = mul(&b, &a);
|
||||||
if ab != None && ba != None {
|
if ab != None && ba != None {
|
||||||
assert!(ab.unwrap() == ba.unwrap())
|
assert!(ab.unwrap() == ba.unwrap())
|
||||||
} else {
|
} else {
|
||||||
|
@ -125,17 +127,14 @@ mod test {
|
||||||
fn comb_sum() {
|
fn comb_sum() {
|
||||||
let mut rng = thread_rng();
|
let mut rng = thread_rng();
|
||||||
let (s1, s2) = (rng.gen_range(2..500), rng.gen_range(2..500));
|
let (s1, s2) = (rng.gen_range(2..500), rng.gen_range(2..500));
|
||||||
let a = crate::gen_matrix(s1, s2).unwrap();
|
let a: Matrix<f64> = gen_matrix(s1, s2).unwrap();
|
||||||
let b = crate::gen_matrix(s1, s2).unwrap();
|
let b: Matrix<f64> = gen_matrix(s1, s2).unwrap();
|
||||||
let c = crate::gen_matrix(s1, s2).unwrap();
|
let c: Matrix<f64> = gen_matrix(s1, s2).unwrap();
|
||||||
assert_eq!(
|
assert_eq!(sum(&sum(&a, &b).unwrap(), &c).unwrap(), sum(&a, &sum(&b, &c).unwrap()).unwrap())
|
||||||
crate::sum(&crate::sum(&a, &b).unwrap(), &c).unwrap(),
|
|
||||||
crate::sum(&a, &crate::sum(&b, &c).unwrap()).unwrap()
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn comb_mult() {
|
fn comb_mul() {
|
||||||
let mut rng = thread_rng();
|
let mut rng = thread_rng();
|
||||||
let (s1, s2, s3, s4) = (
|
let (s1, s2, s3, s4) = (
|
||||||
rng.gen_range(2..500),
|
rng.gen_range(2..500),
|
||||||
|
@ -143,12 +142,13 @@ mod test {
|
||||||
rng.gen_range(2..500),
|
rng.gen_range(2..500),
|
||||||
rng.gen_range(2..500),
|
rng.gen_range(2..500),
|
||||||
);
|
);
|
||||||
let a = crate::gen_matrix(s1, s2).unwrap();
|
let a: Matrix<f64> = gen_matrix(s1, s2).unwrap();
|
||||||
let b = crate::gen_matrix(s2, s3).unwrap();
|
let b: Matrix<f64> = gen_matrix(s2, s3).unwrap();
|
||||||
let c = crate::gen_matrix(s3, s4).unwrap();
|
let c: Matrix<f64> = gen_matrix(s3, s4).unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
crate::mult(&crate::mult(&a, &b).unwrap(), &c).unwrap(),
|
mul(&mul(&a, &b).unwrap(), &c).unwrap(),
|
||||||
crate::mult(&a, &crate::mult(&b, &c).unwrap()).unwrap()
|
mul(&a, &mul(&b, &c).unwrap()).unwrap()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "matrix_graphics"
|
name = "matrix_graphics"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ mod structs;
|
||||||
|
|
||||||
const WIDTH: u32 = 640;
|
const WIDTH: u32 = 640;
|
||||||
const HEIGHT: u32 = 480;
|
const HEIGHT: u32 = 480;
|
||||||
const POLYGON: usize = 7;
|
const POLYGON: usize = 4;
|
||||||
// POLYGON >= 2
|
// POLYGON >= 2
|
||||||
const CENTER_X: u32 = 320;
|
const CENTER_X: u32 = 320;
|
||||||
const CENTER_Y: u32 = 240;
|
const CENTER_Y: u32 = 240;
|
||||||
|
|
|
@ -48,8 +48,8 @@ impl Line {
|
||||||
vec![(self.end_x - self.rotate_center_x)],
|
vec![(self.end_x - self.rotate_center_x)],
|
||||||
vec![(self.end_y - self.rotate_center_y)],
|
vec![(self.end_y - self.rotate_center_y)],
|
||||||
];
|
];
|
||||||
let res_start = matrix::mult(&matrix_rotate, &matrix_start).unwrap();
|
let res_start = matrix::mul(&matrix_rotate, &matrix_start).unwrap();
|
||||||
let res_end = matrix::mult(&matrix_rotate, &matrix_end).unwrap();
|
let res_end = matrix::mul(&matrix_rotate, &matrix_end).unwrap();
|
||||||
(self.start_x, self.start_y) =
|
(self.start_x, self.start_y) =
|
||||||
(res_start[0][0] + self.rotate_center_x, res_start[1][0] + self.rotate_center_y);
|
(res_start[0][0] + self.rotate_center_x, res_start[1][0] + self.rotate_center_y);
|
||||||
(self.end_x, self.end_y) =
|
(self.end_x, self.end_y) =
|
||||||
|
|
Loading…
Reference in a new issue