Skip to content
Snippets Groups Projects
Commit 73f01f11 authored by Jesper Andersson's avatar Jesper Andersson
Browse files

Added .Print() function that outputs matrix in a row col val format

parent 84128da8
No related branches found
No related tags found
No related merge requests found
......@@ -21,6 +21,8 @@
#define SpMat_h
#include <vector>
#include <fstream>
#include <iomanip>
#include <boost/shared_ptr.hpp>
#include "newmat.h"
#include "cg.h"
......@@ -97,6 +99,12 @@ public:
unsigned int NZ() const {return(_nz);}
NEWMAT::ReturnMatrix AsNEWMAT() const;
void Print(const std::string& fname,
unsigned int precision) const;
void Print(const std::string& fname) const {Print(fname,8);}
void Print(unsigned int precision) const {Print(std::string(""),precision);}
void Print() const {Print(8);}
T Peek(unsigned int r, unsigned int c) const;
T operator()(unsigned int r, unsigned int c) const {return(Peek(r,c));} // Read-only
......@@ -373,6 +381,43 @@ NEWMAT::ReturnMatrix SpMat<T>::AsNEWMAT() const
return(M);
}
/////////////////////////////////////////////////////////////////////
//
// Prints matrix in a row col val format that is useful for
// exporting it to Matlab (use Matlab function spconvert).
//
/////////////////////////////////////////////////////////////////////
template<class T>
void SpMat<T>::Print(const std::string& fname,
unsigned int precision) const
{
ostream *sptr=0;
if (!fname.length()) {
sptr = &cout;
}
else {
try {
sptr = new ofstream(fname.c_str());
}
catch(...) {
std::string errmsg("BFMatrix::print: Failed to write to file " + fname);
throw SpMatException(errmsg);
}
}
(*sptr) << setprecision(precision);
for (unsigned int c=0; c<_n; c++) {
for (unsigned int i=0; i<_ri[c].size(); i++) {
if (_val[c][i]) (*sptr) << _ri[c][i]+1 << " " << c+1 << " " << _val[c][i] << endl;
}
}
(*sptr) << _m << " " << _n << " " << 0 << endl;
if (fname.length()) delete sptr;
}
/////////////////////////////////////////////////////////////////////
//
// Solves for x in expression b=(*this)*x. Uses the IML++ templates
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment