MarketIO.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2011 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2012 Desire NUENTSA WAKAM <desire.nuentsa_wakam@inria.fr>
6 //
7 // Eigen is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Lesser General Public
9 // License as published by the Free Software Foundation; either
10 // version 3 of the License, or (at your option) any later version.
11 //
12 // Alternatively, you can redistribute it and/or
13 // modify it under the terms of the GNU General Public License as
14 // published by the Free Software Foundation; either version 2 of
15 // the License, or (at your option) any later version.
16 //
17 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License and a copy of the GNU General Public License along with
24 // Eigen. If not, see <http://www.gnu.org/licenses/>.
25 
26 #ifndef EIGEN_SPARSE_MARKET_IO_H
27 #define EIGEN_SPARSE_MARKET_IO_H
28 
29 #include <iostream>
30 
31 namespace Eigen {
32 
33 namespace internal
34 {
35  template <typename Scalar>
36  inline bool GetMarketLine (std::stringstream& line, int& M, int& N, int& i, int& j, Scalar& value)
37  {
38  line >> i >> j >> value;
39  i--;
40  j--;
41  if(i>=0 && j>=0 && i<M && j<N)
42  {
43  return true;
44  }
45  else
46  return false;
47  }
48  template <typename Scalar>
49  inline bool GetMarketLine (std::stringstream& line, int& M, int& N, int& i, int& j, std::complex<Scalar>& value)
50  {
51  Scalar valR, valI;
52  line >> i >> j >> valR >> valI;
53  i--;
54  j--;
55  if(i>=0 && j>=0 && i<M && j<N)
56  {
57  value = std::complex<Scalar>(valR, valI);
58  return true;
59  }
60  else
61  return false;
62  }
63 
64  template <typename RealScalar>
65  inline void GetVectorElt (const std::string& line, RealScalar& val)
66  {
67  std::istringstream newline(line);
68  newline >> val;
69  }
70 
71  template <typename RealScalar>
72  inline void GetVectorElt (const std::string& line, std::complex<RealScalar>& val)
73  {
74  RealScalar valR, valI;
75  std::istringstream newline(line);
76  newline >> valR >> valI;
77  val = std::complex<RealScalar>(valR, valI);
78  }
79 
80  template<typename Scalar>
81  inline void putMarketHeader(std::string& header,int sym)
82  {
83  header= "%%MatrixMarket matrix coordinate ";
84  if(internal::is_same<Scalar, std::complex<float> >::value || internal::is_same<Scalar, std::complex<double> >::value)
85  {
86  header += " complex";
87  if(sym == Symmetric) header += " symmetric";
88  else if (sym == SelfAdjoint) header += " Hermitian";
89  else header += " general";
90  }
91  else
92  {
93  header += " real";
94  if(sym == Symmetric) header += " symmetric";
95  else header += " general";
96  }
97  }
98 
99  template<typename Scalar>
100  inline void PutMatrixElt(Scalar value, int row, int col, std::ofstream& out)
101  {
102  out << row << " "<< col << " " << value << "\n";
103  }
104  template<typename Scalar>
105  inline void PutMatrixElt(std::complex<Scalar> value, int row, int col, std::ofstream& out)
106  {
107  out << row << " " << col << " " << value.real() << " " << value.imag() << "\n";
108  }
109 
110 
111  template<typename Scalar>
112  inline void putVectorElt(Scalar value, std::ofstream& out)
113  {
114  out << value << "\n";
115  }
116  template<typename Scalar>
117  inline void putVectorElt(std::complex<Scalar> value, std::ofstream& out)
118  {
119  out << value.real << " " << value.imag()<< "\n";
120  }
121 
122 } // end namepsace internal
123 
124 inline bool getMarketHeader(const std::string& filename, int& sym, bool& iscomplex, bool& isvector)
125 {
126  sym = 0;
127  isvector = false;
128  std::ifstream in(filename.c_str(),std::ios::in);
129  if(!in)
130  return false;
131 
132  std::string line;
133  // The matrix header is always the first line in the file
134  std::getline(in, line); assert(in.good());
135 
136  std::stringstream fmtline(line);
137  std::string substr[5];
138  fmtline>> substr[0] >> substr[1] >> substr[2] >> substr[3] >> substr[4];
139  if(substr[2].compare("array") == 0) isvector = true;
140  if(substr[3].compare("complex") == 0) iscomplex = true;
141  if(substr[4].compare("symmetric") == 0) sym = Symmetric;
142  else if (substr[4].compare("Hermitian") == 0) sym = SelfAdjoint;
143 
144  return true;
145 }
146 
147 template<typename SparseMatrixType>
148 bool loadMarket(SparseMatrixType& mat, const std::string& filename)
149 {
150  typedef typename SparseMatrixType::Scalar Scalar;
151  std::ifstream input(filename.c_str(),std::ios::in);
152  if(!input)
153  return false;
154 
155  const int maxBuffersize = 2048;
156  char buffer[maxBuffersize];
157 
158  bool readsizes = false;
159 
160  int M(-1), N(-1), NNZ(-1);
161  int count = 0;
162  while(input.getline(buffer, maxBuffersize))
163  {
164  // skip comments
165  //NOTE An appropriate test should be done on the header to get the symmetry
166  if(buffer[0]=='%')
167  continue;
168 
169  std::stringstream line(buffer);
170 
171  if(!readsizes)
172  {
173  line >> M >> N >> NNZ;
174  if(M > 0 && N > 0 && NNZ > 0)
175  {
176  readsizes = true;
177  std::cout << "sizes: " << M << "," << N << "," << NNZ << "\n";
178  mat.resize(M,N);
179  mat.reserve(NNZ);
180  }
181  }
182  else
183  {
184  int i(-1), j(-1);
185  Scalar value;
186  if( internal::GetMarketLine(line, M, N, i, j, value) )
187  {
188  ++ count;
189  mat.insert(i,j) = value;
190  }
191  else
192  std::cerr << "Invalid read: " << i << "," << j << "\n";
193  }
194  }
195  mat.makeCompressed();
196  if(count!=NNZ)
197  std::cerr << count << "!=" << NNZ << "\n";
198 
199  input.close();
200  return true;
201 }
202 
203 template<typename VectorType>
204 bool loadMarketVector(VectorType& vec, const std::string& filename)
205 {
206  typedef typename VectorType::Scalar Scalar;
207  std::ifstream in(filename.c_str(), std::ios::in);
208  if(!in)
209  return false;
210 
211  std::string line;
212  int n(0), col(0);
213  do
214  { // Skip comments
215  std::getline(in, line); assert(in.good());
216  } while (line[0] == '%');
217  std::istringstream newline(line);
218  newline >> n >> col;
219  assert(n>0 && col>0);
220  vec.resize(n);
221  int i = 0;
222  Scalar value;
223  while ( std::getline(in, line) && (i < n) ){
224  internal::GetVectorElt(line, value);
225  vec(i++) = value;
226  }
227  in.close();
228  if (i!=n){
229  std::cerr<< "Unable to read all elements from file " << filename << "\n";
230  return false;
231  }
232  return true;
233 }
234 
235 template<typename SparseMatrixType>
236 bool saveMarket(const SparseMatrixType& mat, const std::string& filename, int sym = 0)
237 {
238  typedef typename SparseMatrixType::Scalar Scalar;
239  std::ofstream out(filename.c_str(),std::ios::out);
240  if(!out)
241  return false;
242 
243  out.flags(std::ios_base::scientific);
244  out.precision(64);
245  std::string header;
246  internal::putMarketHeader<Scalar>(header, sym);
247  out << header << std::endl;
248  out << mat.rows() << " " << mat.cols() << " " << mat.nonZeros() << "\n";
249  int count = 0;
250  for(int j=0; j<mat.outerSize(); ++j)
251  for(typename SparseMatrixType::InnerIterator it(mat,j); it; ++it)
252  {
253  ++ count;
254  internal::PutMatrixElt(it.value(), it.row()+1, it.col()+1, out);
255  // out << it.row()+1 << " " << it.col()+1 << " " << it.value() << "\n";
256  }
257  out.close();
258  return true;
259 }
260 
261 template<typename VectorType>
262 bool saveMarketVector (const VectorType& vec, const std::string& filename)
263 {
264  typedef typename VectorType::Scalar Scalar;
265  std::ofstream out(filename.c_str(),std::ios::out);
266  if(!out)
267  return false;
268 
269  out.flags(std::ios_base::scientific);
270  out.precision(64);
271  if(internal::is_same<Scalar, std::complex<float> >::value || internal::is_same<Scalar, std::complex<double> >::value)
272  out << "%%MatrixMarket matrix array complex general\n";
273  else
274  out << "%%MatrixMarket matrix array real general\n";
275  out << vec.size() << " "<< 1 << "\n";
276  for (int i=0; i < vec.size(); i++){
277  internal::putVectorElt(vec(i), out);
278  }
279  out.close();
280  return true;
281 }
282 
283 } // end namespace Eigen
284 
285 #endif // EIGEN_SPARSE_MARKET_IO_H