From 8712a104891c5dc55079ddf41ac3eda70cb726c5 Mon Sep 17 00:00:00 2001
From: Mikhail Scherbakov <mkscherbakov@edu.hse.ru>
Date: Sun, 22 Oct 2023 21:19:29 +0300
Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5=202(?=
 =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D1=8B=D0=B5=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82?=
 =?UTF-8?q?=D1=80=D1=83=D0=BA=D1=82=D0=BE=D1=80=D1=8B)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 matrix1.cpp | 61 +++++++++++++++++++++++++++++++++++++++++------------
 matrix1.h   | 23 +++++++++++++++++++-
 test1.cpp   | 46 ++++++++++++++++++++++++++++------------
 3 files changed, 102 insertions(+), 28 deletions(-)

diff --git a/matrix1.cpp b/matrix1.cpp
index eb45089..5ea3aff 100644
--- a/matrix1.cpp
+++ b/matrix1.cpp
@@ -1,42 +1,77 @@
 #include "matrix1.h"
 #include <iostream>
+#include <algorithm>
+#include <initializer_list>
 
 namespace linalg {
+    // Конструкторы
     Matrix::Matrix() : m_ptr(nullptr), m_rows(0), m_columns(0) {}
 
-    Matrix::Matrix(int rows, int columns) {
-        try {
-            if (rows <= 0 || columns <= 0) {
-                m_ptr = nullptr;
-                m_rows = 0;
-                m_columns = 0;
-                throw std::runtime_error("Строки и столбцы должны быть положительными!");
-            } else {
-                m_ptr = new double[rows * columns];
-                m_rows = rows;
-                m_columns = columns;
+    Matrix::Matrix(int rows) : m_rows(rows), m_columns(1) {
+        if (rows <= 0) {
+            throw std::runtime_error("Строки и столбцы должны быть положительными!");
+        }
+        m_ptr = new double[rows];
+    }
+
+    Matrix::Matrix(int rows, int columns) : m_rows(rows), m_columns(columns) {
+        if (rows <= 0 || columns <= 0) {
+            throw std::runtime_error("Строки и столбцы должны быть положительными!");
+        }
+        m_ptr = new double[rows * columns];
+    }
+
+    Matrix::Matrix(const Matrix& other) : m_rows(other.m_rows), m_columns(other.m_columns) {
+        m_ptr = new double[m_rows * m_columns];
+        std::copy(other.m_ptr, other.m_ptr + (m_rows * m_columns), m_ptr);
+    }
+
+    Matrix::Matrix(Matrix&& other) noexcept : m_ptr(other.m_ptr), m_rows(other.m_rows), m_columns(other.m_columns) {
+        other.m_ptr = nullptr;
+        other.m_rows = 0;
+        other.m_columns = 0;
+    }
+
+    Matrix::Matrix(std::initializer_list<std::initializer_list<double>> init_list) {
+        m_rows = init_list.size();
+        m_columns = init_list.begin()->size();
+
+        for (const auto& row : init_list) {
+            if (row.size() != static_cast<size_t>(m_columns)) {
+                throw std::invalid_argument("Непостоянное количество столбцов в init_list.");
             }
-        } catch (const std::runtime_error& e) {
-            std::cerr << "Error: " << e.what() << std::endl;
+        }
+
+        m_ptr = new double[m_rows * m_columns];
+        int i = 0;
+        for (const auto& row : init_list) {
+            std::copy(row.begin(), row.end(), m_ptr + i * m_columns);
+            i++;
         }
     }
 
+    // Деструктор
     Matrix::~Matrix() {
         delete[] m_ptr;
     }
 
+
+// Метод для получения количества строк
     int Matrix::rows() const {
         return m_rows;
     }
 
+    // Метод для получения количества колонок
     int Matrix::columns() const {
         return m_columns;
     }
 
+    // Метод для проверки, является ли матрицей пустой
     bool Matrix::empty() const {
         return m_rows == 0 || m_columns == 0;
     }
 
+    // Метод для изменения размерности матрицы
     void Matrix::reshape(int rows, int columns) {
         try {
             if (rows * columns != m_rows * m_columns || rows <= 0 || columns <= 0) {
diff --git a/matrix1.h b/matrix1.h
index 35c99e4..b878714 100644
--- a/matrix1.h
+++ b/matrix1.h
@@ -1,16 +1,37 @@
 #pragma once
+#include <initializer_list>
+#include <iostream>
 
 namespace linalg {
     class Matrix {
     private:
-        double *m_ptr;
+        double* m_ptr;
         int m_rows;
         int m_columns;
 
     public:
         Matrix();
+        Matrix(int rows);
         Matrix(int rows, int columns);
+        Matrix(const Matrix& other);
+        Matrix(Matrix&& other) noexcept;
+        Matrix(std::initializer_list<std::initializer_list<double>> init_list);
+        template <typename T>
+        Matrix(std::initializer_list<T> init_list) {
+            m_rows = 1;
+            m_columns = init_list.size();
+            if (m_columns == 0) {
+                throw std::runtime_error("init_list не может быть пустым.");
+            }
+            m_ptr = new double[m_columns];
+            int i = 0;
+            for (const auto& val : init_list) {
+                m_ptr[i] = static_cast<double>(val);
+                i++;
+            }
+        }
         ~Matrix();
+
         int rows() const;
         int columns() const;
         bool empty() const;
diff --git a/test1.cpp b/test1.cpp
index 6ddeaa0..508719e 100644
--- a/test1.cpp
+++ b/test1.cpp
@@ -2,23 +2,41 @@
 #include "matrix1.h"
 #include <iostream>
 
-
 void runMatrixGoodTests() {
-    std::cout << "Тест без ошибок";
-    linalg::Matrix matrix(2, 3);
-    std::cout << "Строки: " << matrix.rows() << ", Колонки: " << matrix.columns() << std::endl;
-    std::cout << "Is empty: " << matrix.empty() << std::endl;
-
-    matrix.reshape(3, 2);
-    std::cout << "Reshaped: Строки: " << matrix.rows() << ", Колонки: " << matrix.columns() << std::endl;
+    std::cout << "Хороший тест\n";
+    linalg::Matrix m0;
+    std::cout << "Матрица m0: " <<  "Строки: " << m0.rows() << ", Колонки: " << m0.columns() << std::endl;
+    linalg::Matrix m1(4);
+    std::cout << "Матрица m1(4): " <<  "Строки: " << m1.rows() << ", Колонки: " << m1.columns() << std::endl;
+    linalg::Matrix m2(4, 6);
+    std::cout << "Матрица m2(4, 6): " <<  "Строки: " << m2.rows() << ", Колонки: " << m2.columns() << std::endl;
+    linalg::Matrix m3(m1);
+    std::cout << "Матрица m3(m1): " <<  "Строки: " << m3.rows() << ", Колонки: " << m3.columns() << std::endl;
+    linalg::Matrix m4(std::move(m2));
+    std::cout << "Матрица m4(std::move(m2)): " <<  "Строки: " << m4.rows() << ", Колонки: " << m4.columns() << std::endl;
+    linalg::Matrix m5 = { {1, 2, 3}, {4, 5, 6} };
+    std::cout << "Матрица m5 = { {1, 2, 3}, {4, 5, 6} }: " <<  "Строки: " << m5.rows() << ", Колонки: " << m5.columns() << std::endl;
+    linalg::Matrix m6 = { {1, 2, 3, 4, 5, 6} };
+    std::cout << "Матрица m6 = { {1, 2, 3, 4, 5, 6} }: " <<  "Строки: " << m6.rows() << ", Колонки: " << m6.columns() << std::endl;
+    linalg::Matrix m7 = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
+    std::cout << "Матрица m7 = {1, 2, 3, 4, 5, 6}: " <<  "Строки: " << m7.rows() << ", Колонки: " << m7.columns() << std::endl;
+    linalg::Matrix m8 = { {1}, {2}, {3}, {4}, {5}, {6} };
+    std::cout << "Матрица m8 = { {1}, {2}, {3}, {4}, {5}, {6} }: " <<  "Строки: " << m8.rows() << ", Колонки: " << m8.columns() << std::endl;
 }
 
 void runMatrixBadTests() {
-    std::cout << "Тест с ошибками";
-    linalg::Matrix matrix(-1, 2);
-    std::cout << "Строки: " << matrix.rows() << ", Колонки: " << matrix.columns() << std::endl;
-    std::cout << "Is empty: " << matrix.empty() << std::endl;
+    try {
+        std::cout << "Плохой тест\n";
+        std::cout << "Матрица m5 = { {1, 2, 3}, {4, 5, 6, 7} }: \n";
+        linalg::Matrix m5 = {{1, 2, 3},
+                             {4, 5, 6, 7}};
+        std::cout << "Матрица m1(-4): \n";
+        linalg::Matrix m1(-4);
+        std::cout << "Матрица m2(-4, 3): \n";
+        linalg::Matrix m2(-4, 3);
+
+    } catch (const std::exception& e) {
+        std::cerr << "Error: " << e.what() << std::endl;
+    }
 
-    matrix.reshape(3, 2);
-    std::cout << "Reshaped: Строки: " << matrix.rows() << ", Колонки: " << matrix.columns() << std::endl;
 }
\ No newline at end of file
-- 
GitLab