Untitled

mail@pastecode.io avatar
unknown
c_cpp
3 years ago
3.1 kB
2
Indexable
Never
void SparseMatrix::ChangeSize1D(const int newSize)
{ // change the array size to newSize
    if (newSize < terms)
        cout << "New size must be >= number of terms";

    MatrixTerm *temp = new MatrixTerm[newSize];
    // new array

    copy(smArray, smArray + terms, temp);
    delete [] smArray;

    smArray = temp;
    // make smArray point to the newly created array
    capacity = newSize;
}


void SparseMatrix::StoreSum(const int sum, const int r, const int c)
{
    if (sum != 0) {
        if (terms == capacity)
            ChangeSize1D(2*capacity);
        smArray[terms].row = r;
        smArray[terms].col = c;
        smArray[terms++].value = sum;
    }
}


SparseMatrix SparseMatrix::Multiply(SparseMatrix b)
{
  if (cols != b.rows) // error handling
    cout << "Incompatible matrices";

  SparseMatrix bXpose = b.Transpose();   // transpose b
  SparseMatrix d(rows, b.cols, 0);  // create the output matrix d
  int currRowIndex = 0,
    currRowBegin = 0,
    currRowA = smArray[0].row;

  // introduce dummy terms for handling boundary condition
  if (terms == capacity)
    ChangeSize1D(terms + 1);

  // introduce dummy terms for handling boundary condition
  bXpose.ChangeSize1D(bXpose.terms + 1);
  smArray[terms].row = rows;
  bXpose.smArray[b.terms].row = b.cols;
  bXpose.smArray[b.terms].col = -1;
  int sum = 0;
  while (currRowIndex < terms) { // check currRowA is valid
    int currColB = bXpose.smArray[0].row;
    int currColIndex = 0;
    while (currColIndex <= b.terms){ // process B matrix term by term
      if (smArray[currRowIndex].row != currRowA) {  // row end
        d.StoreSum(sum,currRowA,currColB);   // store the sum
        sum = 0;     // reset the sum
        currRowIndex = currRowBegin; // rewind the row

        while (bXpose.smArray[currColIndex].row == currColB)
          currColIndex++;   // skip terms in the curr col

        currColB = bXpose.smArray[currColIndex].row;  // next col
      } else if (bXpose.smArray[currColIndex].row != currColB) {
        // col end
        d.StoreSum(sum,currRowA,currColB); // output the sum
        sum = 0; // reset the sum
        currRowIndex = currRowBegin; //rewind the row
        currColB = bXpose.smArray[currColIndex].row; // next col
      }
      else {
        if (smArray[currRowIndex].col <
            bXpose.smArray[currColIndex].col)
          currRowIndex++;
        else if (smArray[currRowIndex].col ==
                 bXpose.smArray[currColIndex].col) {
          sum += smArray[currRowIndex].value *
                 bXpose.smArray[currColIndex].value;
          currRowIndex++;
          currColIndex++;
        }
        else
          currColIndex++;
      } // end of if-elseif-else
    } // end of the inner while (currColIndex <= b.terms)
    while (smArray[currRowIndex].row == currRowA)
      currRowIndex++;  // skip terms in the curr row
    currRowBegin = currRowIndex;  //next row
    currRowA = smArray[currRowIndex].row; //next row
  } // end of the outer while (currRowIndex < terms)
  return d;
}