Advertisement
NickAndNick

class Matrix (static array)

Jul 27th, 2024
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.00 KB | Software | 0 0
  1. #include <iomanip>
  2. #include <iostream>
  3. #include <random>
  4.  
  5. template<typename Type, size_t Size, typename = std::enable_if_t<std::is_arithmetic_v<Type>>>
  6. class Array {
  7. public:
  8.     Array() : sequence{} {}
  9.     auto& operator[](size_t i) {
  10.         return sequence[i];
  11.     }
  12.     auto& operator[](size_t i) const {
  13.         return sequence[i];
  14.     }
  15.     auto begin() {
  16.         return &sequence[0];
  17.     }
  18.     auto end() {
  19.         return &sequence[Size];
  20.     }
  21.     auto begin() const {
  22.         return &sequence[0];
  23.     }
  24.     auto end() const {
  25.         return &sequence[Size];
  26.     }
  27.     constexpr size_t size() const {
  28.         return Size;
  29.     }
  30. private:
  31.     Type sequence[Size];
  32. };
  33.  
  34. template<typename Type, size_t Rows, size_t Columns, typename = std::enable_if_t<std::is_arithmetic_v<Type>>>
  35. class Matrix {
  36. public:
  37.     auto& operator[](size_t i) const {
  38.         return matrix[i];
  39.     }
  40.     auto& operator[](size_t i) {
  41.         return matrix[i];
  42.     }
  43.     Matrix operator+(const Matrix& mx) {
  44.         auto tmp = *this;
  45.         for (size_t i = 0; i < Rows; ++i) {
  46.             for (size_t j = 0; j < Columns; ++j) {
  47.                 tmp[i][j] += mx[i][j];
  48.             }
  49.         }
  50.         return tmp;
  51.     }
  52.     Matrix operator-(const Matrix& mx) {
  53.         auto tmp = *this;
  54.         for (size_t i = 0; i < Rows; ++i) {
  55.             for (size_t j = 0; j < Columns; ++j) {
  56.                 tmp[i][j] -= mx[i][j];
  57.             }
  58.         }
  59.         return tmp;
  60.     }
  61.     bool is_compatibility(const Matrix& mx) const {
  62.         return rows() == mx.rows() && columns() == mx.columns();
  63.     }
  64.     size_t rows() const {
  65.         return Rows;
  66.     }
  67.     size_t columns() const {
  68.         return Columns;
  69.     }
  70.     auto begin() {
  71.         return &matrix[0];
  72.     }
  73.     auto end() {
  74.         return &matrix[Rows];
  75.     }
  76.     auto begin() const {
  77.         return &matrix[0];
  78.     }
  79.     auto end() const {
  80.         return &matrix[Rows];
  81.     }
  82. private:
  83.     Array<Type, Columns> matrix[Rows];
  84.     friend Matrix operator*(const Matrix& a, const Matrix& b) {
  85.         static const auto rows = a.rows();
  86.         static const auto columns = b.columns();
  87.         const auto count = a.columns();
  88.         auto tmp = Matrix{};
  89.         for (size_t i = 0; i < rows; ++i) {
  90.             for (size_t j = 0; j < columns; ++j) {
  91.                 for (size_t k = 0; k < count; ++k) {
  92.                     tmp[i][j] += a[i][k] * b[k][j];
  93.                 }
  94.             }
  95.         }
  96.         return tmp;
  97.     }
  98. };
  99.  
  100. using Matrix_int_3_3 = Matrix<int, 3, 3>;
  101.  
  102. void random_fill(Matrix_int_3_3& matrix, int a, int b) {
  103.     if (a > b) std::swap(a, b);
  104.     std::uniform_int_distribution<> uid(a, b);
  105.     std::mt19937 gen{ std::random_device()() };
  106.     for (auto& row : matrix) {
  107.         for (auto& value : row) {
  108.             value = uid(gen);
  109.         }
  110.     }
  111. }
  112.  
  113. void show(const char* prompt, const Matrix_int_3_3& matrix, std::streamsize width) {
  114.     std::cout << prompt << "\n\n";
  115.     for (const auto& row : matrix) {
  116.         for (const auto value : row) {
  117.             std::cout << std::setw(width) << value << ' ';
  118.         }
  119.         std::cout.put('\n');
  120.     }
  121.     std::cout.put('\n');
  122. }
  123.  
  124. int main() {
  125.     constexpr auto l = 1;
  126.     constexpr auto r = 9;
  127.     constexpr std::streamsize w = 5;
  128.     Matrix_int_3_3 matrix_a{};
  129.     random_fill(matrix_a, l, r);
  130.     show("Matrix A:", matrix_a, w);
  131.     Matrix_int_3_3 matrix_b{};
  132.     random_fill(matrix_b, l, r);
  133.     show("Matrix B:", matrix_b, w);
  134.     Matrix_int_3_3 matrix_c{};
  135.     random_fill(matrix_c, l, r);
  136.     show("Matrix C:", matrix_c, w);
  137.     const auto matrix_sum_ab = matrix_a + matrix_b;
  138.     show("Matrix A + B:", matrix_sum_ab, w);
  139.     const auto matrix_sub_ac = matrix_a - matrix_c;
  140.     show("Matrix A - C:", matrix_sub_ac, w);
  141.     if (matrix_b.is_compatibility(matrix_c)) {
  142.         const auto matrix_product_bc = matrix_b * matrix_c;
  143.         show("Matrix B x C:", matrix_product_bc, w);
  144.     } else {
  145.         std::cout << "Matrices A and B are incompatible\n";
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement