SHARE
TWEET

Untitled

a guest Mar 22nd, 2019 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include"SparseMatrix.h"
  2.  
  3. void SparseMatrix::setAt(unsigned row, unsigned col, double val) {
  4.     rangeCheck(row, col);
  5.     // YOUR CODE HERE
  6.     unsigned index = row * ncol + col;
  7.  
  8.     if (val == 0)
  9.         return;
  10.  
  11.     Element e;
  12.     e.row = row;
  13.     e.col = col;
  14.     e.val = val;
  15.  
  16.     if (size == 0) { //moi khoi tao mang
  17.         add(e);
  18.         return;
  19.     }
  20.     else { //mang co it nhat 1 element
  21.         for (int i = 0; i < size; i++) {
  22.             int idx1D = (storage[i].row* ncol + storage[i].col);
  23.             if (idx1D > index) {
  24.                 insertAt(i, e);
  25.                 return;
  26.             }
  27.             else if (idx1D == index) {
  28.                 storage[i].val = val;
  29.                 return;
  30.             }
  31.         }
  32.         add(e);
  33.     }
  34. }  
  35.  
  36. double SparseMatrix::getAt(unsigned row, unsigned col) {
  37.     rangeCheck(row, col);
  38.     // YOUR CODE HERE
  39.     for (int i = 0; i < size; i++) {
  40.         if (row == storage[i].row && col == storage[i].col) {
  41.             return storage[i].val;
  42.         }
  43.     }
  44.     return 0;
  45.    
  46. }
  47.  
  48. SparseMatrix* SparseMatrix::transpose() {
  49.     SparseMatrix* result = NULL;
  50.     // YOUR CODE HERE
  51.     for (int i = 0; i < size; i++) {
  52.         Element temp;
  53.         temp.row = storage[i].col;
  54.         temp.col = storage[i].row;
  55.         temp.val = storage[i].val;
  56.         result->add(temp);
  57.     }
  58.     return result;
  59. }
  60.  
  61. double SparseMatrix::trace() {
  62.     if (nrow != ncol)
  63.         throw "TraceOfNoneSquareMatrix";
  64.     double result = 0;
  65.     // YOUR CODE HERE
  66.     for (int i = 0; i < size; i++)
  67.         if (storage[i].col == storage[i].row)
  68.             result += storage[i].val;
  69.     return result;
  70. }
  71.  
  72. SparseMatrix* SparseMatrix::add(const SparseMatrix& b) {
  73.     if(nrow != b.nrow || ncol != b.ncol)
  74.         throw "MismatchedDimensions";
  75.  
  76.  
  77.     SparseMatrix* result = new SparseMatrix(b);
  78.     // YOUR CODE HERE
  79.     for (int i = 0; i < nrow; i++) {
  80.         for (int j = 0; j < ncol; j++)  {
  81.             result->setAt(i, j, getAt(i, j) + result->getAt(i,j));
  82.         }
  83.     }
  84.     return result;
  85. }
  86.  
  87. SparseMatrix* SparseMatrix::multiplyPointWise(const SparseMatrix& b) {
  88.     if (nrow != b.nrow || ncol != b.ncol)
  89.         throw "MismatchedDimensions";
  90.  
  91.     SparseMatrix* result = new SparseMatrix(b);
  92.     // YOUR CODE HERE
  93.     for (int i = 0; i < size; i++)
  94.         for (int j = 0; j < b.size; j++)
  95.             if (storage[i].col == b.storage[j].col && storage[i].row == b.storage[j].row)
  96.                 result->setAt(storage[i].row, storage[i].col, storage[i].val*b.storage[j].val);
  97.  
  98.     return result;
  99. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top