Ejemplo n.º 1
0
SparseMatrix SparseMatrix::Multiply(SparseMatrix b)
//Multiply two sparse matrices @A@ (\(**\fBthis\fR) and @B@ producing @D@.
{
  if (Cols != b.Rows) {
  cout << "Incompatible matrices" << endl;
  return EmptyMatrix();
  }

  if ((Terms == MaxTerms) || (b.Terms == MaxTerms))
  {
      cout << "One additional space in @a@ or @b@ needed" << endl;
      return EmptyMatrix();
  }
  SparseMatrix bXpose = b.FastTranspose();
  SparseMatrix result;
  int currRowIndex = 0, LastD = -1, currRowBegin = 0, currRowA = smArray[0].row;
  // set boundary conditions
  smArray[Terms].row = Rows; bXpose.smArray[b.Terms].row = b.Cols;
  bXpose.smArray[b.Terms].col = -1; int sum = 0;
  while (currRowIndex < Terms) // generate row @currRowA@ of @result@
  {
      int currColB = bXpose.smArray[0].row; int currColIndex = 0;
	while (currColIndex <= b.Terms) // multiply row @currRowA@ of @a@ by column \fIcurrColB\fP of @b@
	{
 //	   cout << "currRowIndex:" << currRowIndex << " currColIndex:" << currColIndex << " currRowA:" << currRowA << " currColB:" << currColB << endl;
	   if(smArray[currRowIndex].row != currRowA) // end of row @currRowA@
	   {
//	      cout << "1: new col" << endl;
	      result.StoreSum(sum, LastD,currRowA,currColB);
	      currRowIndex = currRowBegin;
	      // go to next column
	      while (bXpose.smArray[currColIndex].row == currColB) currColIndex++;
	      currColB = bXpose.smArray[currColIndex].row;
	   }
	   else if (bXpose.smArray[currColIndex].row != currColB) // end of column \fIcurrColB\fP of @b@
	   {
//	      cout << "2: new row" << endl;
	      result.StoreSum(sum,LastD,currRowA,currColB);
	      // set to multiply row @currRowA@ with next column
	      currRowIndex = currRowBegin; currColB = bXpose.smArray[currColIndex].row;
	   }
	   else
	      switch (compare (smArray[currRowIndex].col, bXpose.smArray[currColIndex].col)) {
		 case '<':   // advance to next term in row.
//		     cout << "3a: next term in row" << endl;
		     currRowIndex++; break;
		 case '=':   // add to \fIsum\fP
//		     cout << "3b:  add to sum" << endl;
		     sum += smArray[currRowIndex].value * bXpose.smArray[currColIndex].value;
		     currRowIndex++; currColIndex++; break;
		 case '>': // advance to next term in column \fIc\fP
//		     cout << "3c: next term in col" << endl;
		     currColIndex++;
	      } // end of switch
	} // of \fBwhile\fP (@currColIndex@ <= @b.Terms)@
	while (smArray[currRowIndex].row == currRowA) // advance to next row
	  currRowIndex++;
	currRowBegin = currRowIndex; currRowA = smArray[currRowIndex].row;
  } //end of \fBwhile\fP (@currRowIndex@ < @Terms@)
  result.Rows = Rows; result.Cols = b.Cols; result.Terms =  LastD+1;
  return result;
} // of @Multiply@