Advertisement
dmkozyrev

classmatrix

Oct 28th, 2015
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.64 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.     bool prod(Matrix& b);
  24.     bool 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(){
  55.     Matrix temp = *this;
  56.     prod(temp, temp);
  57. }
  58.  
  59. void Matrix::power(i64 n){
  60.     Matrix t = *this; t.toId();
  61.     bool notFinished = true;
  62.     while ( notFinished ) {
  63.         if ( n % 2 != 0 ) t.prod(*this);
  64.         n /= 2;
  65.         if ( n > 0 )
  66.             sqr();
  67.         else
  68.             notFinished = false;
  69.     }
  70.     *this = t;
  71. }
  72.  
  73. Matrix::Matrix(){
  74.     vector<i64> temp (1,0);
  75.     matrix.clear();
  76.     matrix.push_back(temp);
  77.     _base = 0;
  78. }
  79.  
  80. i64 Matrix::getVal(i64 y, i64 x){
  81.     i64 countR = getCountRow(), countC = getCountCol();
  82.     if ( y > countR ) y %= countR;
  83.     if ( x > countC ) x %= countC;
  84.     return matrix[y][x];
  85. }
  86.  
  87. Matrix::Matrix(Matrix& m){
  88.     matrix = m.matrix;
  89.     _base = m._base;
  90. }
  91.  
  92. Matrix& Matrix::operator=(Matrix& m){
  93.     matrix = m.matrix;
  94.     _base = m._base;
  95.     return *this;
  96. }
  97.  
  98. bool Matrix::prod(Matrix& b){
  99.     Matrix temp = *this;
  100.     return prod(temp, b);
  101. }
  102.  
  103. bool Matrix::prod(Matrix& a, Matrix& b){
  104.     i64 countRowsFromA = a.getCountRow(), countColsFromA = a.getCountCol();
  105.     i64 countRowsFromB = b.getCountRow(), countColsFromB = b.getCountCol();
  106.    
  107.     bool flag = countColsFromA == countRowsFromB;
  108.     if ( !flag ) return flag;
  109.     matrix.clear();
  110.     _base = a._base;
  111.    
  112.     for ( i64 i = 0; i < countRowsFromA; ++i ){
  113.         vector<i64> temp;
  114.         for ( i64 j = 0; j < countColsFromB; ++j ){
  115.             i64 temp_val = 0;
  116.             for (i64 k = 0; k < countColsFromA; ++k){
  117.                
  118.                 temp_val += a.getVal(i, k) * b.getVal(k, j);
  119.                
  120.                 if ( _base != 0 && ( temp_val >= _base || temp_val < 0 ) ) temp_val %= _base;
  121.             }
  122.             temp.push_back(temp_val);
  123.         }
  124.         matrix.push_back(temp);
  125.     }
  126.     return flag;
  127. }
  128.  
  129. i64 Matrix::getCountRow(){return matrix.size();}
  130. i64 Matrix::getCountCol(){return matrix.at(0).size();}
  131. i64 Matrix::getBase(){return _base;}
  132.  
  133. bool Matrix::setBase(i64 base){
  134.     bool flag = _base > base;
  135.     _base = base;
  136.     if (flag)
  137.         for (auto &i : matrix) for (auto &j : i) j %= _base;
  138.     return flag;
  139. }
  140.  
  141. Matrix::Matrix(i64 M, i64 N, i64 val, i64 base){
  142.     _base = base;
  143.     if ( _base != 0 ) val %= _base;
  144.     matrix.clear();
  145.     for (int i=0; i<M; ++i){
  146.         vector<i64> temp;
  147.         for (int j = 0; j<N; ++j)
  148.             temp.push_back(val);
  149.         matrix.push_back(temp);
  150.     }
  151. }
  152.  
  153. void Matrix::print(){
  154.     for (auto &i : matrix){
  155.         for (auto &j : i)
  156.             cout<<j<<" ";
  157.         cout<<endl;
  158.     }
  159. }
  160.    
  161. bool Matrix::setVal(i64 y, i64 x, i64 val){
  162.     bool flag = y < matrix.size() && x < matrix.at(0).size();
  163.     if ( !flag ) return flag;
  164.     if ( _base != 0 ) {
  165.         val %= _base;
  166.     }
  167.        
  168.     matrix[y][x] = val;
  169.     return flag;
  170. }
  171.  
  172. int main(){
  173.     vector<i64> v = {0,2,1,-1};
  174.     Matrix m(v, 5);
  175.     Matrix n = m, k = m;
  176.     for (int i = 0; i <=100; ++i){
  177.         cout<<"step "<<i<<":"<<endl;
  178.         m = n;
  179.         m.power(i);
  180.         m.print();
  181.     }
  182.     cout<<m.getBase()<<endl;
  183.    
  184.     return 0;
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement