2019-01-08 16:09:25 +01:00
|
|
|
// Copyright 2004, Ondra Kamenik
|
|
|
|
|
|
|
|
#include "twod_matrix.hh"
|
|
|
|
#include "tl_exception.hh"
|
|
|
|
|
|
|
|
ConstTwoDMatrix::ConstTwoDMatrix(const TwoDMatrix &m)
|
|
|
|
: ConstGeneralMatrix(m)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstTwoDMatrix::ConstTwoDMatrix(const TwoDMatrix &m, int first_col, int num)
|
|
|
|
: ConstGeneralMatrix(m, 0, first_col, m.nrows(), num)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstTwoDMatrix::ConstTwoDMatrix(const ConstTwoDMatrix &m, int first_col, int num)
|
|
|
|
: ConstGeneralMatrix(m, 0, first_col, m.nrows(), num)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstTwoDMatrix::ConstTwoDMatrix(int first_row, int num, const TwoDMatrix &m)
|
|
|
|
: ConstGeneralMatrix(m, first_row, 0, num, m.ncols())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstTwoDMatrix::ConstTwoDMatrix(int first_row, int num, const ConstTwoDMatrix &m)
|
|
|
|
: ConstGeneralMatrix(m, first_row, 0, num, m.ncols())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ConstTwoDMatrix::writeMat(mat_t *fd, const char *vname) const
|
|
|
|
{
|
|
|
|
size_t dims[2];
|
|
|
|
dims[0] = nrows();
|
|
|
|
dims[1] = ncols();
|
2019-01-09 15:51:19 +01:00
|
|
|
auto *data = new double[nrows()*ncols()];
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
|
|
for (int j = 0; j < ncols(); j++)
|
|
|
|
for (int i = 0; i < nrows(); i++)
|
|
|
|
data[j*nrows()+i] = get(i, j);
|
|
|
|
|
|
|
|
matvar_t *v = Mat_VarCreate(vname, MAT_C_DOUBLE, MAT_T_DOUBLE, 2, dims, data, 0);
|
|
|
|
|
2019-02-12 15:58:29 +01:00
|
|
|
Mat_VarWrite(fd, v, MAT_COMPRESSION_NONE);
|
2019-01-08 16:09:25 +01:00
|
|
|
|
|
|
|
Mat_VarFree(v);
|
|
|
|
delete[] data;
|
|
|
|
}
|
|
|
|
|
2019-01-24 15:22:36 +01:00
|
|
|
TwoDMatrix &
|
|
|
|
TwoDMatrix::operator=(const ConstTwoDMatrix &m)
|
|
|
|
{
|
|
|
|
GeneralMatrix::operator=(m);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-01-08 16:09:25 +01:00
|
|
|
void
|
|
|
|
TwoDMatrix::copyRow(int from, int to)
|
|
|
|
{
|
|
|
|
if (from != to)
|
|
|
|
copyRow(ConstTwoDMatrix(*this), from, to);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TwoDMatrix::copyRow(const ConstTwoDMatrix &m, int from, int to)
|
|
|
|
{
|
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
|
|
|
getRow(to) = m.getRow(from);
|
2019-01-08 16:09:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TwoDMatrix::addRow(double d, const ConstTwoDMatrix &m, int from, int to)
|
|
|
|
{
|
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
|
|
|
getRow(to).add(d, m.getRow(from));
|
2019-01-08 16:09:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TwoDMatrix::copyColumn(int from, int to)
|
|
|
|
{
|
|
|
|
if (from != to)
|
|
|
|
copyColumn(ConstTwoDMatrix(*this), from, to);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TwoDMatrix::copyColumn(const ConstTwoDMatrix &m, int from, int to)
|
|
|
|
{
|
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
|
|
|
getCol(to) = m.getCol(from);
|
2019-01-08 16:09:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TwoDMatrix::addColumn(double d, const ConstTwoDMatrix &m, int from, int to)
|
|
|
|
{
|
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
|
|
|
getCol(to).add(d, m.getCol(from));
|
2019-01-08 16:09:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TwoDMatrix::save(const char *fname) const
|
|
|
|
{
|
|
|
|
FILE *fd;
|
2019-01-09 16:25:31 +01:00
|
|
|
if (nullptr == (fd = fopen(fname, "w")))
|
2019-01-08 16:09:25 +01:00
|
|
|
{
|
|
|
|
TL_RAISE("Cannot open file for writing in TwoDMatrix::save");
|
|
|
|
}
|
|
|
|
for (int row = 0; row < nrows(); row++)
|
|
|
|
{
|
|
|
|
for (int col = 0; col < ncols(); col++)
|
|
|
|
fprintf(fd, " %20.10g", get(row, col));
|
|
|
|
fprintf(fd, "\n");
|
|
|
|
}
|
|
|
|
fclose(fd);
|
|
|
|
}
|