SparseExtra module

Classes

class  Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >
 The RandomSetter is a wrapper object allowing to set/update a sparse matrix with random access. More...
 

Functions

bool Eigen::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. More...
 
template<typename SparseMatrixType >
bool Eigen::loadMarket (SparseMatrixType &mat, const std::string &filename)
 Loads a sparse matrix from a matrixmarket format file. More...
 
template<typename DenseType >
bool Eigen::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 parsed and the file contains the wrong dimensions it is undefined behaviour. More...
 
template<typename VectorType >
bool Eigen::loadMarketVector (VectorType &vec, const std::string &filename)
 Same functionality as loadMarketDense, deprecated. More...
 
template<typename SparseMatrixType >
bool Eigen::saveMarket (const SparseMatrixType &mat, const std::string &filename, int sym=0)
 writes a sparse Matrix to a marketmarket format file More...
 
template<typename DenseType >
bool Eigen::saveMarketDense (const DenseType &mat, const std::string &filename)
 writes a dense Matrix or vector to a marketmarket format file More...
 
template<typename VectorType >
bool Eigen::saveMarketVector (const VectorType &vec, const std::string &filename)
 Same functionality as saveMarketDense, deprecated. More...
 

Detailed Description

This module contains some experimental features extending the sparse module:

Function Documentation

◆ getMarketHeader()

bool Eigen::getMarketHeader ( const std::string &  filename,
int &  sym,
bool &  iscomplex,
bool &  isdense 
)
inline

Reads the header of a matrixmarket file and determines the properties of a matrix.

Parameters
filenameof the file
symif the matrix is hermitian,symmetric or none of the latter (sym=0)
iscomplexif the matrix has complex or real coefficients
isdenseif the matrix is dense or sparse
Returns
true if the file was found

Definition at line 122 of file MarketIO.h.

123 {
124  sym = 0;
125  iscomplex = false;
126  isdense = false;
127  std::ifstream in(filename.c_str(),std::ios::in);
128  if(!in)
129  return false;
130 
131  std::string line;
132  // The matrix header is always the first line in the file
133  std::getline(in, line); eigen_assert(in.good());
134 
135  std::stringstream fmtline(line);
136  std::string substr[5];
137  fmtline>> substr[0] >> substr[1] >> substr[2] >> substr[3] >> substr[4];
138  if(substr[2].compare("array") == 0) isdense = true;
139  if(substr[3].compare("complex") == 0) iscomplex = true;
140  if(substr[4].compare("symmetric") == 0) sym = Symmetric;
141  else if (substr[4].compare("Hermitian") == 0) sym = SelfAdjoint;
142 
143  return true;
144 }
#define eigen_assert(x)
SelfAdjoint
Symmetric

◆ loadMarket()

template<typename SparseMatrixType >
bool Eigen::loadMarket ( SparseMatrixType &  mat,
const std::string &  filename 
)

Loads a sparse matrix from a matrixmarket format file.

Template Parameters
SparseMatrixTypeto read into, symmetries are not supported
Parameters
matSparseMatrix to read into, current values are overwritten
filenameto parse matrix from
Returns
returns true if file exists. Returns false if the parsing did not succeed.

Definition at line 155 of file MarketIO.h.

156 {
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)
161  return false;
162 
163  char rdbuffer[4096];
164  input.rdbuf()->pubsetbuf(rdbuffer, 4096);
165 
166  const int maxBuffersize = 2048;
167  char buffer[maxBuffersize];
168 
169  bool readsizes = false;
170 
171  typedef Triplet<Scalar,StorageIndex> T;
172  std::vector<T> elements;
173 
174  Index M(-1), N(-1), NNZ(-1);
175  Index count = 0;
176  while(input.getline(buffer, maxBuffersize))
177  {
178  // skip comments
179  //NOTE An appropriate test should be done on the header to get the symmetry
180  if(buffer[0]=='%')
181  continue;
182 
183  if(!readsizes)
184  {
185  std::stringstream line(buffer);
186  line >> M >> N >> NNZ;
187  if(M > 0 && N > 0)
188  {
189  readsizes = true;
190  mat.resize(M,N);
191  mat.reserve(NNZ);
192  elements.reserve(NNZ);
193  }
194  }
195  else
196  {
197  StorageIndex i(-1), j(-1);
198  Scalar value;
199  internal::GetMarketLine(buffer, i, j, value);
200 
201  i--;
202  j--;
203  if(i>=0 && j>=0 && i<M && j<N)
204  {
205  ++count;
206  elements.push_back(T(i,j,value));
207  }
208  else
209  {
210  std::cerr << "Invalid read: " << i << "," << j << "\n";
211  return false;
212  }
213  }
214  }
215 
216  mat.setFromTriplets(elements.begin(), elements.end());
217  if(count!=NNZ){
218  std::cerr << count << "!=" << NNZ << "\n";
219  return false;
220  }
221  input.close();
222  return true;
223 }
int i
Matrix4Xd M
MatrixXf mat
void GetMarketLine(const char *line, StorageIndex &i, StorageIndex &j, std::complex< Scalar > &value)
Definition: MarketIO.h:43
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
std::ptrdiff_t j

◆ loadMarketDense()

template<typename DenseType >
bool Eigen::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 parsed and the file contains the wrong dimensions it is undefined behaviour.

Template Parameters
DenseMatrixTypeto read into
Parameters
matDenseMatrix to read into, current values are overwritten, symmetries are not supported
filenameto parse matrix from
Returns
true if parsing was successful. Returns false if the parsing did not succeed.

Definition at line 236 of file MarketIO.h.

237 {
238  typedef typename DenseType::Scalar Scalar;
239  std::ifstream in(filename.c_str(), std::ios::in);
240  if(!in)
241  return false;
242 
243  std::string line;
244  Index rows(0), cols(0);
245  do
246  { // Skip comments
247  std::getline(in, line); eigen_assert(in.good());
248  } while (line[0] == '%');
249  std::istringstream newline(line);
250  newline >> rows >> cols;
251 
252  bool sizes_not_positive=(rows<1 || cols<1);
253  bool wrong_input_rows = (DenseType::MaxRowsAtCompileTime != Dynamic && rows > DenseType::MaxRowsAtCompileTime) ||
254  (DenseType::RowsAtCompileTime!=Dynamic && rows!=DenseType::RowsAtCompileTime);
255  bool wrong_input_cols = (DenseType::MaxColsAtCompileTime != Dynamic && cols > DenseType::MaxColsAtCompileTime) ||
256  (DenseType::ColsAtCompileTime!=Dynamic && cols!=DenseType::ColsAtCompileTime);
257 
258  if(sizes_not_positive || wrong_input_rows || wrong_input_cols){
259  if(sizes_not_positive){
260  std::cerr<< "non-positive row or column size in file" << filename << "\n";
261  }else{
262  std::cerr<< "Input matrix can not be resized to"<<rows<<" x "<<cols<< "as given in " << filename << "\n";
263  }
264  in.close();
265  return false;
266  }
267 
268  mat.resize(rows,cols);
269  Index row = 0;
270  Index col = 0;
271  Index n=0;
272  Scalar value;
273  while ( std::getline(in, line) && (row < rows) && (col < cols)){
274  internal::GetDenseElt(line, value);
275  //matrixmarket format is column major
276  mat(row,col) = value;
277  row++;
278  if(row==rows){
279  row=0;
280  col++;
281  }
282  n++;
283  }
284  in.close();
285  if (n!=mat.size()){
286  std::cerr<< "Unable to read all elements from file " << filename << "\n";
287  return false;
288  }
289  return true;
290 }
int n
RowXpr row(Index i) const
ColXpr col(Index i) const
void GetDenseElt(const std::string &line, std::complex< RealScalar > &val)
Definition: MarketIO.h:59
const int Dynamic
Derived::Index cols
Derived::Index rows

◆ loadMarketVector()

template<typename VectorType >
bool Eigen::loadMarketVector ( VectorType &  vec,
const std::string &  filename 
)

Same functionality as loadMarketDense, deprecated.

Definition at line 296 of file MarketIO.h.

297 {
298  return loadMarketDense(vec, filename);
299 }
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:236

◆ saveMarket()

template<typename SparseMatrixType >
bool Eigen::saveMarket ( const SparseMatrixType &  mat,
const std::string &  filename,
int  sym = 0 
)

writes a sparse Matrix to a marketmarket format file

Template Parameters
SparseMatrixTypeto write to file
Parameters
matmatrix to write to file
filenamefilename to write to
symat the moment no symmetry operations are supported
Returns
true if writing succeeded

Definition at line 312 of file MarketIO.h.

313 {
314  typedef typename SparseMatrixType::Scalar Scalar;
315  typedef typename SparseMatrixType::RealScalar RealScalar;
316  std::ofstream out(filename.c_str(),std::ios::out);
317  if(!out)
318  return false;
319 
320  out.flags(std::ios_base::scientific);
321  out.precision(std::numeric_limits<RealScalar>::digits10 + 2);
322  std::string header;
323  internal::putMarketHeader<Scalar>(header, sym);
324  out << header << std::endl;
325  out << mat.rows() << " " << mat.cols() << " " << mat.nonZeros() << "\n";
326  int count = 0;
327  for(int j=0; j<mat.outerSize(); ++j)
328  for(typename SparseMatrixType::InnerIterator it(mat,j); it; ++it)
329  {
330  ++ count;
331  internal::PutMatrixElt(it.value(), it.row()+1, it.col()+1, out);
332  }
333  out.close();
334  return true;
335 }
void PutMatrixElt(std::complex< Scalar > value, StorageIndex row, StorageIndex col, std::ofstream &out)
Definition: MarketIO.h:92

◆ saveMarketDense()

template<typename DenseType >
bool Eigen::saveMarketDense ( const DenseType &  mat,
const std::string &  filename 
)

writes a dense Matrix or vector to a marketmarket format file

Template Parameters
DenseMatrixTypeto write to file
Parameters
matmatrix to write to file
filenamefilename to write to
Returns
true if writing succeeded

Definition at line 349 of file MarketIO.h.

350 {
351  typedef typename DenseType::Scalar Scalar;
352  typedef typename DenseType::RealScalar RealScalar;
353  std::ofstream out(filename.c_str(),std::ios::out);
354  if(!out)
355  return false;
356 
357  out.flags(std::ios_base::scientific);
358  out.precision(std::numeric_limits<RealScalar>::digits10 + 2);
359  if(internal::is_same<Scalar, std::complex<float> >::value || internal::is_same<Scalar, std::complex<double> >::value)
360  out << "%%MatrixMarket matrix array complex general\n";
361  else
362  out << "%%MatrixMarket matrix array real general\n";
363  out << mat.rows() << " "<< mat.cols() << "\n";
364  for (Index i=0; i < mat.cols(); i++){
365  for (Index j=0; j < mat.rows(); j++){
366  internal::putDenseElt(mat(j,i), out);
367  }
368  }
369  out.close();
370  return true;
371 }
void putDenseElt(std::complex< Scalar > value, std::ofstream &out)
Definition: MarketIO.h:104

◆ saveMarketVector()

template<typename VectorType >
bool Eigen::saveMarketVector ( const VectorType &  vec,
const std::string &  filename 
)

Same functionality as saveMarketDense, deprecated.

Definition at line 378 of file MarketIO.h.

379 {
380  return saveMarketDense(vec, filename);
381 }
bool saveMarketDense(const DenseType &mat, const std::string &filename)
writes a dense Matrix or vector to a marketmarket format file
Definition: MarketIO.h:349