diff --git a/main.cpp b/main.cpp index 797afabbc575c710f482c12fb2415a8818839bb6..f35f78d9ad401f85451b19cf2eacc24167d2f275 100644 --- a/main.cpp +++ b/main.cpp @@ -2,5 +2,11 @@ int main() { test_trace(); + test_norm(); + test_operators(); + runMatrixGoodTests1(); + runMatrixGoodTests2(); + runMatrixGoodTests4(); + runMatrixGoodTests3(); return 0; } \ No newline at end of file diff --git a/matrix1.cpp b/matrix1.cpp index 6c130f87981309483973b1967cdec80a4b9b83c4..7c1a573e0ac0391a53fa9ea484a0b140df6ba0ba 100644 --- a/matrix1.cpp +++ b/matrix1.cpp @@ -143,88 +143,131 @@ namespace linalg { } double& Matrix::operator()(int row, int col) { - if (row < 0 || row >= m_rows || col < 0 || col >= m_columns) { - throw std::runtime_error("Рндекс неправильный!"); + try { + if (row < 0 || row >= m_rows || col < 0 || col >= m_columns) { + throw std::runtime_error("Рндекс неправильный!"); + } else { + return m_ptr[row * m_columns + col]; + } + } catch (const std::runtime_error& e) { + std::cerr << "Error: " << e.what() << std::endl; + return m_ptr[0]; } - return m_ptr[row * m_columns + col]; } const double& Matrix::operator()(int row, int col) const { - if (row < 0 || row >= m_rows || col < 0 || col >= m_columns) { - throw std::runtime_error("Рндекс неправильный!"); + try { + if (row < 0 || row >= m_rows || col < 0 || col >= m_columns) { + throw std::runtime_error("Рндекс неправильный!"); + } else { + return m_ptr[row * m_columns + col]; + } + } catch (const std::runtime_error& e) { + std::cerr << "Error: " << e.what() << std::endl; + return m_ptr[0]; } - return m_ptr[row * m_columns + col]; } Matrix Matrix::operator+(const Matrix &other) const { - if (m_rows != other.m_rows || m_columns != other.m_columns) { - throw std::runtime_error("Матрицы СЃ таким размероа нельзя складывать или вычитать."); - } - Matrix result(m_rows, m_columns); - for (int i = 0; i < m_rows; i++) { - for (int j = 0; j < m_columns; j++) { - result(i, j) = m_ptr[i * m_columns + j] + other(i, j); + + try { + if (m_rows != other.m_rows || m_columns != other.m_columns) { + throw std::runtime_error("Матрицы СЃ таким размероа нельзя складывать или вычитать."); + } else { + Matrix result(m_rows, m_columns); + for (int i = 0; i < m_rows; i++) { + for (int j = 0; j < m_columns; j++) { + result(i, j) = m_ptr[i * m_columns + j] + other(i, j); + } + } + return result; } + } catch (const std::runtime_error& e) { + std::cerr << "Error: " << e.what() << std::endl; + return other; } - return result; } // Перегрузка оператора += Matrix &Matrix::operator+=(const Matrix &other) { - if (m_rows != other.m_rows || m_columns != other.m_columns) { - throw std::runtime_error("Матрицы СЃ таким размероа нельзя складывать или вычитать."); - } - for (int i = 0; i < m_rows; i++) { - for (int j = 0; j < m_columns; j++) { - m_ptr[i * m_columns + j] += other(i, j); + try { + if (m_rows != other.m_rows || m_columns != other.m_columns) { + throw std::runtime_error("Матрицы СЃ таким размероа нельзя складывать или вычитать."); + } else { + for (int i = 0; i < m_rows; i++) { + for (int j = 0; j < m_columns; j++) { + m_ptr[i * m_columns + j] += other(i, j); + } + } + return *this; } + } catch (const std::runtime_error& e) { + std::cerr << "Error: " << e.what() << std::endl; + return *this; } - return *this; } // Перегрузка оператора - Matrix Matrix::operator-(const Matrix &other) const { - if (m_rows != other.m_rows || m_columns != other.m_columns) { - throw std::runtime_error("Матрицы СЃ таким размероа нельзя складывать или вычитать."); - } - Matrix result(m_rows, m_columns); - for (int i = 0; i < m_rows; i++) { - for (int j = 0; j < m_columns; j++) { - result(i, j) = m_ptr[i * m_columns + j] - other(i, j); + try { + if (m_rows != other.m_rows || m_columns != other.m_columns) { + throw std::runtime_error("Матрицы СЃ таким размероа нельзя складывать или вычитать."); + } else { + Matrix result(m_rows, m_columns); + for (int i = 0; i < m_rows; i++) { + for (int j = 0; j < m_columns; j++) { + result(i, j) = m_ptr[i * m_columns + j] - other(i, j); + } + } + return result; } + } catch (const std::runtime_error& e) { + std::cerr << "Error: " << e.what() << std::endl; + return other; } - return result; } // Перегрузка оператора -= Matrix &Matrix::operator-=(const Matrix &other) { - if (m_rows != other.m_rows || m_columns != other.m_columns) { - throw std::runtime_error("Матрицы СЃ таким размероа нельзя складывать или вычитать."); - } - for (int i = 0; i < m_rows; i++) { - for (int j = 0; j < m_columns; j++) { - m_ptr[i * m_columns + j] -= other(i, j); + try { + if (m_rows != other.m_rows || m_columns != other.m_columns) { + throw std::runtime_error("Матрицы СЃ таким размероа нельзя складывать или вычитать."); + } else { + for (int i = 0; i < m_rows; i++) { + for (int j = 0; j < m_columns; j++) { + m_ptr[i * m_columns + j] -= other(i, j); + } + } + return *this; } + } catch (const std::runtime_error& e) { + std::cerr << "Error: " << e.what() << std::endl; + return *this; } - return *this; } // Перегрузка оператора * Matrix Matrix::operator*(const Matrix &other) const { - if (m_columns != other.m_rows) { - throw std::runtime_error("Матрицы СЃ таким размероа нельзя перемножить."); - } - Matrix result(m_rows, other.m_columns); - for (int i = 0; i < m_rows; i++) { - for (int j = 0; j < other.m_columns; j++) { - double sum = 0.0; - for (int k = 0; k < m_columns; k++) { - sum += m_ptr[i * m_columns + k] * other(k, j); + try { + if (m_rows != other.m_rows || m_columns != other.m_columns) { + throw std::runtime_error("Матрицы СЃ таким размероа нельзя перемножать."); + } else { + Matrix result(m_rows, other.m_columns); + for (int i = 0; i < m_rows; i++) { + for (int j = 0; j < other.m_columns; j++) { + double sum = 0.0; + for (int k = 0; k < m_columns; k++) { + sum += m_ptr[i * m_columns + k] * other(k, j); + } + result(i, j) = sum; + } } - result(i, j) = sum; + return result; } + } catch (const std::runtime_error& e) { + std::cerr << "Error: " << e.what() << std::endl; + return other; } - return result; } // Перегрузка оператора * для умножения РЅР° число @@ -245,11 +288,17 @@ namespace linalg { // Перегрузка оператора *= Matrix &Matrix::operator*=(const Matrix &other) { - if (m_columns != other.m_rows) { - throw std::runtime_error("Матрицы СЃ таким размероа нельзя перемножить."); + try { + if (m_rows != other.m_rows || m_columns != other.m_columns) { + throw std::runtime_error("Матрицы СЃ таким размероа нельзя перемножать."); + } else { + *this = *this * other; + return *this; + } + } catch (const std::runtime_error& e) { + std::cerr << "Error: " << e.what() << std::endl; + return *this; } - *this = *this * other; - return *this; } // Перегрузка оператора *= для умножения РЅР° число @@ -296,10 +345,6 @@ namespace linalg { } double Matrix::trace() const { - if (empty() || m_rows != m_columns) { - throw std::runtime_error("Матрица РЅРµ является квадратной или пустой."); - } - double sum = 0.0; for (int i = 0; i < m_rows; ++i) { sum += m_ptr[i * m_columns + i]; diff --git a/test1.cpp b/test1.cpp index ef863c3f17ccbd546a43e847113f395da18c209f..e4a84406bdac477950d5cce7c219b58b73ddf977 100644 --- a/test1.cpp +++ b/test1.cpp @@ -7,9 +7,170 @@ void printMatrix(const linalg::Matrix& matrix, const std::string& label) { std::cout << matrix; } +void runMatrixGoodTests1() { + 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; +} + +void runMatrixBadTests1() { + std::cout << "Тест СЃ ошибками"; + linalg::Matrix matrix(-1, 2); + 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; +} + +void runMatrixGoodTests2() { + 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 runMatrixBadTests2() { + 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; + } +} + +void runMatrixGoodTests4() { + linalg::Matrix m0; + linalg::Matrix m1(4); + linalg::Matrix m2(4, 6); + linalg::Matrix m3(m1); + linalg::Matrix m4(std::move(m2)); + linalg::Matrix m5 = { {1, 2234252, 3244513225.1245123}, {4, 5, 6} }; + linalg::Matrix m6 = { {1, 2, 3, 4, 5, 6} }; + linalg::Matrix m7 = {1, 2, 3, 4, 5, 6}; + linalg::Matrix m8 = { {1}, {2}, {3}, {4}, {5}, {6} }; + std::cout << m0 << '\n' << m1 << '\n' << m2 << '\n' << m3 << '\n' << m4 << '\n' << m5 << '\n' << m6 << '\n' << m7 << '\n' << m8; + std::cout << "Матрица РґРѕ reshape:\n"; + std::cout << m5; + std::cout << "Матрица после reshape:\n"; + m5.reshape(6, 1); + std::cout << m5; +} + +void runMatrixGoodTests3() { + linalg::Matrix m1 = { {1.0, 2.0}, {3.0, 4.0} }; + + linalg::Matrix m2 = {{1, 3}}; + std::cout << "Значение РґРѕ приравнивания: \n" << m2; + m2 = m1; + std::cout << "Значение после приравнивания: \n" << m2; + + + linalg::Matrix m3 = { {1.0, 2.0, 3.0}, {4.0, 5.0, 6.0} }; + double val = m3(0, 2); // Получить значение элемента + std::cout << "m9(0, 2): " << val << std::endl; + + m3(0, 2) = 7.0; // Рзменить значение элемента + + const linalg::Matrix c_m = { {1.0, 2.0, 3.0}, {4.0, 5.0, 6.0} }; + double val_c = c_m(0, 2); // Получить значение элемента РёР· константной матрицы + std::cout << "Значение РёР· const matrix: " << val_c << std::endl; + std::cout << "Попытка изменить элемент РІ константной матрице вызовет ошибку"; + // c_m(0, 2) = 7.0; // РћРЁРБКА +} + + +void test_operators() { + linalg::Matrix m1 = { {1.0, 2.0}, {4.0, 5.0} }; + linalg::Matrix m2 = { {5.0, 6.0}, {7.0, 8.0} }; + double num = 3.0; + + printMatrix(m1, "Матрица 1"); + printMatrix(m2, "Матрица 2"); + std::cout << "Скаляр: " << num << std::endl; + + // Проверка оператора сложения (+) + linalg::Matrix result = m1 + m2; + printMatrix(result, "Результат сложения"); + + // Проверка оператора сложения (+=) + m1 += m2; + printMatrix(m1, "Результат сложения (+=)"); + + // Проверка оператора вычитания (-) + linalg::Matrix result2 = m1 - m2; + printMatrix(result2, "Результат вычитания"); + + // Проверка оператора вычитания (-=) + m1 -= m2; + printMatrix(m1, "Результат вычитания (-=)"); + + // Проверка оператора умножения (*) + linalg::Matrix result3 = m1 * m2; + printMatrix(result3, "Результат умножения"); + + // Проверка оператора умножения РЅР° число (double * Matrix) + linalg::Matrix result4 = num * m1; + printMatrix(result4, "Результат умножения РЅР° скаляр"); + linalg::Matrix result5 = m1 * num; + printMatrix(result5, "Результат умножения РЅР° скаляр (обратное)"); + + // Проверка оператора умножения *= + m1 *= m2; + printMatrix(m1, "Результат умножения (*=) для матриц"); + // Проверка оператора умножения РЅР° число (Matrix *= double) + m1 *= num; + printMatrix(m1, "Результат умножения РЅР° скаляр (*=)"); + // Проверка оператора сравнения (==) + bool isEqual = m1 == m2; + std::cout << "Матрица 1 == Матрица 2: " << std::boolalpha << isEqual << std::endl; + // Проверка оператора сравнения (!=) + bool isNotEqual = m1 != m2; + std::cout << "Матрица 1 != Матрица 2: " << std::boolalpha << isNotEqual << std::endl; + linalg::Matrix m3 = { {1.0, 2.0}, {3.0, 4.0}, {5, 6} }; + std::cout << "РќРѕРІСѓСЋ матрицу умножаем РЅР° РґСЂСѓРіСѓСЋ Рё получаем ошибку\n" << m3; + m1 *= m3; + +} + +void test_norm() { + linalg::Matrix m1 = { {1.0, 2.0}, {3.0, 4.0} }; + printMatrix(m1, "m1"); + std::cout << m1.norm(); +} + void test_trace() { linalg::Matrix m1 = { {1.0, 2.0}, {3.0, 4.0} }; printMatrix(m1, "m1"); std::cout << m1.trace(); } + + + diff --git a/test1.h b/test1.h index 8a52f7dad810645f40cc189b350c805b409e29e2..48faac3b66ed956d9cd2e670c93c816bc0ac5cd8 100644 --- a/test1.h +++ b/test1.h @@ -2,3 +2,13 @@ void printMatrix(); void test_trace(); +void test_norm(); +void test_operators(); +void runMatrixGoodTests1(); +void runMatrixBadTests1(); +void runMatrixGoodTests2(); +void runMatrixBadTests2(); +void runMatrixGoodTests4(); +void runMatrixGoodTests3(); + +