11#ifndef EIGEN_SPARSE_MARKET_IO_H
12#define EIGEN_SPARSE_MARKET_IO_H
18#include "./InternalHeaderCheck.h"
23template <
typename Scalar,
typename StorageIndex>
24inline void GetMarketLine(
const char* line, StorageIndex& i, StorageIndex& j, Scalar& value) {
25 std::stringstream sline(line);
26 sline >> i >> j >> value;
30inline void GetMarketLine(
const char* line,
int& i,
int& j,
float& value) {
31 std::sscanf(line,
"%d %d %g", &i, &j, &value);
35inline void GetMarketLine(
const char* line,
int& i,
int& j,
double& value) {
36 std::sscanf(line,
"%d %d %lg", &i, &j, &value);
40inline void GetMarketLine(
const char* line,
int& i,
int& j, std::complex<float>& value) {
41 std::sscanf(line,
"%d %d %g %g", &i, &j, &numext::real_ref(value), &numext::imag_ref(value));
45inline void GetMarketLine(
const char* line,
int& i,
int& j, std::complex<double>& value) {
46 std::sscanf(line,
"%d %d %lg %lg", &i, &j, &numext::real_ref(value), &numext::imag_ref(value));
49template <
typename Scalar,
typename StorageIndex>
50inline void GetMarketLine(
const char* line, StorageIndex& i, StorageIndex& j, std::complex<Scalar>& value) {
51 std::stringstream sline(line);
53 sline >> i >> j >> valR >> valI;
54 value = std::complex<Scalar>(valR, valI);
57template <
typename RealScalar>
58inline void GetDenseElt(
const std::string& line, RealScalar& val) {
59 std::istringstream newline(line);
63template <
typename RealScalar>
64inline void GetDenseElt(
const std::string& line, std::complex<RealScalar>& val) {
65 RealScalar valR, valI;
66 std::istringstream newline(line);
67 newline >> valR >> valI;
68 val = std::complex<RealScalar>(valR, valI);
71template <
typename Scalar>
72inline void putMarketHeader(std::string& header,
int sym) {
73 header =
"%%MatrixMarket matrix coordinate ";
74 if (internal::is_same<Scalar, std::complex<float> >::value ||
75 internal::is_same<Scalar, std::complex<double> >::value) {
78 header +=
" symmetric";
80 header +=
" Hermitian";
86 header +=
" symmetric";
92template <
typename Scalar,
typename StorageIndex>
93inline void PutMatrixElt(Scalar value, StorageIndex row, StorageIndex col, std::ofstream& out) {
94 out << row <<
" " << col <<
" " << value <<
"\n";
96template <
typename Scalar,
typename StorageIndex>
97inline void PutMatrixElt(std::complex<Scalar> value, StorageIndex row, StorageIndex col, std::ofstream& out) {
98 out << row <<
" " << col <<
" " << value.real() <<
" " << value.imag() <<
"\n";
101template <
typename Scalar>
102inline void putDenseElt(Scalar value, std::ofstream& out) {
103 out << value <<
"\n";
105template <
typename Scalar>
106inline void putDenseElt(std::complex<Scalar> value, std::ofstream& out) {
107 out << value.real() <<
" " << value.imag() <<
"\n";
122inline bool getMarketHeader(
const std::string& filename,
int& sym,
bool& iscomplex,
bool& isdense) {
126 std::ifstream in(filename.c_str(), std::ios::in);
127 if (!in)
return false;
131 std::getline(in, line);
132 eigen_assert(in.good());
134 std::stringstream fmtline(line);
135 std::string substr[5];
136 fmtline >> substr[0] >> substr[1] >> substr[2] >> substr[3] >> substr[4];
137 if (substr[2].compare(
"array") == 0) isdense =
true;
138 if (substr[3].compare(
"complex") == 0) iscomplex =
true;
139 if (substr[4].compare(
"symmetric") == 0)
141 else if (substr[4].compare(
"Hermitian") == 0)
155template <
typename SparseMatrixType>
156bool loadMarket(SparseMatrixType& mat,
const std::string& filename) {
157 typedef typename SparseMatrixType::Scalar
Scalar;
158 typedef typename SparseMatrixType::StorageIndex StorageIndex;
159 std::ifstream input(filename.c_str(), std::ios::in);
160 if (!input)
return false;
163 input.rdbuf()->pubsetbuf(rdbuffer, 4096);
165 const int maxBuffersize = 2048;
166 char buffer[maxBuffersize];
168 bool readsizes =
false;
171 std::vector<T> elements;
173 Index M(-1), N(-1), NNZ(-1);
175 while (input.getline(buffer, maxBuffersize)) {
178 if (buffer[0] ==
'%')
continue;
181 std::stringstream line(buffer);
182 line >> M >> N >> NNZ;
183 if (M > 0 && N > 0) {
187 elements.reserve(NNZ);
190 StorageIndex i(-1), j(-1);
192 internal::GetMarketLine(buffer, i, j, value);
196 if (i >= 0 && j >= 0 && i < M && j < N) {
198 elements.push_back(T(i, j, value));
200 std::cerr <<
"Invalid read: " << i <<
"," << j <<
"\n";
206 mat.setFromTriplets(elements.begin(), elements.end());
208 std::cerr << count <<
"!=" << NNZ <<
"\n";
225template <
typename DenseType>
227 typedef typename DenseType::Scalar
Scalar;
228 std::ifstream in(filename.c_str(), std::ios::in);
229 if (!in)
return false;
232 Index rows(0), cols(0);
234 std::getline(in, line);
235 eigen_assert(in.good());
236 }
while (line[0] ==
'%');
237 std::istringstream newline(line);
238 newline >> rows >> cols;
240 bool sizes_not_positive = (rows < 1 || cols < 1);
241 bool wrong_input_rows = (DenseType::MaxRowsAtCompileTime !=
Dynamic && rows > DenseType::MaxRowsAtCompileTime) ||
242 (DenseType::RowsAtCompileTime !=
Dynamic && rows != DenseType::RowsAtCompileTime);
243 bool wrong_input_cols = (DenseType::MaxColsAtCompileTime !=
Dynamic && cols > DenseType::MaxColsAtCompileTime) ||
244 (DenseType::ColsAtCompileTime !=
Dynamic && cols != DenseType::ColsAtCompileTime);
246 if (sizes_not_positive || wrong_input_rows || wrong_input_cols) {
247 if (sizes_not_positive) {
248 std::cerr <<
"non-positive row or column size in file" << filename <<
"\n";
250 std::cerr <<
"Input matrix can not be resized to" << rows <<
" x " << cols <<
"as given in " << filename <<
"\n";
256 mat.resize(rows, cols);
261 while (std::getline(in, line) && (row < rows) && (col < cols)) {
262 internal::GetDenseElt(line, value);
264 mat(row, col) = value;
273 if (n != mat.size()) {
274 std::cerr <<
"Unable to read all elements from file " << filename <<
"\n";
283template <
typename VectorType>
298template <
typename SparseMatrixType>
299bool saveMarket(
const SparseMatrixType& mat,
const std::string& filename,
int sym = 0) {
300 typedef typename SparseMatrixType::Scalar
Scalar;
301 typedef typename SparseMatrixType::RealScalar RealScalar;
302 std::ofstream out(filename.c_str(), std::ios::out);
303 if (!out)
return false;
305 out.flags(std::ios_base::scientific);
306 out.precision(std::numeric_limits<RealScalar>::digits10 + 2);
308 internal::putMarketHeader<Scalar>(header, sym);
309 out << header << std::endl;
310 out << mat.rows() <<
" " << mat.cols() <<
" " << mat.nonZeros() <<
"\n";
312 EIGEN_UNUSED_VARIABLE(count);
313 for (
int j = 0; j < mat.outerSize(); ++j)
314 for (
typename SparseMatrixType::InnerIterator it(mat, j); it; ++it) {
316 internal::PutMatrixElt(it.value(), it.row() + 1, it.col() + 1, out);
332template <
typename DenseType>
334 typedef typename DenseType::Scalar
Scalar;
335 typedef typename DenseType::RealScalar RealScalar;
336 std::ofstream out(filename.c_str(), std::ios::out);
337 if (!out)
return false;
339 out.flags(std::ios_base::scientific);
340 out.precision(std::numeric_limits<RealScalar>::digits10 + 2);
341 if (internal::is_same<
Scalar, std::complex<float> >::value || internal::is_same<
Scalar, std::complex<double> >::value)
342 out <<
"%%MatrixMarket matrix array complex general\n";
344 out <<
"%%MatrixMarket matrix array real general\n";
345 out << mat.rows() <<
" " << mat.cols() <<
"\n";
346 for (
Index i = 0; i < mat.cols(); i++) {
347 for (
Index j = 0; j < mat.rows(); j++) {
348 internal::putDenseElt(mat(j, i), out);
359template <
typename VectorType>
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index