Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | None | 0 0
  1. struct matrix{
  2.     int n, m;
  3.     vector<vector<int64_t>> M;
  4.     matrix(int nn, int mm){
  5.         n = nn;
  6.         m = mm;
  7.         M.resize(n);
  8.         for(int i = 0; i < n; ++i)
  9.             M[i].resize(m);
  10.         }
  11.     matrix(vector<vector<int64_t>> &v){
  12.         M = v;
  13.         n = v.size();
  14.         if(n) m = v[0].size();
  15.         }
  16.     void print(){
  17.         cout << "\n";
  18.         for(int i = 0; i < n; ++i,cout << "\n")
  19.             for(int j = 0; j < m; ++j)
  20.                 cout << M[i][j] << " ";
  21.         }
  22.     vector<int64_t>& operator[](int i){
  23.         return M[i];
  24.         }
  25. };
  26.  
  27. matrix transp(matrix &x){
  28.     matrix r(x.m,x.n);
  29.     for(int i = 0; i < x.n; ++i)
  30.         for(int j = 0; j < x.m; ++j)
  31.             r[j][i] = x[i][j];
  32.     return r;
  33. }
  34.  
  35. matrix rtransp(matrix &x){
  36.     matrix r(x.m,x.n);
  37.     for(int i = 0; i < x.n; ++i)
  38.         for(int j = 0; j < x.m; ++j)
  39.             r[x.m-j-1][x.n-i-1] = x[i][j];
  40.     return r;
  41.     }
  42.  
  43. matrix operator +(matrix &a, matrix &b){
  44.     matrix r(a.n,a.m);
  45.     for(int i = 0; i < a.n; ++i)
  46.         for(int j = 0; j < a.m; ++j)
  47.             r[i][j] = a[i][j]+b[i][j];
  48.     return r;
  49. }
  50.  
  51. matrix& operator +=(matrix &a, matrix &b){
  52.     for(int i = 0; i < a.n; ++i)
  53.         for(int j = 0; j < a.m; ++j)
  54.             a[i][j]+=b[i][j];
  55.     return a;
  56.     }
  57. matrix operator *(matrix &a, matrix &b){
  58.     matrix r(a.n,b.m);
  59.     for(int i = 0; i < a.n; ++i)
  60.         for(int j = 0; j < b.m; ++j)
  61.             for(int k = 0; k < a.m; ++k){
  62.                 r[i][j] += a[i][k]*b[k][j];
  63.         }
  64.     return r;
  65.     }
  66.  
  67. matrix& operator *=(matrix &a, matrix &b){
  68.     a = a*b;
  69.     return a;
  70.     }
  71. matrix edmtx(matrix &a){
  72.     matrix res(a.m,a.m);
  73.     for(int i = 0; i < a.m; ++i)
  74.         res[i][i] = 1;
  75.     return res;
  76.     }
  77. matrix operator ^(matrix &a, int64_t b){
  78.     matrix res = edmtx(a);
  79.     for(;b;b>>=1){
  80.         if(b&1) res*=a;
  81.         a*=a;
  82.     }
  83.     return res;
  84.     }
  85. matrix& operator ^=(matrix &a, int64_t b){
  86.     a = a^b;
  87.     return a;
  88. }
  89. ostream& operator <<(ostream& out, matrix& m){
  90.     for(int i = 0; i < m.n; ++i, out << "\n")
  91.         for(int j = 0; j < m.m; ++j)
  92.             out << m[i][j] << " ";
  93.     return out;
  94.     }
  95. ostream& operator <<(ostream& out, matrix m){
  96.     for(int i = 0; i < m.n; ++i, out << "\n")
  97.         for(int j = 0; j < m.m; ++j)
  98.             out << m[i][j] << " ";
  99.     return out;
  100.     }
  101. istream& operator >>(istream& in, matrix& m){
  102.     for(int i = 0; i < m.n; ++i)
  103.         for(int j = 0; j < m.m; ++j)
  104.             in >> m[i][j];
  105.     return in;
  106.     }
  107. bool operator <(matrix& a, matrix& b){
  108.     return a.M<b.M;
  109. }
  110. bool operator >(matrix& a, matrix& b){
  111.     return a.M>b.M;
  112. }
  113. bool operator ==(matrix& a, matrix& b){
  114.     return a.M==b.M;}
  115.  
  116. bool operator !=(matrix& a, matrix& b){
  117.     return a.M!=b.M;
  118.     }
  119. bool operator >=(matrix& a, matrix& b){
  120.     return a.M>=b.M;
  121.     }
  122. bool operator <=(matrix& a, matrix& b){
  123.     return a.M<=b.M;
  124.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement