summaryrefslogtreecommitdiff
path: root/src/types/matrix.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/types/matrix.rs')
-rw-r--r--src/types/matrix.rs65
1 files changed, 42 insertions, 23 deletions
diff --git a/src/types/matrix.rs b/src/types/matrix.rs
index d3b4333..eb0801a 100644
--- a/src/types/matrix.rs
+++ b/src/types/matrix.rs
@@ -1,10 +1,35 @@
use std::{fmt::Display, ops::Add, str::FromStr};
+
+use super::matrix_err::{MatrixSetValueError, ParseMatrixError};
/// Matrix
+#[derive(Debug, PartialEq, Eq)]
pub struct Matrix {
pub nrows: usize,
pub ncols: usize,
pub data: Vec<Vec<i32>>,
}
+
+
+impl FromStr for Matrix {
+ type Err = ParseMatrixError;
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
+ let mut d: Vec<Vec<i32>> = Vec::new();
+ let rows_iter = s.split('\n');
+ for txt in rows_iter {
+ let mut r: Vec<i32> = Vec::new();
+ for ch in txt.split(',') {
+ let parsed = match i32::from_str(ch) {
+ Ok(n) => Ok(n),
+ Err(_e) => Err(ParseMatrixError),
+ };
+ r.push(parsed?);
+ }
+ d.push(r);
+ }
+ Ok(Matrix::new(d))
+ }
+}
+
impl Display for Matrix {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut builder = String::new();
@@ -44,7 +69,11 @@ impl<'a, 'b> Add<&'b Matrix> for &'a Matrix {
x
}
}
+
+
impl Matrix {
+
+ /// Matrix initialiser function
pub fn new(data: Vec<Vec<i32>>) -> Matrix {
Matrix {
nrows: data.len(),
@@ -52,27 +81,19 @@ impl Matrix {
data,
}
}
- pub fn from_str(s: String) -> Matrix {
- let mut d: Vec<Vec<i32>> = Vec::new();
- let rows_iter = s.split('\n');
- for (i, txt) in rows_iter.enumerate() {
- let mut r: Vec<i32> = Vec::new();
- for (j, ch) in txt.split(',').enumerate() {
- // println!("Put {} at {},{}", ch, i, j);
- let parsed = match i32::from_str(ch) {
- Ok(n) => n,
- Err(e) => panic!("Err: {}", e),
- };
- r.push(parsed);
- }
- d.push(r);
- }
- Matrix::new(d)
+ pub fn get(&self, row_index: usize, column_index: usize) -> Option<i32> {
+ let r = self.data.get(row_index)?;
+ let n = r.get(column_index)?;
+ Some(*n)
+ }
+ pub fn set(&mut self, row_index: usize, column_index: usize, new_data: i32) -> Result<(), MatrixSetValueError> {
+ self.data[row_index][column_index] = new_data;
+ Ok(())
}
pub fn is_square(&self) -> bool {
- &self.nrows == &self.ncols
+ self.nrows == self.ncols
}
- pub fn splice(&self, at_index: usize) -> Matrix {
+ fn splice(&self, at_index: usize) -> Matrix {
let mut data: Vec<Vec<i32>> = Vec::new();
for i in 0..self.data.len() {
if i == 0 {
@@ -87,14 +108,12 @@ impl Matrix {
}
data.push(r);
}
- let m = Matrix::new(data);
- // println!("Splice at {}: {}", at_index, m);
- m
+ Matrix::new(data)
}
pub fn determinant(&self) -> i32 {
if !self.is_square() { panic!() };
- if self.nrows == 2 && self.nrows == 2 {
- return &self.data[0][0] * &self.data[1][1] - &self.data[0][1] * &self.data[1][0];
+ if self.nrows == 2 && self.ncols == 2 {
+ return self.data[0][0] * self.data[1][1] - self.data[0][1] * self.data[1][0];
}
let mut tmp = 0;
for (i, n) in self.data[0].iter().enumerate() {