11#ifndef EIGEN_SPARSE_MARKET_IO_H
12#define EIGEN_SPARSE_MARKET_IO_H
21 template <
typename Scalar,
typename StorageIndex>
22 inline void GetMarketLine (
const char* line, StorageIndex& i, StorageIndex& j, Scalar& value)
24 std::stringstream sline(line);
25 sline >> i >> j >> value;
28 template<>
inline void GetMarketLine (
const char* line,
int& i,
int& j,
float& value)
29 { std::sscanf(line,
"%d %d %g", &i, &j, &value); }
31 template<>
inline void GetMarketLine (
const char* line,
int& i,
int& j,
double& value)
32 { std::sscanf(line,
"%d %d %lg", &i, &j, &value); }
34 template<>
inline void GetMarketLine (
const char* line,
int& i,
int& j, std::complex<float>& value)
35 { std::sscanf(line,
"%d %d %g %g", &i, &j, &numext::real_ref(value), &numext::imag_ref(value)); }
37 template<>
inline void GetMarketLine (
const char* line,
int& i,
int& j, std::complex<double>& value)
38 { std::sscanf(line,
"%d %d %lg %lg", &i, &j, &numext::real_ref(value), &numext::imag_ref(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>
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>
66 inline void putMarketHeader(std::string& header,
int sym)
68 header=
"%%MatrixMarket matrix coordinate ";
69 if(internal::is_same<Scalar, std::complex<float> >::value || internal::is_same<Scalar, std::complex<double> >::value)
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>
85 inline void PutMatrixElt(Scalar value, StorageIndex row, StorageIndex col, std::ofstream& out)
87 out << row <<
" "<< col <<
" " << value <<
"\n";
89 template<
typename Scalar,
typename StorageIndex>
90 inline void PutMatrixElt(std::complex<Scalar> value, StorageIndex row, StorageIndex col, std::ofstream& out)
92 out << row <<
" " << col <<
" " << value.real() <<
" " << value.imag() <<
"\n";
96 template<
typename Scalar>
97 inline void putVectorElt(Scalar value, std::ofstream& out)
101 template<
typename Scalar>
102 inline void putVectorElt(std::complex<Scalar> value, std::ofstream& out)
104 out << value.real() <<
" " << value.imag()<<
"\n";
109inline bool getMarketHeader(
const std::string& filename,
int& sym,
bool& iscomplex,
bool& isvector)
114 std::ifstream in(filename.c_str(),std::ios::in);
120 std::getline(in, line); eigen_assert(in.good());
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;
127 if(substr[4].compare(
"symmetric") == 0) sym =
Symmetric;
128 else if (substr[4].compare(
"Hermitian") == 0) sym =
SelfAdjoint;
133template<
typename SparseMatrixType>
134bool loadMarket(SparseMatrixType& mat,
const std::string& filename)
136 typedef typename SparseMatrixType::Scalar Scalar;
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;
150 typedef Triplet<Scalar,StorageIndex> T;
151 std::vector<T> elements;
153 Index M(-1), N(-1), NNZ(-1);
155 while(input.getline(buffer, maxBuffersize))
164 std::stringstream line(buffer);
165 line >> M >> N >> NNZ;
175 StorageIndex i(-1), j(-1);
177 internal::GetMarketLine(buffer, i, j, value);
181 if(i>=0 && j>=0 && i<M && j<N)
184 elements.push_back(T(i,j,value));
187 std::cerr <<
"Invalid read: " << i <<
"," << j <<
"\n";
191 mat.setFromTriplets(elements.begin(), elements.end());
193 std::cerr << count <<
"!=" << NNZ <<
"\n";
199template<
typename VectorType>
200bool loadMarketVector(VectorType& vec,
const std::string& filename)
202 typedef typename VectorType::Scalar Scalar;
203 std::ifstream in(filename.c_str(), std::ios::in);
211 std::getline(in, line); eigen_assert(in.good());
212 }
while (line[0] ==
'%');
213 std::istringstream newline(line);
215 eigen_assert(n>0 && col>0);
219 while ( std::getline(in, line) && (i < n) ){
220 internal::GetVectorElt(line, value);
225 std::cerr<<
"Unable to read all elements from file " << filename <<
"\n";
231template<
typename SparseMatrixType>
232bool saveMarket(
const SparseMatrixType& mat,
const std::string& filename,
int sym = 0)
234 typedef typename SparseMatrixType::Scalar Scalar;
235 typedef typename SparseMatrixType::RealScalar RealScalar;
236 std::ofstream out(filename.c_str(),std::ios::out);
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)
251 internal::PutMatrixElt(it.value(), it.row()+1, it.col()+1, out);
257template<
typename VectorType>
258bool saveMarketVector (
const VectorType& vec,
const std::string& filename)
260 typedef typename VectorType::Scalar Scalar;
261 typedef typename VectorType::RealScalar RealScalar;
262 std::ofstream out(filename.c_str(),std::ios::out);
266 out.flags(std::ios_base::scientific);
267 out.precision(std::numeric_limits<RealScalar>::digits10 + 2);
268 if(internal::is_same<Scalar, std::complex<float> >::value || internal::is_same<Scalar, std::complex<double> >::value)
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++){
274 internal::putVectorElt(vec(i), out);
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index