11 #ifndef EIGEN_SPARSE_MARKET_IO_H 12 #define EIGEN_SPARSE_MARKET_IO_H 20 template <
typename Scalar>
21 inline bool GetMarketLine (std::stringstream& line, Index& M, Index& N, Index& i, Index& j, Scalar& value)
23 line >> i >> j >> value;
26 if(i>=0 && j>=0 && i<M && j<N)
33 template <
typename Scalar>
34 inline bool GetMarketLine (std::stringstream& line, Index& M, Index& N, Index& i, Index& j, std::complex<Scalar>& value)
37 line >> i >> j >> valR >> valI;
40 if(i>=0 && j>=0 && i<M && j<N)
42 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>
85 inline void PutMatrixElt(Scalar value,
int row,
int col, std::ofstream& out)
87 out << row <<
" "<< col <<
" " << value <<
"\n";
89 template<
typename Scalar>
90 inline void PutMatrixElt(std::complex<Scalar> value,
int row,
int 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";
109 inline 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;
133 template<
typename SparseMatrixType>
134 bool loadMarket(SparseMatrixType& mat,
const std::string& filename)
136 typedef typename SparseMatrixType::Scalar Scalar;
137 typedef typename SparseMatrixType::Index Index;
138 std::ifstream input(filename.c_str(),std::ios::in);
142 const int maxBuffersize = 2048;
143 char buffer[maxBuffersize];
145 bool readsizes =
false;
147 typedef Triplet<Scalar,Index> T;
148 std::vector<T> elements;
150 Index M(-1), N(-1), NNZ(-1);
152 while(input.getline(buffer, maxBuffersize))
159 std::stringstream line(buffer);
163 line >> M >> N >> NNZ;
164 if(M > 0 && N > 0 && NNZ > 0)
176 if( internal::GetMarketLine(line, M, N, i, j, value) )
179 elements.push_back(T(i,j,value));
182 std::cerr <<
"Invalid read: " << i <<
"," << j <<
"\n";
185 mat.setFromTriplets(elements.begin(), elements.end());
187 std::cerr << count <<
"!=" << NNZ <<
"\n";
193 template<
typename VectorType>
194 bool loadMarketVector(VectorType& vec,
const std::string& filename)
196 typedef typename VectorType::Scalar Scalar;
197 std::ifstream in(filename.c_str(), std::ios::in);
205 std::getline(in, line); eigen_assert(in.good());
206 }
while (line[0] ==
'%');
207 std::istringstream newline(line);
209 eigen_assert(n>0 && col>0);
213 while ( std::getline(in, line) && (i < n) ){
214 internal::GetVectorElt(line, value);
219 std::cerr<<
"Unable to read all elements from file " << filename <<
"\n";
225 template<
typename SparseMatrixType>
226 bool saveMarket(
const SparseMatrixType& mat,
const std::string& filename,
int sym = 0)
228 typedef typename SparseMatrixType::Scalar Scalar;
229 std::ofstream out(filename.c_str(),std::ios::out);
233 out.flags(std::ios_base::scientific);
236 internal::putMarketHeader<Scalar>(header, sym);
237 out << header << std::endl;
238 out << mat.rows() <<
" " << mat.cols() <<
" " << mat.nonZeros() <<
"\n";
240 for(
int j=0; j<mat.outerSize(); ++j)
241 for(
typename SparseMatrixType::InnerIterator it(mat,j); it; ++it)
244 internal::PutMatrixElt(it.value(), it.row()+1, it.col()+1, out);
251 template<
typename VectorType>
252 bool saveMarketVector (
const VectorType& vec,
const std::string& filename)
254 typedef typename VectorType::Scalar Scalar;
255 std::ofstream out(filename.c_str(),std::ios::out);
259 out.flags(std::ios_base::scientific);
261 if(internal::is_same<Scalar, std::complex<float> >::value || internal::is_same<Scalar, std::complex<double> >::value)
262 out <<
"%%MatrixMarket matrix array complex general\n";
264 out <<
"%%MatrixMarket matrix array real general\n";
265 out << vec.size() <<
" "<< 1 <<
"\n";
266 for (
int i=0; i < vec.size(); i++){
267 internal::putVectorElt(vec(i), out);
275 #endif // EIGEN_SPARSE_MARKET_IO_H Namespace containing all symbols from the Eigen library.
Definition: AdolcForward:45