2019-01-08 17:12:05 +01:00
|
|
|
/* $Header: /var/lib/cvs/dynare_cpp/sylv/testing/MMMatrix.cpp,v 1.1.1.1 2004/06/04 13:01:13 kamenik Exp $ */
|
|
|
|
|
|
|
|
/* Tag $Name: $ */
|
|
|
|
|
|
|
|
#include "MMMatrix.hh"
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
MMMatrixIn::MMMatrixIn(const char *fname)
|
|
|
|
{
|
|
|
|
FILE *fd;
|
2019-01-09 16:25:31 +01:00
|
|
|
if (nullptr == (fd = fopen(fname, "r")))
|
2019-01-08 17:12:05 +01:00
|
|
|
throw MMException(string("Cannot open file ")+fname+" for reading\n");
|
|
|
|
|
|
|
|
char buffer[1000];
|
|
|
|
// jump over initial comments
|
|
|
|
while (fgets(buffer, 1000, fd) && strncmp(buffer, "%%", 2))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
// read in number of rows and cols
|
|
|
|
if (!fgets(buffer, 1000, fd))
|
|
|
|
throw MMException(string("Cannot read rows and cols while reading ")+fname+"\n");
|
|
|
|
if (2 != sscanf(buffer, "%d %d", &rows, &cols))
|
|
|
|
throw MMException("Couldn't parse rows and cols\n");
|
|
|
|
// read in data
|
2019-01-24 15:22:36 +01:00
|
|
|
data = std::shared_ptr<double>(static_cast<double *>(operator new[](rows*cols*sizeof(double))), [](double *arr) { operator delete[](static_cast<void *>(arr)); });
|
2019-01-08 17:12:05 +01:00
|
|
|
int len = rows*cols;
|
|
|
|
int i = 0;
|
|
|
|
while (fgets(buffer, 1000, fd) && i < len)
|
|
|
|
{
|
Dynare++ / sylvester equation solver: refactor Vector and ConstVector classes
- these classes now encapsulate a std::shared_ptr<{const, }double>, so that
they do not perform memory management, and several {Const,}Vector instances
can transparently share the same underlying data
- make converting constructor from ConstVector to Vector explicit, since that
entails memory allocation (but the reverse conversion is almost costless, so
keep it implicit); do the same for GeneralMatrix/ConstGeneralMatrix,
TwoDMatrix/ConstTwoDMatrix
- remove the constructors that were extracting a row/column from a matrix, and
replace them by getRow() and getCol() methods on {Const,}GeneralMatrix
- rename and change the API of the complex version Vector::add(), so that it is
explicit that it deals with complex numbers
- add constructors that take a MATLAB mxArray
2019-01-22 16:07:44 +01:00
|
|
|
if (1 != sscanf(buffer, "%lf", const_cast<double *>(data.get())+i))
|
2019-01-08 17:12:05 +01:00
|
|
|
throw MMException(string("Couldn't parse float number ")+buffer+"\n");
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (i < len)
|
|
|
|
{
|
|
|
|
char mes[1000];
|
|
|
|
sprintf(mes, "Couldn't read all %d lines, read %d so far\n", len, i);
|
|
|
|
throw MMException(mes);
|
|
|
|
}
|
|
|
|
fclose(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MMMatrixOut::write(const char *fname, int rows, int cols, const double *data)
|
|
|
|
{
|
|
|
|
FILE *fd;
|
2019-01-09 16:25:31 +01:00
|
|
|
if (nullptr == (fd = fopen(fname, "w")))
|
2019-01-08 17:12:05 +01:00
|
|
|
throw MMException(string("Cannot open file ")+fname+" for writing\n");
|
|
|
|
|
|
|
|
if (0 > fprintf(fd, "%%%%MatrixMarket matrix array real general\n"))
|
|
|
|
throw MMException(string("Output error when writing file ")+fname);
|
|
|
|
if (0 > fprintf(fd, "%d %d\n", rows, cols))
|
|
|
|
throw MMException(string("Output error when writing file ")+fname);
|
|
|
|
int running = 0;
|
|
|
|
for (int i = 0; i < cols; i++)
|
|
|
|
{
|
|
|
|
for (int j = 0; j < rows; j++)
|
|
|
|
{
|
|
|
|
if (0 > fprintf(fd, "%40.35g\n", data[running]))
|
|
|
|
throw MMException(string("Output error when writing file ")+fname);
|
|
|
|
running++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MMMatrixOut::write(const char *fname, const GeneralMatrix &m)
|
|
|
|
{
|
|
|
|
write(fname, m.numRows(), m.numCols(), m.base());
|
|
|
|
}
|