26 #ifndef EIGEN_SPARSE_MARKET_IO_H
27 #define EIGEN_SPARSE_MARKET_IO_H
35 template <
typename Scalar>
36 inline bool GetMarketLine (std::stringstream& line,
int& M,
int& N,
int& i,
int& j, Scalar& value)
38 line >> i >> j >> value;
41 if(i>=0 && j>=0 && i<M && j<N)
48 template <
typename Scalar>
49 inline bool GetMarketLine (std::stringstream& line,
int& M,
int& N,
int& i,
int& j, std::complex<Scalar>& value)
52 line >> i >> j >> valR >> valI;
55 if(i>=0 && j>=0 && i<M && j<N)
57 value = std::complex<Scalar>(valR, valI);
64 template <
typename RealScalar>
65 inline void GetVectorElt (
const std::string& line, RealScalar& val)
67 std::istringstream newline(line);
71 template <
typename RealScalar>
72 inline void GetVectorElt (
const std::string& line, std::complex<RealScalar>& val)
74 RealScalar valR, valI;
75 std::istringstream newline(line);
76 newline >> valR >> valI;
77 val = std::complex<RealScalar>(valR, valI);
80 template<
typename Scalar>
81 inline void putMarketHeader(std::string& header,
int sym)
83 header=
"%%MatrixMarket matrix coordinate ";
84 if(internal::is_same<Scalar, std::complex<float> >::value || internal::is_same<Scalar, std::complex<double> >::value)
87 if(sym == Symmetric) header +=
" symmetric";
88 else if (sym == SelfAdjoint) header +=
" Hermitian";
89 else header +=
" general";
94 if(sym == Symmetric) header +=
" symmetric";
95 else header +=
" general";
99 template<
typename Scalar>
100 inline void PutMatrixElt(Scalar value,
int row,
int col, std::ofstream& out)
102 out << row <<
" "<< col <<
" " << value <<
"\n";
104 template<
typename Scalar>
105 inline void PutMatrixElt(std::complex<Scalar> value,
int row,
int col, std::ofstream& out)
107 out << row <<
" " << col <<
" " << value.real() <<
" " << value.imag() <<
"\n";
111 template<
typename Scalar>
112 inline void putVectorElt(Scalar value, std::ofstream& out)
114 out << value <<
"\n";
116 template<
typename Scalar>
117 inline void putVectorElt(std::complex<Scalar> value, std::ofstream& out)
119 out << value.real <<
" " << value.imag()<<
"\n";
124 inline bool getMarketHeader(
const std::string& filename,
int& sym,
bool& iscomplex,
bool& isvector)
128 std::ifstream in(filename.c_str(),std::ios::in);
134 std::getline(in, line); assert(in.good());
136 std::stringstream fmtline(line);
137 std::string substr[5];
138 fmtline>> substr[0] >> substr[1] >> substr[2] >> substr[3] >> substr[4];
139 if(substr[2].compare(
"array") == 0) isvector =
true;
140 if(substr[3].compare(
"complex") == 0) iscomplex =
true;
141 if(substr[4].compare(
"symmetric") == 0) sym =
Symmetric;
142 else if (substr[4].compare(
"Hermitian") == 0) sym =
SelfAdjoint;
147 template<
typename SparseMatrixType>
148 bool loadMarket(SparseMatrixType& mat,
const std::string& filename)
150 typedef typename SparseMatrixType::Scalar Scalar;
151 std::ifstream input(filename.c_str(),std::ios::in);
155 const int maxBuffersize = 2048;
156 char buffer[maxBuffersize];
158 bool readsizes =
false;
160 int M(-1), N(-1), NNZ(-1);
162 while(input.getline(buffer, maxBuffersize))
169 std::stringstream line(buffer);
173 line >> M >> N >> NNZ;
174 if(M > 0 && N > 0 && NNZ > 0)
177 std::cout <<
"sizes: " << M <<
"," << N <<
"," << NNZ <<
"\n";
186 if( internal::GetMarketLine(line, M, N, i, j, value) )
189 mat.insert(i,j) = value;
192 std::cerr <<
"Invalid read: " << i <<
"," << j <<
"\n";
195 mat.makeCompressed();
197 std::cerr << count <<
"!=" << NNZ <<
"\n";
203 template<
typename VectorType>
204 bool loadMarketVector(VectorType& vec,
const std::string& filename)
206 typedef typename VectorType::Scalar Scalar;
207 std::ifstream in(filename.c_str(), std::ios::in);
215 std::getline(in, line); assert(in.good());
216 }
while (line[0] ==
'%');
217 std::istringstream newline(line);
219 assert(n>0 && col>0);
223 while ( std::getline(in, line) && (i < n) ){
224 internal::GetVectorElt(line, value);
229 std::cerr<<
"Unable to read all elements from file " << filename <<
"\n";
235 template<
typename SparseMatrixType>
236 bool saveMarket(
const SparseMatrixType& mat,
const std::string& filename,
int sym = 0)
238 typedef typename SparseMatrixType::Scalar Scalar;
239 std::ofstream out(filename.c_str(),std::ios::out);
243 out.flags(std::ios_base::scientific);
246 internal::putMarketHeader<Scalar>(header, sym);
247 out << header << std::endl;
248 out << mat.rows() <<
" " << mat.cols() <<
" " << mat.nonZeros() <<
"\n";
250 for(
int j=0; j<mat.outerSize(); ++j)
251 for(
typename SparseMatrixType::InnerIterator it(mat,j); it; ++it)
254 internal::PutMatrixElt(it.value(), it.row()+1, it.col()+1, out);
261 template<
typename VectorType>
262 bool saveMarketVector (
const VectorType& vec,
const std::string& filename)
264 typedef typename VectorType::Scalar Scalar;
265 std::ofstream out(filename.c_str(),std::ios::out);
269 out.flags(std::ios_base::scientific);
271 if(internal::is_same<Scalar, std::complex<float> >::value || internal::is_same<Scalar, std::complex<double> >::value)
272 out <<
"%%MatrixMarket matrix array complex general\n";
274 out <<
"%%MatrixMarket matrix array real general\n";
275 out << vec.size() <<
" "<< 1 <<
"\n";
276 for (
int i=0; i < vec.size(); i++){
277 internal::putVectorElt(vec(i), out);
285 #endif // EIGEN_SPARSE_MARKET_IO_H