Advertisement
dmkozyrev

classmatrix

Oct 28th, 2015
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. using namespace std;
  5. typedef long long i64;
  6.  
  7. class Matrix{
  8. private:
  9.     vector<vector<i64>> matrix;
  10.     i64 _base;
  11. public:
  12.     Matrix();
  13.     Matrix(vector<i64>& v, i64 base = 0);
  14.     Matrix(i64 M, i64 N, i64 val = 0, i64 base = 0);
  15.     Matrix(Matrix& m);
  16.     void print();
  17.     i64 getCountRow();
  18.     i64 getCountCol();
  19.     i64 getBase();
  20.     bool setVal(i64 y, i64 x, i64 val);
  21.     i64 getVal(i64 y, i64 x);
  22.     bool setBase(i64 base);
  23.     void prod(Matrix& b);
  24.     void prod(Matrix& a , Matrix& b);
  25.     void power(i64 n);
  26.     void sqr();
  27.     void toId();   
  28.     Matrix& operator=(Matrix& m);
  29. };
  30.  
  31. Matrix::Matrix(vector<i64>& v, i64 base){
  32.     matrix.clear();
  33.     _base = base;
  34.     i64 n = round(sqrt(v.size()));
  35.     for (i64 i = 0, k = 0; i < n; ++i){
  36.         vector<i64> temp;
  37.         for (i64 j = 0; j < n; ++j){
  38.             i64 val = v[k++];
  39.             if ( _base != 0 ) val %= _base;
  40.             temp.push_back(val);
  41.             if ( k >= v.size()) k %= v.size();
  42.         }
  43.         matrix.push_back(temp);
  44.     }
  45. }
  46.  
  47. void Matrix::toId(){
  48.     i64 countR = getCountRow(), countC = getCountCol();
  49.     for (int i = 0; i < countR; ++i)
  50.         for (int j = 0; j < countC; ++j)
  51.             setVal(i, j, i == j ? 1:0);
  52. }
  53.  
  54. void Matrix::sqr(){prod(*this);}
  55.  
  56. void Matrix::power(i64 n){
  57.     Matrix t = *this; t.toId();
  58.     bool notFinished = true;
  59.     while ( notFinished ) {
  60.         if ( n % 2 != 0 ) t.prod(*this);
  61.         n /= 2;
  62.         if ( n > 0 )
  63.             sqr();
  64.         else
  65.             notFinished = false;
  66.     }
  67.     *this = t;
  68. }
  69.  
  70. Matrix::Matrix(){
  71.     vector<i64> temp (1,0);
  72.     matrix.clear();
  73.     matrix.push_back(temp);
  74.     _base = 0;
  75. }
  76.  
  77. i64 Matrix::getVal(i64 y, i64 x){
  78.     i64 countR = getCountRow(), countC = getCountCol();
  79.     if ( y > countR ) y %= countR;
  80.     if ( x > countC ) x %= countC;
  81.     return matrix[y][x];
  82. }
  83.  
  84. Matrix::Matrix(Matrix& m){
  85.     matrix = m.matrix;
  86.     _base = m._base;
  87. }
  88.  
  89. Matrix& Matrix::operator=(Matrix& m){
  90.     matrix = m.matrix;
  91.     _base = m._base;
  92.     return *this;
  93. }
  94.  
  95. void Matrix::prod(Matrix& b){
  96.     i64 countR = getCountRow(), countC = b.getCountCol(), d = getCountCol(), base = getBase();
  97.     Matrix temp(countR, countC, 0, base);
  98.     for ( i64 i = 0; i < countR; ++i )
  99.         for ( i64 j = 0; j < countC; ++j ){
  100.             i64 temp_val = 0;
  101.             for (i64 k = 0; k < d; ++k){
  102.                 temp_val += getVal(i, k) * b.getVal(k, j);
  103.                 if ( base != 0 && temp_val >= base ) temp_val %= base;
  104.             }
  105.             temp.setVal(i, j, temp_val);
  106.         }
  107.     *this = temp;
  108. }
  109.  
  110. void Matrix::prod(Matrix& a, Matrix& b){
  111.     a.prod(b);
  112.     matrix = a.matrix;
  113.     _base = a._base;
  114. }
  115.  
  116. i64 Matrix::getCountRow(){return matrix.size();}
  117. i64 Matrix::getCountCol(){return matrix.at(0).size();}
  118. i64 Matrix::getBase(){return _base;}
  119.  
  120. bool Matrix::setBase(i64 base){
  121.     bool flag = _base > base;
  122.     _base = base;
  123.     if (flag)
  124.         for (auto &i : matrix) for (auto &j : i) j %= _base;
  125.     return flag;
  126. }
  127.  
  128. Matrix::Matrix(i64 M, i64 N, i64 val, i64 base){
  129.     _base = base;
  130.     if ( _base != 0 ) val %= _base;
  131.     matrix.clear();
  132.     for (int i=0; i<M; ++i){
  133.         vector<i64> temp;
  134.         for (int j = 0; j<N; ++j)
  135.             temp.push_back(val);
  136.         matrix.push_back(temp);
  137.     }
  138. }
  139.  
  140. void Matrix::print(){
  141.     for (auto &i : matrix){
  142.         for (auto &j : i)
  143.             cout<<j<<" ";
  144.         cout<<endl;
  145.     }
  146. }
  147.    
  148. bool Matrix::setVal(i64 y, i64 x, i64 val){
  149.     bool flag = y < matrix.size() && x < matrix.at(0).size();
  150.     if ( !flag ) return flag;
  151.     if ( _base != 0 ) {
  152.         val %= _base;
  153.     }
  154.        
  155.     matrix[y][x] = val;
  156.     return flag;
  157. }
  158.  
  159. int main(){
  160.     vector<i64> v = {0,2,1,-1};
  161.     Matrix m(v, 5);
  162.     Matrix n = m, k = m;
  163.     for(int i = 1; i <= 100; ++i){
  164.         cout << "matrix n - step " << i << ": "<<endl;
  165.         n.print();
  166.         n.prod(m);
  167.         k = m;
  168.         k.power(i);
  169.         cout << "matrix k - step " << i << ": "<<endl;
  170.         k.print();
  171.         for (int j = 0; j < 20; j++)cout<<"~";
  172.         cout<<endl;
  173.     }
  174.    
  175.     return 0;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement