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 typedef Triplet<Scalar,int> T;
161 std::vector<T> elements;
163 int M(-1), N(-1), NNZ(-1);
165 while(input.getline(buffer, maxBuffersize))
172 std::stringstream line(buffer);
176 line >> M >> N >> NNZ;
177 if(M > 0 && N > 0 && NNZ > 0)
180 std::cout <<
"sizes: " << M <<
"," << N <<
"," << NNZ <<
"\n";
189 if( internal::GetMarketLine(line, M, N, i, j, value) )
192 elements.push_back(T(i,j,value));
195 std::cerr <<
"Invalid read: " << i <<
"," << j <<
"\n";
198 mat.setFromTriplets(elements.begin(), elements.end());
200 std::cerr << count <<
"!=" << NNZ <<
"\n";
206 template<
typename VectorType>
207 bool loadMarketVector(VectorType& vec,
const std::string& filename)
209 typedef typename VectorType::Scalar Scalar;
210 std::ifstream in(filename.c_str(), std::ios::in);
218 std::getline(in, line); assert(in.good());
219 }
while (line[0] ==
'%');
220 std::istringstream newline(line);
222 assert(n>0 && col>0);
226 while ( std::getline(in, line) && (i < n) ){
227 internal::GetVectorElt(line, value);
232 std::cerr<<
"Unable to read all elements from file " << filename <<
"\n";
238 template<
typename SparseMatrixType>
239 bool saveMarket(
const SparseMatrixType& mat,
const std::string& filename,
int sym = 0)
241 typedef typename SparseMatrixType::Scalar Scalar;
242 std::ofstream out(filename.c_str(),std::ios::out);
246 out.flags(std::ios_base::scientific);
249 internal::putMarketHeader<Scalar>(header, sym);
250 out << header << std::endl;
251 out << mat.rows() <<
" " << mat.cols() <<
" " << mat.nonZeros() <<
"\n";
253 for(
int j=0; j<mat.outerSize(); ++j)
254 for(
typename SparseMatrixType::InnerIterator it(mat,j); it; ++it)
257 internal::PutMatrixElt(it.value(), it.row()+1, it.col()+1, out);
264 template<
typename VectorType>
265 bool saveMarketVector (
const VectorType& vec,
const std::string& filename)
267 typedef typename VectorType::Scalar Scalar;
268 std::ofstream out(filename.c_str(),std::ios::out);
272 out.flags(std::ios_base::scientific);
274 if(internal::is_same<Scalar, std::complex<float> >::value || internal::is_same<Scalar, std::complex<double> >::value)
275 out <<
"%%MatrixMarket matrix array complex general\n";
277 out <<
"%%MatrixMarket matrix array real general\n";
278 out << vec.size() <<
" "<< 1 <<
"\n";
279 for (
int i=0; i < vec.size(); i++){
280 internal::putVectorElt(vec(i), out);
288 #endif // EIGEN_SPARSE_MARKET_IO_H