Advertisement
StoneHaos

simon4

Nov 6th, 2020
2,296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <math.h>
  4.  
  5. using namespace std;
  6.  
  7. class Matrix {
  8. public:
  9.     int size;
  10.     vector<vector<double>> m;
  11.     Matrix() {
  12.         this->size = 0;
  13.     }
  14.     Matrix(vector<vector<double>> m) {
  15.         int n = m.size();
  16.         for (int i = 0; i < n; ++ i) {
  17.             if (m[i].size() != n) {
  18.                 n = -1;
  19.                 break;
  20.             }
  21.         }
  22.         if (n == -1) {
  23.             cout << "Init Error\n";
  24.             this->size = 0;
  25.         }
  26.         else {
  27.             this->size = m.size();
  28.             this->m = m;
  29.         }
  30.     }
  31.  
  32.     void print() {
  33.         for (int i = 0; i < this->size; ++ i) {
  34.             for (int j = 0; j < this->size; ++ j)
  35.                 cout << this->m[i][j] << " ";
  36.             cout << endl;
  37.         }
  38.     }
  39.  
  40.     Matrix minor(int a, int b) {
  41.         if (this->size <= 1 || a >= this->size || b >= this->size) {
  42.             cout << "Minor Error\n";
  43.             return Matrix();
  44.         }
  45.         vector<vector<double>> m;
  46.         for (int i = 0; i < this->size; ++ i) {
  47.             if (i == a)
  48.                 continue;
  49.             m.push_back(vector<double>());
  50.             for (int j = 0; j < this->size; ++ j) {
  51.                 if (j == b)
  52.                     continue;
  53.                 m[m.size() - 1].push_back(this->m[i][j]);
  54.             }
  55.         }
  56.         return Matrix(m);
  57.     }
  58.  
  59.     double det() {
  60.         if (this->size < 2) {
  61.             cout << "Det Error\n";
  62.             return 0;
  63.         }
  64.         else if (this->size == 2) {
  65.             return this->m[0][0] * this->m[1][1] - this->m[0][1] * this->m[1][0];
  66.         }
  67.         else {
  68.             double s = 0;
  69.             for (int i = 0; i < this->size; ++ i)
  70.                 s += pow(-1, 1.0 * i) * this->m[0][i] * this->minor(0, i).det();
  71.             return s;
  72.         }
  73.     }
  74.  
  75.     ~Matrix() {}
  76.  
  77.     Matrix operator+(Matrix M) {
  78.         if (this->size != M.size) {
  79.             cout << "op+ Error\n";
  80.             return Matrix();
  81.         }
  82.         for (int i = 0; i < this->size; ++ i)
  83.             for (int j = 0; j < this->size; ++ j)
  84.                 this->m[i][j] += M.m[i][j];
  85.         return Matrix(this->m);
  86.     }
  87.  
  88.     Matrix operator-(Matrix M) {
  89.         if (this->size != M.size) {
  90.             cout << "op- Error\n";
  91.             return Matrix();
  92.         }
  93.         for (int i = 0; i < this->size; ++ i)
  94.             for (int j = 0; j < this->size; ++ j)
  95.                 this->m[i][j] -= M.m[i][j];
  96.         return Matrix(this->m);
  97.     }
  98.  
  99.     Matrix operator*(Matrix M) {
  100.         if (this->size != M.size) {
  101.             cout << "op* Error\n";
  102.             return Matrix();
  103.         }
  104.         vector<vector<double>> m;
  105.         for (int i = 0; i < this->size; ++ i) {
  106.             m.push_back(vector<double>());
  107.             for (int j = 0; j < this->size; ++ j) {
  108.                 double s = 0;
  109.                 for (int k = 0; k < this->size; ++ k)
  110.                     s += this->m[i][k] * M.m[k][j];
  111.                 m[i].push_back(s);
  112.             }
  113.         }
  114.         return Matrix(m);
  115.     }
  116.  
  117.     Matrix operator*=(double n) {
  118.         for (int i = 0; i < this->size; ++ i)
  119.             for (int j = 0; j < this->size; ++ j)
  120.                 this->m[i][j] *= n;
  121.         return Matrix(this->m);
  122.     }
  123. };
  124.  
  125. int main(void) {
  126.     int s = 3;
  127.     double x[3][3] = {
  128.         {5, 1, 7},
  129.         {-10, -2, 1},
  130.         {0, 1, 2}
  131.     }, y[3][3] = {
  132.         {2, 4, 1},
  133.         {3, 1, 0},
  134.         {7, 2, 1}
  135.     };
  136.     vector<vector<double>> v1, v2;
  137.     for (int i = 0; i < s; ++ i) {
  138.         v1.push_back(vector<double>(s));
  139.         v2.push_back(vector<double>(s));
  140.         for (int j = 0; j < s; ++ j) {
  141.             v1[i][j] = x[i][j];
  142.             v2[i][j] = y[i][j];
  143.         }
  144.     }
  145.  
  146.     Matrix a(v1), b(v2), res;
  147.    
  148.     res = ((a - b) *= 2) * ((a * a) + b);
  149.     res.print();
  150.  
  151.     return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement