Esempio n. 1
0
/**
 *Overloading the '*' operator. Only works when matrix a's columns are equal to matrix b's 
 *rows, otherwise there is a size error.
 *Returns a new matrix equal to a * b
 *
 */
SMatrix operator*(const SMatrix& a, const SMatrix& b) throw(MatrixError) {
   if(a.cols() != b.rows()) {    //Check whether a's columns are equal to b's rows
      throw MatrixError("Matrix size error");      //if not throw size error
   } 
   
   SMatrix c(a.rows(),b.cols());    //dimensions of new matrix 
   
   for(auto it = a.ridx_.cbegin();it != a.ridx_.cend();++it) {    //Loop through a's rows
      int row = it->first;
      int nElements = it->second.first + it->second.second;
      for(SMatrix::size_type i = 0; i < b.cols(); ++i) {    //Loop through b's columns
         int col = i;   
         int val = 0;
         for(int pos = it->second.first; pos < nElements; ++pos) {   //Loop through a's columns
            val += a.vals_[pos] * b(a.cidx_[pos],i);      //adding the multiplications to 
         }                                                  //val
         if(val != 0) {
            c.setVal(row,col,val);
         }

      }
   }

   return c;
}
Esempio n. 2
0
/**
 *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;   
}
Esempio n. 3
0
/**
 *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;   
}
Esempio n. 4
0
/**
 *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;  
}
Esempio n. 5
0
/**
 *Transpose method, that transposes a matrix.
 *Returns a new matrix equal to the transpose of a.
 */
SMatrix transpose(const SMatrix& a) {

   SMatrix c(a.rows(),a.cols());
   
   for(auto it = a.ridx_.cbegin();it != a.ridx_.cend();++it) { //Loop through a's rows
      int row = it->first;
      int nElements = it->second.first + it->second.second;
      for(int pos = it->second.first; pos < nElements; ++pos) {   //Loop through a's columns
         c.setVal(a.cidx_[pos],row,a.vals_[pos]);     //add value to new matrix                  
      }                                               //with rows and columns swapped
   }
   return c;
}