From ed596baf4673b9e0302e34f1671566026afc6db3 Mon Sep 17 00:00:00 2001 From: blank2581538 <rrshagin@edu.hse.ru> Date: Thu, 23 Jan 2025 22:21:37 +0300 Subject: [PATCH] added loadmatrix --- source/procedure.cpp | 34 +++++++++++++++++++++++++++++++++- source/procedure.h | 4 +++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/source/procedure.cpp b/source/procedure.cpp index cac56d9..7815650 100644 --- a/source/procedure.cpp +++ b/source/procedure.cpp @@ -27,7 +27,7 @@ bool check2_char(std::istream& in, char expected) { } return true; } -linalg::Matrix<Complex> readrow(std::istream& fin) { +linalg::Matrix<Complex> readrow(std::istream& fin) {//Файловый поток для чтения и записи в файл size_t count = 1; linalg::Matrix<Complex> row; if (!check2_char(fin, '|')) { @@ -44,3 +44,35 @@ linalg::Matrix<Complex> readrow(std::istream& fin) { } return row; } +linalg::Matrix<Complex> loadmatrix(const char* file_name) { + std::ifstream file(file_name); + if (!file) throw std::runtime_error("Could not open the file!\n"); + linalg::Matrix<Complex> first_row = readrow(file); + if (first_row.empty()) throw std::runtime_error("Nothing to analyze because the matrix is empty!\n"); + linalg::Matrix<Complex> result(1, first_row.size()); + for (size_t i = 0; i < result.columns(); ++i) { + result(0, i) = first_row(0, i); + } + size_t row_count = 1; + for (char ch; file >> ch;) { + file.unget(); + linalg::Matrix<Complex> row = readrow(file); + if (row.size() != first_row.size()) { + throw std::runtime_error("The number of elements in the rows is not same!\n"); + } + result.reshape(++row_count, first_row.size()); + for (size_t j = 0; j < result.columns(); ++j) { + result(row_count - 1, j) = row(0, j); + } + } + return result; +} +void analyse(const linalg::Matrix<Complex>& matr, const char* file_name) { + std::cout << "The original matrix was read from file " << file_name << ":\n"; + std::cout << matr << '\n'; + std::cout << "Transposed matrix:\n" << linalg::transpose(matr) << '\n'; + std::cout << "Trace: " << matr.trace() << "\n\n"; + std::cout << "Determinant: " << matr.det() << "\n\n"; + std::cout << "Inverted matrix:\n" << linalg::invert(matr) << '\n'; +} + diff --git a/source/procedure.h b/source/procedure.h index e204681..6c0ae96 100644 --- a/source/procedure.h +++ b/source/procedure.h @@ -4,4 +4,6 @@ const char* collate(int arg_count, char* args[]); void deal(std::exception& exc) noexcept; bool check2_char(std::istream& in, char expected); -linalg::Matrix<Complex> readrow(std::istream& fin); \ No newline at end of file +linalg::Matrix<Complex> readrow(std::istream& fin); +linalg::Matrix<Complex> loadmatrix(const char* file_name); +void analyse(const linalg::Matrix<Complex>& matr, const char* file_name); \ No newline at end of file -- GitLab