Add generics

Signed-off-by: Ivan Bushchik <ivabus@ivabus.dev>
This commit is contained in:
Ivan Bushchik 2023-03-12 20:06:47 +03:00
parent 98613eff8f
commit e43529b29f
No known key found for this signature in database
GPG key ID: 9F6DDABE11A2674D
5 changed files with 46 additions and 46 deletions

View file

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

View file

@ -1,10 +1,9 @@
// AUGGGGHHHHHHHHHHHHH
// I WANT TO USE GENERICS
// BUT I DON'T KNOW HOW TO USE THEM PROPERLY
// use rand::{thread_rng, Rng};
use std::ops::{Add, AddAssign, Mul, MulAssign};
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();
for i in a {
@ -16,7 +15,7 @@ fn check_valid(a: &Vec<Vec<f64>>) -> bool {
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)) {
return None;
}
@ -25,7 +24,7 @@ pub fn sum(a: &Vec<Vec<f64>>, b: &Vec<Vec<f64>>) -> Option<Vec<Vec<f64>>> {
return None;
}
let mut c: Vec<Vec<f64>> = Vec::new();
let mut c: Matrix<T> = Vec::new();
for i in 0..a.len() {
c.push(vec![]);
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)
}
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)) {
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 mut c: Vec<Vec<f64>> = Vec::new();
let mut c: Matrix<T> = Vec::new();
for i in 0..a.len() {
c.push(vec![]);
for j in 0..b[0].len() {
let mut s = 0.;
let mut s: T = 0_u8.into();
for r in 0..m {
s += a[i][r] * b[r][j];
}
c[i].push(s as f64);
c[i].push(s as T);
}
}
Some(c)
}
/*
#[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) {
return None;
}
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 {
m.push(vec![]);
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)
@ -82,38 +84,38 @@ fn gen_matrix(i: usize, j: usize) -> Option<Vec<Vec<f64>>> {
#[cfg(test)]
mod test {
use rand::{Rng, thread_rng};
use super::*;
use rand::{thread_rng, Rng};
#[test]
fn basic_sum() {
let a = vec![vec![1., 2.], vec![3., 4.]];
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]
fn basic_mult() {
fn basic_mul() {
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.]];
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]
fn mv_sum() {
let mut rng = thread_rng();
let (s1, s2) = (rng.gen_range(2..500), rng.gen_range(2..500));
let a = crate::gen_matrix(s1, s2).unwrap();
let b = crate::gen_matrix(s1, s2).unwrap();
assert_eq!(crate::sum(&a, &b).unwrap(), crate::sum(&b, &a).unwrap())
let a: Matrix<f64> = gen_matrix(s1, s2).unwrap();
let b: Matrix<f64> = gen_matrix(s1, s2).unwrap();
assert_eq!(sum(&a, &b).unwrap(), sum(&b, &a).unwrap())
}
#[test]
#[ignore]
fn mv_mult() {
let a = crate::gen_matrix(10, 1).unwrap();
let b = crate::gen_matrix(1, 10).unwrap();
let ab = crate::mult(&a, &b);
let ba = crate::mult(&b, &a);
fn mv_mul() {
let a: Matrix<f64> = gen_matrix(10, 1).unwrap();
let b: Matrix<f64> = gen_matrix(1, 10).unwrap();
let ab = mul(&a, &b);
let ba = mul(&b, &a);
if ab != None && ba != None {
assert!(ab.unwrap() == ba.unwrap())
} else {
@ -125,17 +127,14 @@ mod test {
fn comb_sum() {
let mut rng = thread_rng();
let (s1, s2) = (rng.gen_range(2..500), rng.gen_range(2..500));
let a = crate::gen_matrix(s1, s2).unwrap();
let b = crate::gen_matrix(s1, s2).unwrap();
let c = crate::gen_matrix(s1, s2).unwrap();
assert_eq!(
crate::sum(&crate::sum(&a, &b).unwrap(), &c).unwrap(),
crate::sum(&a, &crate::sum(&b, &c).unwrap()).unwrap()
)
let a: Matrix<f64> = gen_matrix(s1, s2).unwrap();
let b: Matrix<f64> = gen_matrix(s1, s2).unwrap();
let c: Matrix<f64> = gen_matrix(s1, s2).unwrap();
assert_eq!(sum(&sum(&a, &b).unwrap(), &c).unwrap(), sum(&a, &sum(&b, &c).unwrap()).unwrap())
}
#[test]
fn comb_mult() {
fn comb_mul() {
let mut rng = thread_rng();
let (s1, s2, s3, s4) = (
rng.gen_range(2..500),
@ -143,12 +142,13 @@ mod test {
rng.gen_range(2..500),
rng.gen_range(2..500),
);
let a = crate::gen_matrix(s1, s2).unwrap();
let b = crate::gen_matrix(s2, s3).unwrap();
let c = crate::gen_matrix(s3, s4).unwrap();
let a: Matrix<f64> = gen_matrix(s1, s2).unwrap();
let b: Matrix<f64> = gen_matrix(s2, s3).unwrap();
let c: Matrix<f64> = gen_matrix(s3, s4).unwrap();
assert_eq!(
crate::mult(&crate::mult(&a, &b).unwrap(), &c).unwrap(),
crate::mult(&a, &crate::mult(&b, &c).unwrap()).unwrap()
mul(&mul(&a, &b).unwrap(), &c).unwrap(),
mul(&a, &mul(&b, &c).unwrap()).unwrap()
)
}
}
*/

View file

@ -1,6 +1,6 @@
[package]
name = "matrix_graphics"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
license = "MIT"

View file

@ -14,7 +14,7 @@ mod structs;
const WIDTH: u32 = 640;
const HEIGHT: u32 = 480;
const POLYGON: usize = 7;
const POLYGON: usize = 4;
// POLYGON >= 2
const CENTER_X: u32 = 320;
const CENTER_Y: u32 = 240;

View file

@ -48,8 +48,8 @@ impl Line {
vec![(self.end_x - self.rotate_center_x)],
vec![(self.end_y - self.rotate_center_y)],
];
let res_start = matrix::mult(&matrix_rotate, &matrix_start).unwrap();
let res_end = matrix::mult(&matrix_rotate, &matrix_end).unwrap();
let res_start = matrix::mul(&matrix_rotate, &matrix_start).unwrap();
let res_end = matrix::mul(&matrix_rotate, &matrix_end).unwrap();
(self.start_x, self.start_y) =
(res_start[0][0] + self.rotate_center_x, res_start[1][0] + self.rotate_center_y);
(self.end_x, self.end_y) =