diff --git a/src/matrix.rs b/src/matrix.rs deleted file mode 100644 index a98064b..0000000 --- a/src/matrix.rs +++ /dev/null @@ -1,41 +0,0 @@ -//A matrix is constructed by chaining together elements - -//Before even a struct, we make an enum! -//The enum can allow us to set Number or Nothing, -//Stopping the infinite size issues - -//Element : either a Number or Nothing - -//Number : a node that containes a value, -// and points to other elements - - - -struct Number { - value : isize, - // the value to the right - // By putting the value in a Box<>, we point to a - right : Option>, - // the value below - below : Option> - -} - -struct Matrix { - size : (usize, usize), - elements : Vec> -} - - -impl Matrix { - pub fn new(rows : usize, columns : usize) -> Matrix { - //Construct an empty vector - let mut elements : Vec> = vec![vec![0;columns];rows]; - - Matrix { - size : (rows , columns), - elements : elements - } - } - -} diff --git a/src/matrix/addition.rs b/src/matrix/addition.rs new file mode 100644 index 0000000..3dfd291 --- /dev/null +++ b/src/matrix/addition.rs @@ -0,0 +1,12 @@ +use super::Matrix; +use std::ops::Add; + +impl Add for Matrix { + type Output = Matrix; + + fn add(self, other: Matrix) -> Matrix { + + + } + +} diff --git a/src/matrix/mod.rs b/src/matrix/mod.rs new file mode 100644 index 0000000..4cff922 --- /dev/null +++ b/src/matrix/mod.rs @@ -0,0 +1,109 @@ +use std::ops::{Add, Index, IndexMut}; + +// TODO: create a generic type that has some restrictions: +// Needs to implement the add trait +struct Matrix { + size : (usize, usize), + elements : Vec> +} + + +impl Matrix { + //Creates a matrix struct + pub fn new(rows : usize, columns : usize) -> Matrix { + + //Construct an empty vector + let elements : Vec> = vec![vec![0;columns];rows]; + + //Construct the struct + Matrix { + size : (rows , columns), + elements + } + } + + // Gets the size of the matrix + pub fn size(& self) -> (usize, usize) { + self.size + } +} +// implementation of the fmt::Display trait + +// implementation of index ([]) +// Technically ([]) is unsafe too, but... +// I probably should just let the vector handle the error + +// After doing more research, I can instead use get in order to make the output +// an Option<> +impl Index<(usize, usize)> for Matrix { + type Output = Option; + + fn index(&self, index: (usize, usize)) -> &Self::Output { + //Since it returns an Option, its not as simple as a get + let row : Option<&Vec> = self.elements.get(index.0); //the 'T' in Option from get is a reference + //Now, we need to unpack the Option + match row + } +} + +//mutable variant - so we can modify the entries +impl IndexMut<(usize, usize)> for Matrix { + fn index_mut(&mut self, index: (usize, usize)) -> &mut Self::Output { + &mut self.elements[index.0][index.1] + } +} + + +// implementation of add (+) +// (+) is unsafe; they could add two matricies of different sizes +// that's undefined behavior. SO, we're going to pass an error if that happens +impl Add for Matrix { + type Output = Result; + + fn add(self, rhs: Self) -> Result { + // Returns Err if they are not the same size + if self.size() != rhs.size() { + return Err("Both matricies must be of the same size!".to_string()); + } + + // Makes a new matrix + let mut return_matrix = Matrix::new(self.size.0, self.size.1); + + //Go through each element + for r in 0..self.size().0{ + for c in 0..self.size().1 { + //Can't assign it??? + //Ahhhhh ok, so I must implement both the immutable and mutable versions of an + //index before I am able to call it + return_matrix[(r,c)] = self[(r,c)] + rhs[(r,c)]; + } + } + + Ok(return_matrix) + } +} + +// implementation of eq (==) +impl PartialEq for Matrix { + fn eq(& self, other : &Matrix) -> bool { + + //First check if they are the same size + if self.size != other.size { + return false; + } + + //Next check if each element is the same + //We dont need to check EVERY element, just each vector + let rows = self.size.1; + + for i in 0..rows { + //Oh nice, it implements PartialEq. I would have to check + //in C++ normally, and it would probably be the same address of stuff + if self.elements[i] != other.elements[i] { + return false; + } + } + true + } +} +