Eigen-unsupported  5.0.1-dev+284dcc12
 
Loading...
Searching...
No Matches
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// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10
11#ifndef EIGEN_SPARSE_MARKET_IO_H
12#define EIGEN_SPARSE_MARKET_IO_H
13
14#include <iostream>
15#include <vector>
16
17// IWYU pragma: private
18#include "./InternalHeaderCheck.h"
19
20namespace Eigen {
21
22namespace internal {
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;
27}
28
29template <>
30inline void GetMarketLine(const char* line, int& i, int& j, float& value) {
31 std::sscanf(line, "%d %d %g", &i, &j, &value);
32}
33
34template <>
35inline void GetMarketLine(const char* line, int& i, int& j, double& value) {
36 std::sscanf(line, "%d %d %lg", &i, &j, &value);
37}
38
39template <>
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));
42}
43
44template <>
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));
47}
48
49template <typename Scalar, typename StorageIndex>
50inline void GetMarketLine(const char* line, StorageIndex& i, StorageIndex& j, std::complex<Scalar>& value) {
51 std::stringstream sline(line);
52 Scalar valR, valI;
53 sline >> i >> j >> valR >> valI;
54 value = std::complex<Scalar>(valR, valI);
55}
56
57template <typename RealScalar>
58inline void GetDenseElt(const std::string& line, RealScalar& val) {
59 std::istringstream newline(line);
60 newline >> val;
61}
62
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);
69}
70
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) {
76 header += " complex";
77 if (sym == Symmetric)
78 header += " symmetric";
79 else if (sym == SelfAdjoint)
80 header += " Hermitian";
81 else
82 header += " general";
83 } else {
84 header += " real";
85 if (sym == Symmetric)
86 header += " symmetric";
87 else
88 header += " general";
89 }
90}
91
92template <typename Scalar, typename StorageIndex>
93inline void PutMatrixElt(Scalar value, StorageIndex row, StorageIndex col, std::ofstream& out) {
94 out << row << " " << col << " " << value << "\n";
95}
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";
99}
100
101template <typename Scalar>
102inline void putDenseElt(Scalar value, std::ofstream& out) {
103 out << value << "\n";
104}
105template <typename Scalar>
106inline void putDenseElt(std::complex<Scalar> value, std::ofstream& out) {
107 out << value.real() << " " << value.imag() << "\n";
108}
109
110} // end namespace internal
111
122inline bool getMarketHeader(const std::string& filename, int& sym, bool& iscomplex, bool& isdense) {
123 sym = 0;
124 iscomplex = false;
125 isdense = false;
126 std::ifstream in(filename.c_str(), std::ios::in);
127 if (!in) return false;
128
129 std::string line;
130 // The matrix header is always the first line in the file
131 std::getline(in, line);
132 eigen_assert(in.good());
133
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)
140 sym = Symmetric;
141 else if (substr[4].compare("Hermitian") == 0)
142 sym = SelfAdjoint;
143
144 return true;
145}
146
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;
161
162 char rdbuffer[4096];
163 input.rdbuf()->pubsetbuf(rdbuffer, 4096);
164
165 const int maxBuffersize = 2048;
166 char buffer[maxBuffersize];
167
168 bool readsizes = false;
169
171 std::vector<T> elements;
172
173 Index M(-1), N(-1), NNZ(-1);
174 Index count = 0;
175 while (input.getline(buffer, maxBuffersize)) {
176 // skip comments
177 // NOTE An appropriate test should be done on the header to get the symmetry
178 if (buffer[0] == '%') continue;
179
180 if (!readsizes) {
181 std::stringstream line(buffer);
182 line >> M >> N >> NNZ;
183 if (M > 0 && N > 0) {
184 readsizes = true;
185 mat.resize(M, N);
186 mat.reserve(NNZ);
187 elements.reserve(NNZ);
188 }
189 } else {
190 StorageIndex i(-1), j(-1);
191 Scalar value;
192 internal::GetMarketLine(buffer, i, j, value);
193
194 i--;
195 j--;
196 if (i >= 0 && j >= 0 && i < M && j < N) {
197 ++count;
198 elements.push_back(T(i, j, value));
199 } else {
200 std::cerr << "Invalid read: " << i << "," << j << "\n";
201 return false;
202 }
203 }
204 }
205
206 mat.setFromTriplets(elements.begin(), elements.end());
207 if (count != NNZ) {
208 std::cerr << count << "!=" << NNZ << "\n";
209 return false;
210 }
211 input.close();
212 return true;
213}
214
225template <typename DenseType>
226bool loadMarketDense(DenseType& mat, const std::string& filename) {
227 typedef typename DenseType::Scalar Scalar;
228 std::ifstream in(filename.c_str(), std::ios::in);
229 if (!in) return false;
230
231 std::string line;
232 Index rows(0), cols(0);
233 do { // Skip comments
234 std::getline(in, line);
235 eigen_assert(in.good());
236 } while (line[0] == '%');
237 std::istringstream newline(line);
238 newline >> rows >> cols;
239
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);
245
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";
249 } else {
250 std::cerr << "Input matrix can not be resized to" << rows << " x " << cols << "as given in " << filename << "\n";
251 }
252 in.close();
253 return false;
254 }
255
256 mat.resize(rows, cols);
257 Index row = 0;
258 Index col = 0;
259 Index n = 0;
260 Scalar value;
261 while (std::getline(in, line) && (row < rows) && (col < cols)) {
262 internal::GetDenseElt(line, value);
263 // matrixmarket format is column major
264 mat(row, col) = value;
265 row++;
266 if (row == rows) {
267 row = 0;
268 col++;
269 }
270 n++;
271 }
272 in.close();
273 if (n != mat.size()) {
274 std::cerr << "Unable to read all elements from file " << filename << "\n";
275 return false;
276 }
277 return true;
278}
279
283template <typename VectorType>
284bool loadMarketVector(VectorType& vec, const std::string& filename) {
285 return loadMarketDense(vec, filename);
286}
287
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;
304
305 out.flags(std::ios_base::scientific);
306 out.precision(std::numeric_limits<RealScalar>::digits10 + 2);
307 std::string header;
308 internal::putMarketHeader<Scalar>(header, sym);
309 out << header << std::endl;
310 out << mat.rows() << " " << mat.cols() << " " << mat.nonZeros() << "\n";
311 int count = 0;
312 EIGEN_UNUSED_VARIABLE(count);
313 for (int j = 0; j < mat.outerSize(); ++j)
314 for (typename SparseMatrixType::InnerIterator it(mat, j); it; ++it) {
315 ++count;
316 internal::PutMatrixElt(it.value(), it.row() + 1, it.col() + 1, out);
317 }
318 out.close();
319 return true;
320}
321
331
332template <typename DenseType>
333bool saveMarketDense(const DenseType& mat, const std::string& filename) {
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;
338
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";
343 else
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);
349 }
350 }
351 out.close();
352 return true;
353}
354
359template <typename VectorType>
360bool saveMarketVector(const VectorType& vec, const std::string& filename) {
361 return saveMarketDense(vec, filename);
362}
363
364} // end namespace Eigen
365
366#endif // EIGEN_SPARSE_MARKET_IO_H
bool loadMarketVector(VectorType &vec, const std::string &filename)
Same functionality as loadMarketDense, deprecated.
Definition MarketIO.h:284
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
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
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
const int Dynamic