added: MatrixOps::saveAsc

save a matrix as a dense asc file for easy import into e.g. octave
note this is only useful for debugging small systems, as the asc
files grow very big and unmanageable
This commit is contained in:
Arne Morten Kvarving 2012-11-20 09:57:17 +01:00
parent e4ad0d62df
commit c02d4ffeb8
2 changed files with 36 additions and 0 deletions

View File

@ -60,6 +60,12 @@ class MatrixOps {
//! \param[in] The matrix to extract the diagonal from
//! \returns M = diag(A)
static Matrix extractDiagonal(const Matrix& A);
//! \brief Save a matrix as a dense asc file
//! \param[in] A The matrix to save
//! \param[in] file File name
//! \details This is only useful for debugging as the files grow very big
static void saveAsc(const Matrix& A, const std::string& file);
};
#include "matrixops_impl.hpp"

View File

@ -158,3 +158,33 @@ Matrix MatrixOps::extractDiagonal(const Matrix& A)
return result;
}
void MatrixOps::saveAsc(const Matrix& A, const std::string& file)
{
std::ofstream f;
f.open(file.c_str());
f << "% " << A.N() << " " << A.M() << std::endl;
int prevrow=-1;
for (Matrix::ConstIterator it = A.begin();
it != A.end(); ++it) {
for (int i=0;i<int(it.index())-prevrow-1;++i) {
for (size_t j=0;j<A.M();++j)
f << "0 ";
f << std::endl;
}
int prevcol=-1;
for (Matrix::ConstColIterator it2 = it->begin();
it2 != it->end();++it2) {
for (int j=0;j<int(it2.index())-prevcol-1;++j)
f << "0 ";
double val = *it2;
f << val << " ";
prevcol = it2.index();
}
for (int j=0;j<int(A.M())-prevcol-1;++j)
f << "0 ";
prevrow = it.index();
f << std::endl;
}
f.close();
}