Advertisement
MathQ_

Untitled

Apr 7th, 2023
573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. struct indices {
  4.     int x, y;
  5. };
  6.  
  7. class matrix {
  8. private:
  9.     int Nx, Ny;
  10.     double **A;
  11.  
  12. public:
  13.     matrix() : Nx(0), Ny(0), A(nullptr) {}
  14.     matrix(int nx, int ny) : Nx(nx), Ny(ny) {
  15.         A = new double*[Nx];
  16.         for (int i = 0; i < Nx; ++i) {
  17.             A[i] = new double[Ny];
  18.             for (int j = 0; j < Ny; ++j) {
  19.                 A[i][j] = 0;
  20.             }
  21.         }
  22.     }
  23.     matrix(const matrix& other) : Nx(other.Nx), Ny(other.Ny) {
  24.         A = new double*[Nx];
  25.         for (int i = 0; i < Nx; ++i) {
  26.             A[i] = new double[Ny];
  27.             for (int j = 0; j < Ny; ++j) {
  28.                 A[i][j] = other.A[i][j];
  29.             }
  30.         }
  31.     }
  32.     ~matrix() {
  33.         for (int i = 0; i < Nx; ++i) {
  34.             delete[] A[i];
  35.         }
  36.         delete[] A;
  37.     }
  38.     matrix& operator=(const matrix& other) {
  39.         matrix tmp(other);
  40.         Nx = tmp.Nx;
  41.         Ny = tmp.Ny;
  42.         A = tmp.A;
  43.         return *this;
  44.     }
  45.     double &operator[](const indices& idx) {
  46.         return A[idx.x][idx.y];
  47.     }
  48.     friend std::ostream& operator<<(std::ostream& out, const matrix& mx) {
  49.         for (int i = 0; i < mx.Nx; ++i) {
  50.             for (int j = 0; j < mx.Ny; ++j) {
  51.                 out << mx.A[i][j] << " ";
  52.             }
  53.             out << "\n";
  54.         }
  55.         return out;
  56.     }
  57.     matrix operator*(const matrix& mx) {
  58.         if (Ny != mx.Nx) {
  59.             return matrix();
  60.         }
  61.         matrix tmp(Nx, mx.Ny);
  62.         for (int i = 0; i < Nx; ++i) {
  63.             for (int j = 0; j < mx.Ny; ++j) {
  64.                 for (int k = 0; k < Ny; ++k) {
  65.                     tmp.A[i][j] += A[i][k] * mx.A[k][j];
  66.                 }
  67.             }
  68.         }
  69.         return tmp;
  70.     }
  71.     matrix operator+(const matrix& mx) {
  72.         if (Nx != mx.Nx or Ny != mx.Ny) {
  73.             return matrix();
  74.         }
  75.         matrix tmp(mx);
  76.         for (int i = 0; i < Nx; ++i) {
  77.             for (int j = 0; j < Ny; ++j) {
  78.                 tmp.A[i][j] += A[i][j];
  79.             }
  80.         }
  81.         return tmp;
  82.     }
  83. };
  84.  
  85. int main() {
  86.     matrix A(2, 2), B(2, 2);
  87.  
  88.     A[indices{.x = 0, .y = 0}] = 1;
  89.     A[indices{.x = 0, .y = 1}] = 2;
  90.     A[indices{.x = 1, .y = 0}] = 3;
  91.     A[indices{.x = 1, .y = 1}] = 4;
  92.  
  93.     B[indices{.x = 0, .y = 0}] = 1;
  94.     B[indices{.x = 0, .y = 1}] = 2;
  95.     B[indices{.x = 1, .y = 0}] = 3;
  96.     B[indices{.x = 1, .y = 1}] = 4;
  97.  
  98.     std::cout << A + B << '\n';
  99.     std::cout << A * B << '\n';
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement