feat(matrix_op): add multiplication
matrix_ops: add matrix multiply capability chore(fmt): flatten crate directory tests(ops): add tests for multiplication
This commit is contained in:
parent
0412ac54d5
commit
333c6c7281
7 changed files with 70 additions and 40 deletions
11
src/lib.rs
11
src/lib.rs
|
|
@ -9,20 +9,23 @@
|
||||||
//!
|
//!
|
||||||
//! Examples:
|
//! Examples:
|
||||||
//! ```
|
//! ```
|
||||||
//! ...
|
|
||||||
//! use matrix::Matrix;
|
//! use matrix::Matrix;
|
||||||
//! let m = Matrix::from_str("1,2,3\n4,5,6\n7,8,9");
|
//! use std::str::FromStr;
|
||||||
|
//! let m = Matrix::from_str("1,2,3\n4,5,6\n7,8,9").expect("Expected this to work");
|
||||||
//! println!("Matrix string formatting:\n{}", m);
|
//! println!("Matrix string formatting:\n{}", m);
|
||||||
//! println!("Evaluate determinant of matrix: {}", m.determinant());
|
//! println!("Evaluate determinant of matrix: {}", m.determinant());
|
||||||
//! println!("Transpose of matrix m:\n{}", m.transpose());
|
//! println!("Transpose of matrix m:\n{}", m.transpose());
|
||||||
//! ...
|
//!
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
pub mod types;
|
mod matrix;
|
||||||
|
pub mod error;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
||||||
|
pub use matrix::Matrix;
|
||||||
|
|
||||||
pub fn test() {
|
pub fn test() {
|
||||||
println!("Testing code here");
|
println!("Testing code here");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,16 +3,17 @@
|
||||||
//! Example usage - addition of two matrices:
|
//! Example usage - addition of two matrices:
|
||||||
//! ```
|
//! ```
|
||||||
//! use matrix::Matrix;
|
//! use matrix::Matrix;
|
||||||
//! let m1 = Matrix::from_str("1,1,1\n1,1,1\n1,1,1");
|
//! use std::str::FromStr;
|
||||||
//! let m2 = Matrix::from_str("2,2,2\n2,2,2\n2,2,2");
|
//! let m1 = Matrix::from_str("1,2\n3,4").expect("Expect parse correct");
|
||||||
//! println!("Sum of m1 + m2: \n{}", m1 + m2);
|
//! let m2 = Matrix::from_str("1,1\n1,1").expect("Expect parse correct");
|
||||||
|
//! let m_add = &m1 + &m2;
|
||||||
|
//! println!("m1 + m2 =\n{}", m_add);
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
|
||||||
//! TODO:: Create matrix multiplication method
|
//! TODO:: Create matrix multiplication method
|
||||||
|
|
||||||
use std::{fmt::Display, ops::{Add, Mul, Sub}, str::FromStr};
|
use std::{fmt::Display, ops::{Add, Mul, Sub}, str::FromStr};
|
||||||
|
|
||||||
use super::matrix_err::{MatrixSetValueError, ParseMatrixError};
|
use crate::error::{MatrixSetValueError, ParseMatrixError};
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub struct Matrix {
|
pub struct Matrix {
|
||||||
/// Number of rows in matrix.
|
/// Number of rows in matrix.
|
||||||
|
|
@ -91,11 +92,31 @@ impl<'a, 'b> Sub<&'b Matrix> for &'a Matrix {
|
||||||
fn sub(self, rhs: &'b Matrix) -> Self::Output {
|
fn sub(self, rhs: &'b Matrix) -> Self::Output {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<'a, 'b> Mul<&'b Matrix> for &'a Matrix {
|
impl<'a, 'b> Mul<&'b Matrix> for &'a Matrix {
|
||||||
type Output = Matrix;
|
type Output = Matrix;
|
||||||
fn mul(self, rhs: &'b Matrix) -> Self::Output {
|
fn mul(self, rhs: &'b Matrix) -> Self::Output {
|
||||||
todo!()
|
fn reduce(lhs: &Matrix, rhs: &Matrix, at_r: usize, at_c: usize) -> i32 {
|
||||||
|
let mut tmp = 0;
|
||||||
|
for i in 0..lhs.ncols {
|
||||||
|
tmp += lhs.get(at_r, i).unwrap() * rhs.get(i, at_c).unwrap();
|
||||||
|
}
|
||||||
|
tmp
|
||||||
|
}
|
||||||
|
let mut d: Vec<Vec<i32>> = Vec::new();
|
||||||
|
if self.ncols != rhs.nrows {
|
||||||
|
println!("LHS: \n{}RHS: \n{}", self, rhs);
|
||||||
|
println!("LHS nrows: {} ;; RHS ncols: {}", self.nrows, rhs.ncols);
|
||||||
|
panic!()
|
||||||
|
}
|
||||||
|
for i in 0..self.nrows {
|
||||||
|
let mut r: Vec<i32> = Vec::new();
|
||||||
|
for j in 0..rhs.ncols {
|
||||||
|
r.push(reduce(self, rhs, i, j));
|
||||||
|
}
|
||||||
|
d.push(r);
|
||||||
|
}
|
||||||
|
Matrix::new(d)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1,2 +1,3 @@
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub mod matrix_test;
|
mod matrix_test_parse;
|
||||||
|
mod matrix_test_ops;
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,7 @@
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use crate::types::{matrix::Matrix, matrix_err::ParseMatrixError};
|
use crate::{matrix::Matrix, error::ParseMatrixError};
|
||||||
|
|
||||||
#[test]
|
|
||||||
pub fn test_matrix_init_from_string() -> Result<(), ParseMatrixError> {
|
|
||||||
let data_target = vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]];
|
|
||||||
let target = Matrix {
|
|
||||||
nrows: 3,
|
|
||||||
ncols: 3,
|
|
||||||
data: data_target,
|
|
||||||
};
|
|
||||||
let test = Matrix::from_str("1,2,3\n4,5,6\n7,8,9")?;
|
|
||||||
assert_eq!(target, test);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_matrix_add() -> Result<(), ParseMatrixError> {
|
pub fn test_matrix_add() -> Result<(), ParseMatrixError> {
|
||||||
let m1 = Matrix::from_str("1,2,3\n4,5,6\n7,8,9")?;
|
let m1 = Matrix::from_str("1,2,3\n4,5,6\n7,8,9")?;
|
||||||
|
|
@ -37,13 +25,16 @@ pub fn test_matrix_transposition() -> Result<(), ParseMatrixError> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
pub fn test_matrix_parse_malformed() -> () {
|
pub fn test_matrix_mul() -> Result<(), ParseMatrixError> {
|
||||||
let malformed = "1,23,\n,567,\n\n5";
|
let m1 = Matrix::from_str("1,2\n1,2")?;
|
||||||
let m = Matrix::from_str(malformed);
|
let m2 = Matrix::from_str("1,3\n2,4")?;
|
||||||
match m {
|
let m3 = Matrix::from_str("1,2\n3,4")?;
|
||||||
Ok(_) => panic!("This malformed matrix string should not have succeeded"),
|
let m4 = Matrix::from_str("1\n2")?;
|
||||||
Err(_) => (),
|
let t1 = Matrix::from_str("5,11\n5,11")?;
|
||||||
}
|
let t2 = Matrix::from_str("5\n11")?;
|
||||||
|
assert_eq!(&m1 * &m2, t1);
|
||||||
|
assert_eq!(&m3 * &m4, t2);
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
22
src/tests/matrix_test_parse.rs
Normal file
22
src/tests/matrix_test_parse.rs
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
use crate::{matrix::Matrix, error::ParseMatrixError};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_matrix_init_from_string() -> Result<(), ParseMatrixError> {
|
||||||
|
let data_target = vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]];
|
||||||
|
let target = Matrix::new(data_target);
|
||||||
|
let test = Matrix::from_str("1,2,3\n4,5,6\n7,8,9")?;
|
||||||
|
assert_eq!(target, test);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
pub fn test_matrix_parse_malformed() -> () {
|
||||||
|
let malformed = "1,23,\n,567,\n\n5";
|
||||||
|
let m = Matrix::from_str(malformed);
|
||||||
|
match m {
|
||||||
|
Ok(_) => panic!("This malformed matrix string should not have succeeded"),
|
||||||
|
Err(_) => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -1,8 +0,0 @@
|
||||||
//! Matrix-related type definitions
|
|
||||||
//!
|
|
||||||
//! Includes modules:
|
|
||||||
//! - Matrix
|
|
||||||
//! - Matrix parse and arithmetic errors
|
|
||||||
|
|
||||||
pub mod matrix;
|
|
||||||
pub mod matrix_err;
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue