Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. template<typename T>
  5. class Matrix {
  6.     std::vector<std::vector<T>> a;
  7.  
  8. public:
  9.     Matrix(const std::vector<std::vector<T>> &b) : a(b) {
  10.     }
  11.  
  12.     Matrix(int N, int M) {
  13.         a.resize(N);
  14.         for (int i = 0; i < N; ++i) a[i].resize(M);
  15.     }
  16.  
  17.     std::pair<size_t, size_t> size() const {
  18.         std::pair<size_t, size_t> ans = {a.size(), 0};
  19.         if (a.size())
  20.             ans.second = a[0].size();
  21.         return ans;
  22.     }
  23.  
  24.     std::vector<T> &operator[](int i) {
  25.         return a[i];
  26.     }
  27.  
  28.     const std::vector<T> &operator[](int i) const {
  29.         return a[i];
  30.     }
  31.  
  32.     Matrix<T> &operator+=(const Matrix<T> &other) {
  33.         for (size_t i = 0; i < a.size(); ++i) {
  34.             for (size_t j = 0; j < a[0].size(); ++j) {
  35.                 a[i][j] += other[i][j];
  36.             }
  37.         }
  38.         return *this;
  39.     }
  40.  
  41.     Matrix<T> operator+(const Matrix<T> &other) const {
  42.         Matrix<T> ans(*this);
  43.         ans += other;
  44.         return ans;
  45.     }
  46.  
  47.     template<typename TT>
  48.     Matrix<T> &operator*=(const TT &x) {
  49.         for (size_t i = 0; i < a.size(); ++i) {
  50.             for (size_t j = 0; j < a[0].size(); ++j) {
  51.                 a[i][j] *= x;
  52.             }
  53.         }
  54.         return *this;
  55.     }
  56.  
  57.     Matrix<T> &operator*=(const Matrix<T> &other) {
  58. //        if (a[0].size() != other.size().first) std::cerr << "kek\n";
  59.         std::vector<std::vector<T>> ans(a.size(), std::vector<T>(other.size().second));
  60.         for (size_t i = 0; i < a.size(); ++i) {
  61.             for (size_t k = 0; k < a[0].size(); ++k) {
  62.                 for (size_t j = 0; j < other.size().second; ++j) {
  63.                     ans[i][j] += a[i][k] * other[k][j];
  64.                 }
  65.             }
  66.         }
  67.         swap(a, ans);
  68.         return *this;
  69.     }
  70.  
  71.     Matrix<T> operator*(const Matrix<T> &other) const {
  72.         auto ans(*this);
  73.         ans *= other;
  74.         return ans;
  75.     }
  76.  
  77.     template<typename TT>
  78.     Matrix<T> operator*(const TT &x) const {
  79.         Matrix<T> ans(*this);
  80.         ans *= x;
  81.         return ans;
  82.     }
  83.  
  84.     Matrix<T> &transpose() {
  85.         std::vector<std::vector<T>> ans(a[0].size(), std::vector<T>(a.size()));
  86.         for (size_t i = 0; i < a.size(); ++i) {
  87.             for (size_t j = 0; j < a[0].size(); ++j) {
  88.                 ans[j][i] = a[i][j];
  89.             }
  90.         }
  91.         std::swap(a, ans);
  92.         return *this;
  93.     }
  94.  
  95.     Matrix<T> transposed() const {
  96.         Matrix<T> ans(*this);
  97.         ans.transpose();
  98.         return ans;
  99.     }
  100.  
  101.     template<typename U>
  102.     std::vector<U> solve(const std::vector<U> &B) {
  103.         const U EPS = 1e-9;
  104.         std::vector<std::vector<U>> A(a.size());
  105.         for (size_t i = 0; i < a.size(); i++) {
  106.             A[i].resize(a[i].size());
  107.             for (size_t j = 0; j < a[i].size(); j++) {
  108.                 A[i][j] = a[i][j];
  109.             }
  110.         }
  111.         int n = A.size();
  112.         for (int i = 0; i < n; i++) {
  113.             A[i].push_back(B[i]);
  114.         }
  115.         int m = static_cast<int>(A[0].size()) - 1;
  116.         std::vector<int> where(m, -1);
  117.         std::vector<std::pair<int, int>> sw;
  118.         for (int col = 0, row = 0; col < m && row < n; ++col) {
  119.             int sel = row;
  120.             for (int i = row; i < n; ++i)
  121.                 if (abs(A[i][col]) > abs(A[sel][col]))
  122.                     sel = i;
  123.             if (abs(A[sel][col]) < EPS)
  124.                 continue;
  125.             for (int i = col; i <= m; ++i)
  126.                 std::swap(A[sel][i], A[row][i]);
  127.             sw.push_back({sel, row});
  128.             where[col] = row;
  129.             for (int i = 0; i < n; ++i)
  130.                 if (i != row) {
  131.                     U c = A[i][col] / A[row][col];
  132.                     for (int j = col; j <= m; ++j)
  133.                         A[i][j] -= A[row][j] * c;
  134.                 }
  135.             ++row;
  136.         }
  137.         std::vector<U> ans;
  138.         ans.assign(m, 0);
  139.         for (int i = 0; i < m; ++i)
  140.             if (where[i] != -1) {
  141.                 U x = A[where[i]][m] / A[where[i]][i];
  142.                 ans[i] = U(x);
  143.             }
  144.         /*for (auto it : sw) {
  145.             std::swap(ans[it.first], ans[it.second]);
  146.         }*/
  147.         return ans;
  148.     }
  149.  
  150.     class iterator {
  151.     public:
  152.         typedef iterator self_type;
  153.         typedef T value_type;
  154.         typedef T &reference;
  155.         typedef T *pointer;
  156.         typedef std::forward_iterator_tag iterator_category;
  157.         typedef int difference_type;
  158.  
  159.         iterator(size_t x, size_t y, size_t n, size_t m, std::vector<std::vector<T>> *tmp)
  160.                 : x(x), y(y), n(n), m(m), a(tmp) {}
  161.  
  162.         self_type &operator++() {
  163.             ++y;
  164.             if (y == m) {
  165.                 x++;
  166.                 y = 0;
  167.             }
  168.             return *this;
  169.         }
  170.  
  171.         reference operator*() { return (*a)[x][y]; }
  172.  
  173.         bool operator==(const self_type &rhs) { return x == rhs.x && y == rhs.y && a == rhs.a; }
  174.  
  175.         bool operator!=(const self_type &rhs) { return !((*this) == rhs); }
  176.  
  177.     private:
  178.         size_t x, y, n, m;
  179.         std::vector<std::vector<T>> *a;
  180.     };
  181.  
  182.     class const_iterator {
  183.     public:
  184.         typedef const_iterator self_type;
  185.         typedef T value_type;
  186.         typedef const T &reference;
  187.         typedef T *pointer;
  188.         typedef std::forward_iterator_tag iterator_category;
  189.         typedef int difference_type;
  190.  
  191.         const_iterator(int x, int y, size_t n, size_t m, const std::vector<std::vector<T>> *tmp)
  192.                 : x(x), y(y), n(n), m(m), a(tmp) {}
  193.  
  194.         self_type &operator++() {
  195.             ++y;
  196.             if (y == m) {
  197.                 x++;
  198.                 y = 0;
  199.             }
  200.             return *this;
  201.         }
  202.  
  203.         const reference operator*() { return (*a)[x][y]; }
  204.  
  205.         bool operator==(const self_type &rhs) { return x == rhs.x && y == rhs.y && a == rhs.a; }
  206.  
  207.         bool operator!=(const self_type &rhs) { return !((*this) == rhs); }
  208.  
  209.     private:
  210.         size_t x, y, n, m;
  211.         const std::vector<std::vector<T>> *a;
  212.     };
  213.  
  214.     iterator begin() {
  215.         return iterator(0, 0, static_cast<int>(a.size()), static_cast<int>(a[0].size()), &a);
  216.     }
  217.  
  218.     iterator end() {
  219.         return iterator(a.size(), 0, static_cast<int>(a.size()), static_cast<int>(a[0].size()), &a);
  220.     }
  221.  
  222.     const_iterator begin() const {
  223.         return const_iterator(0, 0, static_cast<int>(a.size()), static_cast<int>(a[0].size()), &a);
  224.     }
  225.  
  226.     const_iterator end() const {
  227.         return const_iterator(a.size(), 0,
  228.                               static_cast<int>(a.size()), static_cast<int>(a[0].size()), &a);
  229.     }
  230. };
  231.  
  232. template<typename T>
  233. std::ostream &operator<<(std::ostream &out, const Matrix<T> &m) {
  234.     auto mSize = m.size();
  235.     for (size_t i = 0; i < mSize.first; ++i) {
  236.         if (i != 0)
  237.             out << '\n';
  238.         for (size_t j = 0; j < mSize.second; ++j) {
  239.             if (j)
  240.                 out << '\t';
  241.             out << m[i][j];
  242.         }
  243.     }
  244.     return out;
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement