11 #ifndef EIGEN_SPARSE_MARKET_IO_H
12 #define EIGEN_SPARSE_MARKET_IO_H
21 template <
typename Scalar,
typename StorageIndex>
24 std::stringstream sline(line);
29 { std::sscanf(line,
"%d %d %g", &
i, &
j, &
value); }
32 { std::sscanf(line,
"%d %d %lg", &
i, &
j, &
value); }
40 template <
typename Scalar,
typename StorageIndex>
41 inline void GetMarketLine (
const char* line, StorageIndex&
i, StorageIndex&
j, std::complex<Scalar>&
value)
43 std::stringstream sline(line);
45 sline >>
i >>
j >> valR >> valI;
46 value = std::complex<Scalar>(valR,valI);
49 template <
typename RealScalar>
52 std::istringstream newline(line);
56 template <
typename RealScalar>
57 inline void GetVectorElt (
const std::string& line, std::complex<RealScalar>& val)
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,
typename StorageIndex>
89 template<
typename Scalar,
typename StorageIndex>
96 template<
typename Scalar>
101 template<
typename Scalar>
114 std::ifstream in(
filename.c_str(),std::ios::in);
122 std::stringstream fmtline(line);
123 std::string substr[5];
124 fmtline>> substr[0] >> substr[1] >> substr[2] >> substr[3] >> substr[4];
125 if(substr[2].
compare(
"array") == 0) isvector =
true;
126 if(substr[3].
compare(
"complex") == 0) iscomplex =
true;
133 template<
typename SparseMatrixType>
137 typedef typename SparseMatrixType::StorageIndex StorageIndex;
138 std::ifstream input(
filename.c_str(),std::ios::in);
143 input.rdbuf()->pubsetbuf(rdbuffer, 4096);
145 const int maxBuffersize = 2048;
146 char buffer[maxBuffersize];
148 bool readsizes =
false;
151 std::vector<T> elements;
155 while(input.getline(
buffer, maxBuffersize))
164 std::stringstream line(
buffer);
165 line >>
M >>
N >> NNZ;
175 StorageIndex
i(-1),
j(-1);
181 if(
i>=0 &&
j>=0 &&
i<
M &&
j<
N)
187 std::cerr <<
"Invalid read: " <<
i <<
"," <<
j <<
"\n";
191 mat.setFromTriplets(elements.begin(), elements.end());
193 std::cerr << count <<
"!=" << NNZ <<
"\n";
199 template<
typename VectorType>
203 std::ifstream in(
filename.c_str(), std::ios::in);
212 }
while (line[0] ==
'%');
213 std::istringstream newline(line);
219 while ( std::getline(in, line) && (
i <
n) ){
225 std::cerr<<
"Unable to read all elements from file " <<
filename <<
"\n";
231 template<
typename SparseMatrixType>
240 out.flags(std::ios_base::scientific);
241 out.precision(std::numeric_limits<RealScalar>::digits10 + 2);
243 internal::putMarketHeader<Scalar>(header, sym);
244 out << header << std::endl;
245 out <<
mat.rows() <<
" " <<
mat.cols() <<
" " <<
mat.nonZeros() <<
"\n";
247 for(
int j=0;
j<
mat.outerSize(); ++
j)
248 for(
typename SparseMatrixType::InnerIterator it(
mat,
j); it; ++it)
257 template<
typename VectorType>
266 out.flags(std::ios_base::scientific);
267 out.precision(std::numeric_limits<RealScalar>::digits10 + 2);
269 out <<
"%%MatrixMarket matrix array complex general\n";
271 out <<
"%%MatrixMarket matrix array real general\n";
272 out << vec.size() <<
" "<< 1 <<
"\n";
273 for (
int i=0;
i < vec.size();
i++){
282 #endif // EIGEN_SPARSE_MARKET_IO_H