Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <ostream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <cmath>
  6.  
  7. class Matrix: public std::vector<std::vector<double>> {
  8. public:
  9.   // конструкторы
  10.   Matrix () = default;
  11.   Matrix (int n, int m);
  12.  
  13.   // полуконструкторы
  14.   static Matrix eye(int n, double value = 1.0) {
  15.     Matrix t(n, n);
  16.  
  17.     for(int i = 0; i < n; ++i)
  18.       t[i][i] = value;
  19.  
  20.     return t;
  21.   }
  22.  
  23.   // размеры
  24.   auto sizes() const;
  25.  
  26.   // операторы
  27.   Matrix operator* (const Matrix& other) const;
  28.   Matrix operator+ (const Matrix& other) const;
  29.   Matrix operator- (const Matrix& other) const;
  30.  
  31.   // операции
  32.   void add_row_to_row(int n, int m, double factor = 1.0);
  33.   void swap_rows(int n, int m);
  34.   void multiply_row(int n, double factor);
  35.  
  36.   int find_pivot(int n, double EPS = 1e-07);
  37.   void set_zeros(double EPS = 1e-07);
  38.  
  39.   // алгебра
  40.   Matrix inverse() const;
  41.   double square_norm() const;
  42.  
  43.   // input and output
  44.   friend std::ostream& operator<< (std::ostream& os, const Matrix& m);
  45.   friend std::istream& operator>> (std::istream& is, Matrix& m);
  46. };
  47.  
  48. // конструкторы
  49. Matrix::Matrix(int n, int m):
  50. std::vector<std::vector<double>> (n, std::vector<double> (m, 0)) {}
  51.  
  52. // размеры
  53. auto Matrix::sizes() const {
  54.   std::pair<int, int> result;
  55.  
  56.   result.first = static_cast<int>(this->size());
  57.   result.second = static_cast<int>((*this)[0].size());
  58.  
  59.   return result;
  60. }
  61.  
  62. // операторы
  63. Matrix Matrix::operator* (const Matrix& other) const {
  64.   // по столбцам a_1_1 ... a_1_n
  65.   // по строкам a_1_1 .. a_m_1
  66.   int m = (this->sizes()).first;
  67.   int n = (this->sizes()).second;
  68.  
  69.   int other_m = other.sizes().first;
  70.   int other_n = other.sizes().second;
  71.  
  72.   Matrix t (m, other_n);
  73.  
  74.   for(int i = 0; i < m; ++i){
  75.     for(int j = 0; j < other_n; ++j){
  76.       for(int k = 0; k < n; ++k)
  77.         t[i][j] += (*this)[i][k] * other[k][j];
  78.     }
  79.   }
  80.  
  81.   return t;
  82. }
  83.  
  84. Matrix Matrix::operator+ (const Matrix& other) const {
  85.   if(this->sizes() != other.sizes())
  86.     return Matrix::eye(1);
  87.  
  88.   Matrix t = *this;
  89.  
  90.   for(int i = 0; i < t.sizes().first; ++i){
  91.     for(int j = 0; j < t.sizes().second; ++j){
  92.       t[i][j] += other[i][j];
  93.     }
  94.   }
  95.  
  96.   return t;
  97. }
  98.  
  99. Matrix Matrix::operator- (const Matrix& other) const {
  100.   if(this->sizes() != other.sizes())
  101.     return Matrix::eye(1);
  102.  
  103.   Matrix t = *this;
  104.  
  105.   for(int i = 0; i < t.sizes().first; ++i){
  106.     for(int j = 0; j < t.sizes().second; ++j){
  107.       t[i][j] -= other[i][j];
  108.     }
  109.   }
  110.  
  111.   return t;
  112. }
  113.  
  114. // операции
  115. void Matrix::add_row_to_row(int n, int m, double factor){
  116.   for(int j = 0; j < (*this)[0].size(); ++j)
  117.     (*this)[n][j] += factor * (*this)[m][j];
  118. }
  119.  
  120. void Matrix::swap_rows(int n, int m){
  121.   std::swap((*this)[n], (*this)[m]);
  122. }
  123.  
  124. void Matrix::multiply_row(int n, double factor){
  125.   for(int j = 0; j < (*this)[0].size(); ++j)
  126.     (*this)[n][j] *= factor;
  127. }
  128.  
  129. int Matrix::find_pivot(int n, double EPS){
  130.   for(int i = n; i < this->size(); ++i)
  131.     if(std::abs((*this)[i][n]) > EPS)
  132.       return i;
  133.  
  134.   return -1;
  135. }
  136.  
  137. void Matrix::set_zeros(double EPS){
  138.   for(auto& i: *this)
  139.     for(auto& j: i)
  140.       if(std::abs(j) < EPS)
  141.         j = 0;
  142. }
  143.  
  144. // алгебра
  145. Matrix Matrix::inverse() const {
  146.   Matrix t = (*this);
  147.   Matrix inversed = Matrix::eye(t.size());
  148.  
  149.   for(int i = 0; i < t.size(); ++i){
  150.     int pivot = t.find_pivot(i);
  151.  
  152.     t.swap_rows(i, pivot);
  153.     inversed.swap_rows(i, pivot);
  154.  
  155.     double factor = t[i][i];
  156.     t.multiply_row(i, 1.0 / factor);
  157.     inversed.multiply_row(i, 1.0 / factor);
  158.  
  159.     for(int j = 0; j < t.size(); ++j){
  160.       if(j != i){
  161.         factor = t[j][i];
  162.  
  163.         t.add_row_to_row(j, i, -factor);
  164.         inversed.add_row_to_row(j, i, -factor);
  165.       }
  166.     }
  167.   }
  168.  
  169.   return inversed;
  170. }
  171.  
  172. double Matrix::square_norm() const {
  173.   double norm = 0;
  174.  
  175.   for(auto&& i: *this){
  176.     for(auto&& j: i){
  177.       norm += j * j;
  178.     }
  179.   }
  180.  
  181.   return norm;
  182. };
  183.  
  184. // input and output
  185. std::ostream& operator<< (std::ostream& os, const Matrix& m) {
  186.   for(auto&& i: m){
  187.     for(auto&& j: i){
  188.       os << j << ' ';
  189.     }
  190.  
  191.     os << '\n';
  192.   }
  193.  
  194.   return os;
  195. }
  196.  
  197. std::istream& operator>> (std::istream& is, Matrix& m) {
  198.   for(auto& i: m)
  199.     for(auto& j: i)
  200.       is >> j;
  201.  
  202.   return is;
  203. }
  204.  
  205. Matrix generate_matrix(int n){
  206.     if(n < 3)
  207.         return Matrix::eye(n);
  208.  
  209.     Matrix t(n, n);
  210.  
  211.     t[0][0] = -1.0;
  212.     t[0][1] = 1.0;
  213.  
  214.     for(int i = 1; i < n - 1; ++i){
  215.         t[i][i - 1] = 1.0;
  216.         t[i][i] = -2.0;
  217.         t[i][i + 1] = 1.0;
  218.     }
  219.  
  220.     t[n - 1][n - 2] = 1.0;
  221.     t[n - 1][n - 1] = -((n - 1) * 1.0 / n);
  222.  
  223.     return t;
  224. }
  225.  
  226. int main(){
  227.   return 0;
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement