11 #ifndef EIGEN_SPARSE_MARKET_IO_H
12 #define EIGEN_SPARSE_MARKET_IO_H
23 template <
typename Scalar,
typename StorageIndex>
25 std::stringstream sline(
line);
49 template <
typename Scalar,
typename StorageIndex>
51 std::stringstream sline(
line);
53 sline >>
i >>
j >> valR >> valI;
54 value = std::complex<Scalar>(valR, valI);
57 template <
typename RealScalar>
59 std::istringstream newline(
line);
63 template <
typename RealScalar>
66 std::istringstream newline(
line);
67 newline >> valR >> valI;
68 val = std::complex<RealScalar>(valR, valI);
71 template <
typename Scalar>
73 header =
"%%MatrixMarket matrix coordinate ";
78 header +=
" symmetric";
80 header +=
" Hermitian";
86 header +=
" symmetric";
92 template <
typename Scalar,
typename StorageIndex>
96 template <
typename Scalar,
typename StorageIndex>
101 template <
typename Scalar>
105 template <
typename Scalar>
126 std::ifstream in(
filename.c_str(), std::ios::in);
127 if (!in)
return false;
131 std::getline(in,
line);
134 std::stringstream fmtline(
line);
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)
155 template <
typename SparseMatrixType>
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;
175 while (input.getline(buffer, maxBuffersize)) {
178 if (buffer[0] ==
'%')
continue;
181 std::stringstream
line(buffer);
183 if (
M > 0 &&
N > 0) {
187 elements.reserve(NNZ);
190 StorageIndex
i(-1),
j(-1);
196 if (
i >= 0 &&
j >= 0 &&
i <
M &&
j <
N) {
200 std::cerr <<
"Invalid read: " <<
i <<
"," <<
j <<
"\n";
208 std::cerr << count <<
"!=" << NNZ <<
"\n";
225 template <
typename DenseType>
228 std::ifstream in(
filename.c_str(), std::ios::in);
229 if (!in)
return false;
234 std::getline(in,
line);
236 }
while (
line[0] ==
'%');
237 std::istringstream newline(
line);
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";
274 std::cerr <<
"Unable to read all elements from file " <<
filename <<
"\n";
283 template <
typename VectorType>
298 template <
typename SparseMatrixType>
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;
314 for (
typename SparseMatrixType::InnerIterator it(
mat,
j); it; ++it) {
332 template <
typename DenseType>
337 if (!
out)
return false;
339 out.flags(std::ios_base::scientific);
340 out.precision(std::numeric_limits<RealScalar>::digits10 + 2);
342 out <<
"%%MatrixMarket matrix array complex general\n";
344 out <<
"%%MatrixMarket matrix array real general\n";
359 template <
typename VectorType>
int i
Definition: BiCGSTAB_step_by_step.cpp:9
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
Eigen::SparseMatrix< double > mat
Definition: EigenUnitTest.cpp:10
Eigen::Triplet< double > T
Definition: EigenUnitTest.cpp:11
#define EIGEN_UNUSED_VARIABLE(var)
Definition: Macros.h:966
#define eigen_assert(x)
Definition: Macros.h:910
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
SCALAR Scalar
Definition: bench_gemm.cpp:45
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:50
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:46
void compare(const Packet &a, const Packet &b)
Definition: blasutil.cpp:24
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
Index size() const
Definition: SparseMatrixBase.h:187
Index nonZeros() const
Definition: SparseCompressedBase.h:64
Index cols() const
Definition: SparseMatrix.h:161
Index outerSize() const
Definition: SparseMatrix.h:166
void resize(Index rows, Index cols)
Definition: SparseMatrix.h:734
Index rows() const
Definition: SparseMatrix.h:159
void setFromTriplets(const InputIterators &begin, const InputIterators &end)
Definition: SparseMatrix.h:1328
void reserve(Index reserveSize)
Definition: SparseMatrix.h:315
A small structure to hold a non zero as a triplet (i,j,value).
Definition: SparseUtil.h:187
@ N
Definition: constructor.cpp:22
@ SelfAdjoint
Definition: Constants.h:227
@ Symmetric
Definition: Constants.h:229
void putMarketHeader(std::string &header, int sym)
Definition: MarketIO.h:72
void PutMatrixElt(Scalar value, StorageIndex row, StorageIndex col, std::ofstream &out)
Definition: MarketIO.h:93
void GetDenseElt(const std::string &line, RealScalar &val)
Definition: MarketIO.h:58
void GetMarketLine(const char *line, StorageIndex &i, StorageIndex &j, Scalar &value)
Definition: MarketIO.h:24
void putDenseElt(Scalar value, std::ofstream &out)
Definition: MarketIO.h:102
EIGEN_DEVICE_FUNC internal::add_const_on_value_type_t< EIGEN_MATHFUNC_RETVAL(imag_ref, Scalar)> imag_ref(const Scalar &x)
Definition: MathFunctions.h:1072
EIGEN_DEVICE_FUNC internal::add_const_on_value_type_t< EIGEN_MATHFUNC_RETVAL(real_ref, Scalar)> real_ref(const Scalar &x)
Definition: MathFunctions.h:1051
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
bool loadMarketVector(VectorType &vec, const std::string &filename)
Same functionality as loadMarketDense, deprecated.
Definition: MarketIO.h:284
squared absolute value
Definition: GlobalFunctions.h:87
bool saveMarket(const SparseMatrixType &mat, const std::string &filename, int sym=0)
writes a sparse Matrix to a marketmarket format file
Definition: MarketIO.h:299
bool loadMarket(SparseMatrixType &mat, const std::string &filename)
Loads a sparse matrix from a matrixmarket format file.
Definition: MarketIO.h:156
bool saveMarketVector(const VectorType &vec, const std::string &filename)
Same functionality as saveMarketDense, deprecated.
Definition: MarketIO.h:360
bool getMarketHeader(const std::string &filename, int &sym, bool &iscomplex, bool &isdense)
Reads the header of a matrixmarket file and determines the properties of a matrix.
Definition: MarketIO.h:122
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
bool loadMarketDense(DenseType &mat, const std::string &filename)
Loads a dense Matrix or Vector from a matrixmarket file. If a statically sized matrix has to be parse...
Definition: MarketIO.h:226
bool saveMarketDense(const DenseType &mat, const std::string &filename)
writes a dense Matrix or vector to a marketmarket format file
Definition: MarketIO.h:333
const int Dynamic
Definition: Constants.h:25
string filename
Definition: MergeRestartFiles.py:39
val
Definition: calibrate.py:119
line
Definition: calibrate.py:103
Definition: Eigen_Colamd.h:49
std::string string(const unsigned &i)
Definition: oomph_definitions.cc:286
Definition: fft_test_shared.h:66
std::ofstream out("Result.txt")
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2