in_chainz

Untitled

Feb 21st, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. template<typename T>
  5. class Matrix {
  6. private:
  7.     std::vector<std::vector<T> >
  8.             mat_;
  9.  
  10. public:
  11.     explicit Matrix(const std::vector<std::vector<T> > &mat) : mat_(mat) {}
  12.  
  13.     Matrix(size_t n, size_t m) {
  14.         mat_.resize(n);
  15.         mat_.resize(m);
  16.     }
  17.  
  18.     const std::pair<size_t, size_t> size() const {
  19.         if (mat_.empty())
  20.             return {0, 0};
  21.         return {mat_.size(), mat_[0].size()};
  22.     }
  23.  
  24.     const std::vector<T> &operator[](size_t i) const {
  25.         return mat_[i];
  26.     }
  27.  
  28.     std::vector<T> &operator[](size_t i) {
  29.         return mat_[i];
  30.     }
  31.  
  32.     Matrix &operator+=(const Matrix &other) {
  33.         auto[n, m] = this->size();
  34.         for (size_t i = 0; i < n; ++i) {
  35.             for (size_t j = 0; j < m; ++j)
  36.                 (*this)[i][j] += other[i][j];
  37.         }
  38.         return *this;
  39.     }
  40.  
  41.     Matrix &operator*=(int alpha) {
  42.         auto[n, m] = this->size();
  43.         for (size_t i = 0; i < n; ++i) {
  44.             for (size_t j = 0; j < m; ++j)
  45.                 (*this)[i][j] *= alpha;
  46.         }
  47.         return *this;
  48.     }
  49.  
  50.     const Matrix& transposed() const {
  51.         auto[n, m] = this->size();
  52.         std::vector<std::vector<T> > result(m, std::vector<T>(n));
  53.         for (size_t i = 0; i < n; ++i) {
  54.             for (size_t j = 0; j < m; ++j) {
  55.                 result[j][i] = (*this)[i][j];
  56.             }
  57.         }
  58.         Matrix rs(result);
  59.         return rs;
  60.     }
  61.  
  62.     void transpose() {
  63.         *this = Matrix(transposed());
  64.     }
  65. };
  66.  
  67. template<typename T>
  68. Matrix<T> operator+(const Matrix<T> &one, const Matrix<T> &two) {
  69.     Matrix tmp = one;
  70.     return tmp += two;
  71. }
  72.  
  73. template<typename T>
  74. Matrix<T> operator*(const Matrix<T> &one, int alpha) {
  75.     Matrix tmp = one;
  76.     return tmp *= alpha;
  77. }
  78.  
  79. template<typename T>
  80. std::ostream &operator<<(std::ostream &out, const Matrix<T> &m) {
  81.     auto[n, k] = m.size();
  82.     for (size_t i = 0; i < n; ++i) {
  83.         for (size_t j = 0; j < k; ++j) {
  84.             out << m[i][j];
  85.             if (j + 1 < k)
  86.                 out << '\t';
  87.         }
  88.         if (i + 1 < n)
  89.             out << '\n';
  90.     }
  91.     return out;
  92. }
  93.  
  94.  
  95. int main() {
  96.     std::vector<std::vector<int> > a(5), b(5);
  97.     for (int i = 0; i < 5; ++i) {
  98.         for (int j = 0; j < 5; ++j) {
  99.             a[i].push_back(i + j + 2);
  100.             b[i].push_back(4);
  101.         }
  102.     }
  103.     Matrix m(a);
  104.  
  105.     Matrix t(b);
  106.     auto dim = m.size();
  107.     // std::cout << m << std::endl;
  108.     // std::cout << dim.first << ' ' << dim.second << std::endl;
  109.     Matrix rs = m.transposed();
  110.     std::cout << rs << std::endl;
  111.     //std::cout << t << std::endl;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment