As matrizes podem ter até 30000 linhas e 30000 colunas. ?? como resolver esse problema…
- User
As matrizes podem ter até 30000 linhas e 30000 colunas. ?? como resolver esse problema
-----------------------main.cpp-------------------------- #include #include #include "SparseMatrix.h"
SparseMatrix* readSparseMatrix(std::string file_name) { std::ifstream file(file_name);
if (!file) { std::cout << "Error opening file: " << file_name << std::endl; return nullptr; } int rows, cols; file >> rows >> cols; SparseMatrix* matrix = new SparseMatrix(rows, cols); int row, col; double value; while (file >> row >> col >> value) { matrix->insert(row, col, value); } file.close(); return matrix;}
SparseMatrix* sum(SparseMatrix* A, SparseMatrix* B) { if (A->getRows() != B->getRows() || A->getCols() != B->getCols()) { std::cout << "Matrices must have the same dimensions for addition." << std::endl; return nullptr; }
SparseMatrix* result = new SparseMatrix(A->getRows(), A->getCols()); for (int i = 0; i < A->getRows(); i++) { for (int j = 0; j < A->getCols(); j++) { double valueA = A->get(i, j); double valueB = B->get(i, j); result->insert(i, j, valueA + valueB); } } return result;}
SparseMatrix* multiply(SparseMatrix* A, SparseMatrix* B) { if (A->getCols() != B->getRows()) { std::cout << "The number of columns in matrix A must be equal to the number of rows in matrix B for multiplication." << std::endl; return nullptr; }
SparseMatrix* result = new SparseMatrix(A->getRows(), B->getCols()); for (int i = 0; i < A->getRows(); i++) { for (int j = 0; j < B->getCols(); j++) { double value = 0.0; for (int k = 0; k < A->getCols(); k++) { value += A->get(i, k) * B->get(k, j); } result->insert(i, j, value); } } return result;}
int main() { // Exemplo de uso SparseMatrix* A = readSparseMatrix("matrixA.txt"); SparseMatrix* B = readSparseMatrix("matrixB.txt");
if (A != nullptr && B != nullptr) { std::cout << "Matrix A:" << std::endl; A->print(); std::cout << "Matrix B:" << std::endl; B->print(); SparseMatrix* sumResult = sum(A, B); if (sumResult != nullptr) { std::cout << "Sum of matrices:" << std::endl; sumResult->print(); delete sumResult; } SparseMatrix* multiplyResult = multiply(A, B); if (multiplyResult != nullptr) { std::cout << "Multiplication of matrices:" << std::endl; multiplyResult->print(); delete multiplyResult; } } delete A; delete B; return 0;}
----------------------SparseMatrix.h------------------------------ #ifndef SPARSE_MATRIX_H #define SPARSE_MATRIX_H
#include "node.h" // Inclua o arquivo node.h
class SparseMatrix { public: SparseMatrix(int m, int n); ~SparseMatrix(); void insert(int i, int j, double value); double get(int i, int j); void print(); int getRows() { return rows; } int getCols() { return cols; }
- Assistant
Para resolver o problema de lidar com matrizes muito grandes (com até 30.000 linhas e 30.000 colunas), você pode seguir algumas estratégias:
-
Mantenha a estrutura de matriz esparsa: a estrutura de matriz esparsa ajuda a armazenar somente os elementos não nulos de uma matriz, economizando espaço e tempo computacional em muitas operações. No entanto, pode haver algumas melhorias na implementação atual:
-
- User
o codigo fica em loop inifinito quanto tem uma matrx de 30.000 linhas e 30.000 colunas
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.