diff --git a/src/lib.rs b/src/lib.rs index e5eb248..0a019df 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ use num_traits::{Num, NumAssign}; +use core::panic; use std::fmt::{self, Debug}; use std::ops::Add; use std::ops::Sub; @@ -67,6 +68,47 @@ impl Matrix () { + if row1 >= self.rows || row2 >= self.rows { + panic!("Row index is out of bounds."); + } + + //Get copy of row2 + let temp = self.get_row(row2); + let mut index1 = 0; + let mut index2 = 0; + //Move from row1 to row2 + index1 += self.columns * row1; + index2 += self.columns * row2; + for i in 0..self.columns { + self.data[index2 + i] = self.data[index1 + i]; + self.data[index1 + i] = temp[i]; + } + } + + pub fn get_determinant(&self) -> T { + if self.rows != self.columns { + panic!("Only nxn matrixes can have a determinant."); + } + + let mut trig_matrix = Matrix { + columns: self.columns, + rows: self.rows, + data: self.data.clone(), + }; + let mut sign = 1.0; + + for i in 0..self.columns { + let mut pivot = self.get(i, i); + + if pivot.is_zero() { + break; //Skip if pivot is + } + } + + return self.data[0]; + } } impl Add for Matrix {