Advertisement
dmkozyrev

class matrix

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