11 #ifndef EIGEN_SPARSE_MARKET_IO_H 12 #define EIGEN_SPARSE_MARKET_IO_H 20 template <
typename Scalar>
23 line >> i >> j >> value;
26 if(i>=0 && j>=0 && i<M && j<N)
33 template <
typename Scalar>
37 line >> i >> j >> valR >> valI;
40 if(i>=0 && j>=0 && i<M && j<N)
42 value = std::complex<Scalar>(valR, valI);
49 template <
typename RealScalar>
50 inline void GetVectorElt (
const std::string& line, RealScalar& val)
52 std::istringstream newline(line);
56 template <
typename RealScalar>
57 inline void GetVectorElt (
const std::string& line, std::complex<RealScalar>& val)
59 RealScalar valR, valI;
60 std::istringstream newline(line);
61 newline >> valR >> valI;
62 val = std::complex<RealScalar>(valR, valI);
65 template<
typename Scalar>
68 header=
"%%MatrixMarket matrix coordinate ";
72 if(sym ==
Symmetric) header +=
" symmetric";
73 else if (sym ==
SelfAdjoint) header +=
" Hermitian";
74 else header +=
" general";
79 if(sym ==
Symmetric) header +=
" symmetric";
80 else header +=
" general";
84 template<
typename Scalar>
87 out << row <<
" "<< col <<
" " << value <<
"\n";
89 template<
typename Scalar>
92 out << row <<
" " << col <<
" " << value.real() <<
" " << value.imag() <<
"\n";
96 template<
typename Scalar>
101 template<
typename Scalar>
102 inline void putVectorElt(std::complex<Scalar> value, std::ofstream& out)
104 out << value.real <<
" " << value.imag()<<
"\n";
109 inline bool getMarketHeader(
const std::string& filename,
int& sym,
bool& iscomplex,
bool& isvector)
113 std::ifstream in(filename.c_str(),std::ios::in);
121 std::stringstream fmtline(line);
122 std::string substr[5];
123 fmtline>> substr[0] >> substr[1] >> substr[2] >> substr[3] >> substr[4];
124 if(substr[2].compare(
"array") == 0) isvector =
true;
125 if(substr[3].compare(
"complex") == 0) iscomplex =
true;
126 if(substr[4].compare(
"symmetric") == 0) sym =
Symmetric;
127 else if (substr[4].compare(
"Hermitian") == 0) sym =
SelfAdjoint;
132 template<
typename SparseMatrixType>
133 bool loadMarket(SparseMatrixType& mat,
const std::string& filename)
135 typedef typename SparseMatrixType::Scalar Scalar;
137 std::ifstream input(filename.c_str(),std::ios::in);
141 const int maxBuffersize = 2048;
142 char buffer[maxBuffersize];
144 bool readsizes =
false;
147 std::vector<T> elements;
149 Index M(-1),
N(-1), NNZ(-1);
151 while(input.getline(buffer, maxBuffersize))
158 std::stringstream line(buffer);
162 line >> M >>
N >> NNZ;
163 if(M > 0 &&
N > 0 && NNZ > 0)
178 elements.push_back(T(i,j,value));
181 std::cerr <<
"Invalid read: " << i <<
"," << j <<
"\n";
184 mat.setFromTriplets(elements.begin(), elements.end());
186 std::cerr << count <<
"!=" << NNZ <<
"\n";
192 template<
typename VectorType>
195 typedef typename VectorType::Scalar Scalar;
196 std::ifstream in(filename.c_str(), std::ios::in);
205 }
while (line[0] ==
'%');
206 std::istringstream newline(line);
212 while ( std::getline(in, line) && (i < n) ){
218 std::cerr<<
"Unable to read all elements from file " << filename <<
"\n";
224 template<
typename SparseMatrixType>
225 bool saveMarket(
const SparseMatrixType& mat,
const std::string& filename,
int sym = 0)
227 typedef typename SparseMatrixType::Scalar Scalar;
228 std::ofstream out(filename.c_str(),std::ios::out);
232 out.flags(std::ios_base::scientific);
235 internal::putMarketHeader<Scalar>(header, sym);
236 out << header << std::endl;
237 out << mat.rows() <<
" " << mat.cols() <<
" " << mat.nonZeros() <<
"\n";
239 for(
int j=0; j<mat.outerSize(); ++j)
240 for(
typename SparseMatrixType::InnerIterator it(mat,j); it; ++it)
250 template<
typename VectorType>
253 typedef typename VectorType::Scalar Scalar;
254 std::ofstream out(filename.c_str(),std::ios::out);
258 out.flags(std::ios_base::scientific);
261 out <<
"%%MatrixMarket matrix array complex general\n";
263 out <<
"%%MatrixMarket matrix array real general\n";
264 out << vec.size() <<
" "<< 1 <<
"\n";
265 for (
int i=0; i < vec.size(); i++){
274 #endif // EIGEN_SPARSE_MARKET_IO_H void putMarketHeader(std::string &header, int sym)
bool loadMarketVector(VectorType &vec, const std::string &filename)
void putVectorElt(Scalar value, std::ofstream &out)
bool getMarketHeader(const std::string &filename, int &sym, bool &iscomplex, bool &isvector)
void PutMatrixElt(Scalar value, int row, int col, std::ofstream &out)
bool saveMarketVector(const VectorType &vec, const std::string &filename)
EIGEN_DEVICE_FUNC ColXpr col(Index i)
This is the const version of col().
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
EIGEN_DEVICE_FUNC RowXpr row(Index i)
This is the const version of row(). */.
A small structure to hold a non zero as a triplet (i,j,value).
bool loadMarket(SparseMatrixType &mat, const std::string &filename)
void GetVectorElt(const std::string &line, RealScalar &val)
bool saveMarket(const SparseMatrixType &mat, const std::string &filename, int sym=0)
bool GetMarketLine(std::stringstream &line, Index &M, Index &N, Index &i, Index &j, Scalar &value)