11#ifndef EIGEN_SPARSE_MARKET_IO_H
12#define EIGEN_SPARSE_MARKET_IO_H
21 template <
typename Scalar,
typename StorageIndex>
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)
46 value = std::complex<Scalar>(
valR,
valI);
49 template <
typename RealScalar>
56 template <
typename RealScalar>
62 val = std::complex<RealScalar>(
valR,
valI);
65 template<
typename Scalar>
68 header=
"%%MatrixMarket matrix coordinate ";
84 template<
typename Scalar,
typename StorageIndex>
87 out <<
row <<
" "<<
col <<
" " << value <<
"\n";
89 template<
typename Scalar,
typename StorageIndex>
92 out <<
row <<
" " <<
col <<
" " << value.real() <<
" " << value.imag() <<
"\n";
96 template<
typename Scalar>
101 template<
typename Scalar>
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);
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;
133template<
typename SparseMatrixType>
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;
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)
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>
202 typedef typename VectorType::Scalar
Scalar;
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";
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);
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)
257template<
typename VectorType>
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);
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++){
int n
Definition BiCGSTAB_simple.cpp:1
int i
Definition BiCGSTAB_step_by_step.cpp:9
#define eigen_assert(x)
Definition Macros.h:1037
MatrixXf mat
Definition Tutorial_AdvancedInitialization_CommaTemporary.cpp:1
Eigen::Triplet< double > T
Definition Tutorial_sparse_example.cpp:6
SCALAR Scalar
Definition bench_gemm.cpp:46
Matrix< RealScalar, Dynamic, Dynamic > M
Definition bench_gemm.cpp:51
NumTraits< Scalar >::Real RealScalar
Definition bench_gemm.cpp:47
void compare(const Packet &a, const Packet &b)
Definition blasutil.cpp:24
subroutine header
Definition cblat1.f:91
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:180
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void resize(Index rows, Index cols)
Definition PlainObjectBase.h:271
A small structure to hold a non zero as a triplet (i,j,value).
Definition SparseUtil.h:163
@ N
Definition constructor.cpp:23
@ SelfAdjoint
Definition Constants.h:225
@ Symmetric
Definition Constants.h:227
void putMarketHeader(std::string &header, int sym)
Definition MarketIO.h:66
void PutMatrixElt(Scalar value, StorageIndex row, StorageIndex col, std::ofstream &out)
Definition MarketIO.h:85
void GetMarketLine(const char *line, StorageIndex &i, StorageIndex &j, Scalar &value)
Definition MarketIO.h:22
void GetVectorElt(const std::string &line, RealScalar &val)
Definition MarketIO.h:50
void putVectorElt(Scalar value, std::ofstream &out)
Definition MarketIO.h:97
EIGEN_DEVICE_FUNC internal::add_const_on_value_type< EIGEN_MATHFUNC_RETVAL(imag_ref, Scalar)>::type imag_ref(const Scalar &x)
Definition MathFunctions.h:1265
EIGEN_DEVICE_FUNC internal::add_const_on_value_type< EIGEN_MATHFUNC_RETVAL(real_ref, Scalar)>::type real_ref(const Scalar &x)
Definition MathFunctions.h:1237
Namespace containing all symbols from the Eigen library.
Definition bench_norm.cpp:85
bool getMarketHeader(const std::string &filename, int &sym, bool &iscomplex, bool &isvector)
Definition MarketIO.h:109
bool loadMarketVector(VectorType &vec, const std::string &filename)
Definition MarketIO.h:200
bool saveMarket(const SparseMatrixType &mat, const std::string &filename, int sym=0)
Definition MarketIO.h:232
bool loadMarket(SparseMatrixType &mat, const std::string &filename)
Definition MarketIO.h:134
bool saveMarketVector(const VectorType &vec, const std::string &filename)
Definition MarketIO.h:258
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:74
Definition BandTriangularSolver.h:13
uint8_t count
Definition ref_serial.h:256
Definition ForwardDeclarations.h:17
std::ofstream out("Result.txt")
std::ptrdiff_t j
Definition tut_arithmetic_redux_minmax.cpp:2