Advertisement
Infernale

NCTU LAB 24/10 NUM 3 [Matrix.cpp]

Oct 24th, 2019
494
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #include "Matrix.h"
  2.  
  3. Matrix::Matrix(){
  4.   //Do Nothing;
  5. }
  6.  
  7. Matrix::Matrix(int row, int column, vector <double> coefficient){
  8.   this->row = row;
  9.   this->column = column;
  10.   this->coefficient = coefficient;
  11. }
  12.  
  13. void Matrix::setMatrix(int row, int column, vector<double> coefficient){
  14.   this->row = row;
  15.   this->column = column;
  16.   this->coefficient = coefficient;
  17. }
  18.  
  19. vector<double> Matrix::getCoefficient() const {
  20.   return this->coefficient;
  21. }
  22.  
  23. int Matrix::getRow() const {
  24.   return this->row;
  25. }
  26.  
  27. int Matrix::getColumn() const {
  28.   return this->column;
  29. }
  30.  
  31. void Matrix::operator()(){
  32.   auto it = this->coefficient.begin();
  33.   for(int i=0; i<this->row; i++){
  34.     for(int j=0; j<this->column; j++){
  35.       cout << fixed << setprecision(2) << setw(8) << *it++;
  36.     }
  37.     cout << endl;
  38.   }
  39. }
  40.  
  41. bool Matrix::isAddable(const Matrix& matrix){
  42.   if(this->row == matrix.getRow() && this->column == matrix.getColumn())
  43.     return true;
  44.   return false;
  45. }
  46.  
  47. bool Matrix::isSubstractable(const Matrix& matrix){
  48.   if(this->row == matrix.getRow() && this->column == matrix.getColumn())
  49.     return true;
  50.   return false;
  51. }
  52.  
  53. bool Matrix::isMultipliable(const Matrix& matrix){
  54.   if(this->column == matrix.getRow())
  55.     return true;
  56.   return false;
  57. }
  58.  
  59. Matrix Matrix::operator+(const Matrix& matrix){
  60.   Matrix res;
  61.   vector <double> result;
  62.   //transform(this->coefficient.begin(), this->coefficient.end(), matrix.coefficient.begin(), result.begin(), ::plus<int>());
  63.   auto it1 = this->coefficient.begin();
  64.   auto it2 = matrix.coefficient.begin();
  65.   for( ;it1 != this->coefficient.end(); it1++, it2++){
  66.     double num = *it1 + *it2;
  67.     result.push_back(num);
  68.   }
  69.   res.setMatrix(this->row, this->column, result);
  70.   return res;
  71. }
  72.  
  73. Matrix Matrix::operator-(const Matrix& matrix){
  74.   Matrix res;
  75.   vector <double> result;
  76.   auto it1 = this->coefficient.begin();
  77.   auto it2 = matrix.coefficient.begin();
  78.   for( ;it1 != this->coefficient.end(); it1++, it2++){
  79.     double num = *it1 - *it2;
  80.     result.push_back(num);
  81.   }
  82.   res.setMatrix(this->row, this->column, result);
  83.   return res;
  84. }
  85.  
  86. Matrix Matrix::operator*(const Matrix& matrix){
  87.   Matrix res;
  88.   vector <double> result;
  89.   auto it1 = this->coefficient.begin();
  90.   auto it2 = matrix.coefficient.begin();
  91.   for(int i=0; i<this->row; i++){
  92.     for(int j=0; j<matrix.column; j++){
  93.       double sum = 0;
  94.       for(int k=0; k<this->column; k++){
  95.         sum += *(it1 + i*this->column+k) * *(it2 + k*matrix.column+j);
  96.       }
  97.       result.push_back(sum);
  98.     }
  99.   }
  100.   res.setMatrix(this->row, matrix.column, result);
  101.   return res;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement