Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <vector>
- using std::cin;
- using std::cout;
- using std::vector;
- using std::ostream;
- using std::string;
- using std::pair;
- using std::make_pair;
- template <typename T>
- class Matrix {
- private:
- vector<vector<T>> body;
- public:
- Matrix(const vector<vector<T>>& d)
- : body(d) {
- }
- // размер
- pair<size_t, size_t> size() const {
- pair<size_t, size_t> a = make_pair(body.size(), body.front().size());
- return a;
- }
- T& operator() (size_t i, size_t j) {
- return body[i][j];
- }
- const T& operator() (size_t i, size_t j) const {
- return body[i][j];
- }
- //сложение и скаляр
- Matrix& operator += (const Matrix& other) {
- for (size_t i = 0; i != body.size(); ++i)
- for (size_t j = 0; j != body[i].size(); ++j)
- body[i][j] += other.body[i][j];
- return *this;
- }
- Matrix operator + (const Matrix& other) const {
- auto C = *this;
- C += other;
- return C;
- }
- template <typename N>
- Matrix& operator *= (const N& scalar) {
- for (size_t i = 0; i != body.size(); ++i) {
- for (size_t j = 0; j != body[i].size(); ++j) {
- body[i][j] *= scalar;
- }
- }
- return *this;
- }
- template <typename N>
- Matrix operator * (const N& scalar) const {
- auto C = *this;
- C *= scalar;
- return C;
- }
- //транспозиция
- Matrix& transpose() {
- size_t n = body.size();
- size_t m = body.front().size();
- vector<vector<T>> matT(m, vector<T>(n));
- for (size_t i = 0; i != n; ++i) {
- for (size_t j = 0; j != m; ++j) {
- matT[j][i] = body[i][j];
- }
- }
- body = matT;
- return *this;
- }
- Matrix transposed() const {
- Matrix mat = *this;
- return mat.transpose();
- }
- //перемножение
- Matrix& operator *= (const Matrix& other) {
- size_t m = body.size();
- size_t n = body.front().size();
- size_t p = other.size().second;
- std::vector<std::vector<T>> prod(m, std::vector<T>(p, 0));
- for (size_t i = 0; i != m; ++i) {
- for (size_t j = 0; j != p; ++j) {
- for (size_t k = 0; k != n; ++k) {
- prod[i][j] += body[i][k] * other.body[k][j];
- }
- }
- }
- body = prod;
- return *this;
- }
- Matrix operator * (const Matrix& other) const {
- Matrix prod = *this;
- prod *= other;
- return prod;
- }
- //итераторы
- class Iter {
- private:
- size_t row, col;
- Matrix<T> mat;
- public:
- Iter(const size_t i, const size_t j, const Matrix<T>& a)
- : row(i)
- , col(j)
- , mat(a) {
- }
- T operator * () const {
- return mat(row, col);
- }
- Iter& operator++ () {
- ++col;
- if (col >= mat.size().second) {
- ++row;
- col = 0;
- }
- return *this;
- }
- bool operator == (const Iter& other) const {
- return row == other.row && col == other.col;
- }
- bool operator != (const Iter& other) const {
- return !(*this == other);
- }
- };
- Iter begin() const {
- return Iter(0, 0, body);
- }
- Iter begin() {
- return Iter(0, 0, body);
- }
- Iter end() const {
- return Iter(body.size(), 0, body);
- }
- Iter end() {
- return Iter(body.size(), 0, body);
- }
- };
- template <typename T>
- std::ostream& operator << (std::ostream& out, const Matrix<T>& mat) {
- string line = "";
- for (size_t i = 0; i != mat.size().first; ++i) {
- string space = "";
- out << line;
- for (size_t j = 0; j != mat.size().second; ++j) {
- out << space << mat(i, j);
- space = "\t";
- }
- line = "\n";
- }
- return out;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement