/** *Overloading the '!=' operator. *Returns a bool value whether the matrices are not equal * */ bool operator!=(const SMatrix& a, const SMatrix& b) { bool notEqual = false; if(a.rows() != b.rows() || a.cols() != b.cols()) { //If the matrices have different notEqual = true; //dimensions } else { b.begin(); for(a.begin(); !a.end() && !notEqual; a.next()) { //Compare all values of a to b until if(a.value() != b.value()) { //not matching values are notEqual = true; //found } b.next(); } } return notEqual; }
/** *Overloading the '==' operator. *Returns a bool value whether the matrices are equal * */ bool operator==(const SMatrix& a, const SMatrix& b) { bool equal = true; if(a.rows() != b.rows() || a.cols() != b.cols()) { //If the matrices do not have the equal = false; //same dimensions } else { b.begin(); for(a.begin(); !a.end(); a.next()) { //compare all the values of a to b if(a.value() != b.value()) { equal = false; } b.next(); } } return equal; }
/** *Overloading the '-' operator. Only works when both matrices have equal dimensions *Returns a new matrix equal to a - b * */ SMatrix operator-(const SMatrix& a, const SMatrix& b) throw(MatrixError) { if(a.cols() != b.cols() || a.rows() != b.rows()) { //Check whether or not dimensions are equal throw MatrixError("Matrix size error"); //if not throw size error } SMatrix c(a.rows(),a.cols()); //dimensions of new matrix b.begin(); for(a.begin(); !a.end(); a.next()) { //Loop through all values of a and b, and subtract int val = a.value() - b.value(); c.setVal(a.rowPointer_,a.colPointer_,val); //add new subtraction to new matrix b.next(); } return c; }