Prusa Slicer 2.6.0
Loading...
Searching...
No Matches
Eigen::MatrixMarketIterator< Scalar > Class Template Reference

Iterator to browse matrices from a specified folder. More...

#include <src/eigen/unsupported/Eigen/src/SparseExtra/MatrixMarketIterator.h>

+ Collaboration diagram for Eigen::MatrixMarketIterator< Scalar >:

Public Types

typedef Matrix< Scalar, Dynamic, 1 > VectorType
 
typedef SparseMatrix< Scalar, ColMajorMatrixType
 

Public Member Functions

 MatrixMarketIterator (const std::string &folder)
 
 ~MatrixMarketIterator ()
 
MatrixMarketIteratoroperator++ ()
 
 operator bool () const
 
MatrixTypematrix ()
 
VectorTyperhs ()
 
VectorTyperefX ()
 
std::string & matname ()
 
int sym ()
 
bool hasRhs ()
 
bool hasrefX ()
 
bool isFolderValid ()
 

Protected Member Functions

bool Fileexists (std::string file)
 
void Getnextvalidmatrix ()
 

Protected Attributes

int m_sym
 
MatrixType m_mat
 
VectorType m_rhs
 
VectorType m_refX
 
std::string m_matname
 
bool m_isvalid
 
bool m_matIsLoaded
 
bool m_hasRhs
 
bool m_hasrefX
 
std::string m_folder
 
DIR * m_folder_id
 
struct dirent * m_curs_id
 

Private Types

typedef NumTraits< Scalar >::Real RealScalar
 

Detailed Description

template<typename Scalar>
class Eigen::MatrixMarketIterator< Scalar >

Iterator to browse matrices from a specified folder.

This is used to load all the matrices from a folder. The matrices should be in Matrix Market format It is assumed that the matrices are named as matname.mtx and matname_SPD.mtx if the matrix is Symmetric and positive definite (or Hermitian) The right hand side vectors are loaded as well, if they exist. They should be named as matname_b.mtx. Note that the right hand side for a SPD matrix is named as matname_SPD_b.mtx

Sometimes a reference solution is available. In this case, it should be named as matname_x.mtx

Sample code

Template Parameters
ScalarThe scalar type

Member Typedef Documentation

◆ MatrixType

template<typename Scalar >
typedef SparseMatrix<Scalar,ColMajor> Eigen::MatrixMarketIterator< Scalar >::MatrixType

◆ RealScalar

template<typename Scalar >
typedef NumTraits<Scalar>::Real Eigen::MatrixMarketIterator< Scalar >::RealScalar
private

◆ VectorType

template<typename Scalar >
typedef Matrix<Scalar,Dynamic,1> Eigen::MatrixMarketIterator< Scalar >::VectorType

Constructor & Destructor Documentation

◆ MatrixMarketIterator()

template<typename Scalar >
Eigen::MatrixMarketIterator< Scalar >::MatrixMarketIterator ( const std::string &  folder)
inline
51 : m_sym(0), m_isvalid(false), m_matIsLoaded(false), m_hasRhs(false), m_hasrefX(false), m_folder(folder)
52 {
53 m_folder_id = opendir(folder.c_str());
54 if(m_folder_id)
56 }
bool m_hasRhs
Definition MatrixMarketIterator.h:237
bool m_isvalid
Definition MatrixMarketIterator.h:235
void Getnextvalidmatrix()
Definition MatrixMarketIterator.h:187
bool m_matIsLoaded
Definition MatrixMarketIterator.h:236
DIR * m_folder_id
Definition MatrixMarketIterator.h:240
bool m_hasrefX
Definition MatrixMarketIterator.h:238
int m_sym
Definition MatrixMarketIterator.h:230
std::string m_folder
Definition MatrixMarketIterator.h:239

References Eigen::MatrixMarketIterator< Scalar >::Getnextvalidmatrix(), and Eigen::MatrixMarketIterator< Scalar >::m_folder_id.

+ Here is the call graph for this function:

◆ ~MatrixMarketIterator()

template<typename Scalar >
Eigen::MatrixMarketIterator< Scalar >::~MatrixMarketIterator ( )
inline

Member Function Documentation

◆ Fileexists()

template<typename Scalar >
bool Eigen::MatrixMarketIterator< Scalar >::Fileexists ( std::string  file)
inlineprotected
174 {
175 std::ifstream file_id(file.c_str());
176 if (!file_id.good() )
177 {
178 return false;
179 }
180 else
181 {
182 file_id.close();
183 return true;
184 }
185 }

Referenced by Eigen::MatrixMarketIterator< Scalar >::refX(), and Eigen::MatrixMarketIterator< Scalar >::rhs().

+ Here is the caller graph for this function:

◆ Getnextvalidmatrix()

template<typename Scalar >
void Eigen::MatrixMarketIterator< Scalar >::Getnextvalidmatrix ( )
inlineprotected
188 {
189 m_isvalid = false;
190 // Here, we return with the next valid matrix in the folder
191 while ( (m_curs_id = readdir(m_folder_id)) != NULL) {
192 m_isvalid = false;
193 std::string curfile;
194 curfile = m_folder + "/" + m_curs_id->d_name;
195 // Discard if it is a folder
196 if (m_curs_id->d_type == DT_DIR) continue; //FIXME This may not be available on non BSD systems
197// struct stat st_buf;
198// stat (curfile.c_str(), &st_buf);
199// if (S_ISDIR(st_buf.st_mode)) continue;
200
201 // Determine from the header if it is a matrix or a right hand side
202 bool isvector,iscomplex=false;
203 if(!getMarketHeader(curfile,m_sym,iscomplex,isvector)) continue;
204 if(isvector) continue;
205 if (!iscomplex)
206 {
207 if(internal::is_same<Scalar, std::complex<float> >::value || internal::is_same<Scalar, std::complex<double> >::value)
208 continue;
209 }
210 if (iscomplex)
211 {
213 continue;
214 }
215
216
217 // Get the matrix name
218 std::string filename = m_curs_id->d_name;
219 m_matname = filename.substr(0, filename.length()-4);
220
221 // Find if the matrix is SPD
222 size_t found = m_matname.find("SPD");
223 if( (found!=std::string::npos) && (m_sym != NonSymmetric) )
224 m_sym = SPD;
225
226 m_isvalid = true;
227 break;
228 }
229 }
std::string m_matname
Definition MatrixMarketIterator.h:234
struct dirent * m_curs_id
Definition MatrixMarketIterator.h:241
bool getMarketHeader(const std::string &filename, int &sym, bool &iscomplex, bool &isvector)
Definition MarketIO.h:109
@ NonSymmetric
Definition MatrixMarketIterator.h:18
@ SPD
Definition MatrixMarketIterator.h:17
@ value
Definition Meta.h:63

References Eigen::getMarketHeader(), Eigen::MatrixMarketIterator< Scalar >::m_curs_id, Eigen::MatrixMarketIterator< Scalar >::m_folder, Eigen::MatrixMarketIterator< Scalar >::m_folder_id, Eigen::MatrixMarketIterator< Scalar >::m_isvalid, Eigen::MatrixMarketIterator< Scalar >::m_matname, Eigen::MatrixMarketIterator< Scalar >::m_sym, Eigen::NonSymmetric, and Eigen::SPD.

Referenced by Eigen::MatrixMarketIterator< Scalar >::MatrixMarketIterator(), and Eigen::MatrixMarketIterator< Scalar >::operator++().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ hasrefX()

template<typename Scalar >
bool Eigen::MatrixMarketIterator< Scalar >::hasrefX ( )
inline

◆ hasRhs()

template<typename Scalar >
bool Eigen::MatrixMarketIterator< Scalar >::hasRhs ( )
inline

◆ isFolderValid()

template<typename Scalar >
bool Eigen::MatrixMarketIterator< Scalar >::isFolderValid ( )
inline

◆ matname()

template<typename Scalar >
std::string & Eigen::MatrixMarketIterator< Scalar >::matname ( )
inline

◆ matrix()

template<typename Scalar >
MatrixType & Eigen::MatrixMarketIterator< Scalar >::matrix ( )
inline

Return the sparse matrix corresponding to the current file

75 {
76 // Read the matrix
77 if (m_matIsLoaded) return m_mat;
78
79 std::string matrix_file = m_folder + "/" + m_matname + ".mtx";
80 if ( !loadMarket(m_mat, matrix_file))
81 {
82 std::cerr << "Warning loadMarket failed when loading \"" << matrix_file << "\"" << std::endl;
83 m_matIsLoaded = false;
84 return m_mat;
85 }
86 m_matIsLoaded = true;
87
88 if (m_sym != NonSymmetric)
89 {
90 // Check whether we need to restore a full matrix:
91 RealScalar diag_norm = m_mat.diagonal().norm();
92 RealScalar lower_norm = m_mat.template triangularView<Lower>().norm();
93 RealScalar upper_norm = m_mat.template triangularView<Upper>().norm();
94 if(lower_norm>diag_norm && upper_norm==diag_norm)
95 {
96 // only the lower part is stored
97 MatrixType tmp(m_mat);
98 m_mat = tmp.template selfadjointView<Lower>();
99 }
100 else if(upper_norm>diag_norm && lower_norm==diag_norm)
101 {
102 // only the upper part is stored
103 MatrixType tmp(m_mat);
104 m_mat = tmp.template selfadjointView<Upper>();
105 }
106 }
107 return m_mat;
108 }
MatrixType m_mat
Definition MatrixMarketIterator.h:231
NumTraits< Scalar >::Real RealScalar
Definition MatrixMarketIterator.h:44
SparseMatrix< Scalar, ColMajor > MatrixType
Definition MatrixMarketIterator.h:47
RealScalar norm() const
Definition SparseDot.h:84
const ConstDiagonalReturnType diagonal() const
Definition SparseMatrix.h:650
bool loadMarket(SparseMatrixType &mat, const std::string &filename)
Definition MarketIO.h:134

References Eigen::SparseMatrix< _Scalar, _Options, _StorageIndex >::diagonal(), Eigen::loadMarket(), Eigen::MatrixMarketIterator< Scalar >::m_folder, Eigen::MatrixMarketIterator< Scalar >::m_mat, Eigen::MatrixMarketIterator< Scalar >::m_matIsLoaded, Eigen::MatrixMarketIterator< Scalar >::m_matname, Eigen::MatrixMarketIterator< Scalar >::m_sym, Eigen::NonSymmetric, and Eigen::SparseMatrixBase< Derived >::norm().

Referenced by Eigen::MatrixMarketIterator< Scalar >::rhs().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ operator bool()

template<typename Scalar >
Eigen::MatrixMarketIterator< Scalar >::operator bool ( ) const
inline

◆ operator++()

template<typename Scalar >
MatrixMarketIterator & Eigen::MatrixMarketIterator< Scalar >::operator++ ( )
inline
64 {
65 m_matIsLoaded = false;
66 m_hasrefX = false;
67 m_hasRhs = false;
69 return *this;
70 }

References Eigen::MatrixMarketIterator< Scalar >::Getnextvalidmatrix(), Eigen::MatrixMarketIterator< Scalar >::m_hasrefX, Eigen::MatrixMarketIterator< Scalar >::m_hasRhs, and Eigen::MatrixMarketIterator< Scalar >::m_matIsLoaded.

+ Here is the call graph for this function:

◆ refX()

template<typename Scalar >
VectorType & Eigen::MatrixMarketIterator< Scalar >::refX ( )
inline

Return a reference solution If it is not provided and if the right hand side is not available then refX is randomly generated such that A*refX = b where A and b are the matrix and the rhs. Note that when a rhs is provided, refX is not available

146 {
147 // Check if a reference solution is provided
148 if (m_hasrefX) return m_refX;
149
150 std::string lhs_file;
151 lhs_file = m_folder + "/" + m_matname + "_x.mtx";
152 m_hasrefX = Fileexists(lhs_file);
153 if (m_hasrefX)
154 {
156 m_hasrefX = loadMarketVector(m_refX, lhs_file);
157 }
158 else
159 m_refX.resize(0);
160 return m_refX;
161 }
VectorType m_refX
Definition MatrixMarketIterator.h:233
bool Fileexists(std::string file)
Definition MatrixMarketIterator.h:173
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void resize(Index rows, Index cols)
Definition PlainObjectBase.h:279
Index cols() const
Definition SparseMatrix.h:138
bool loadMarketVector(VectorType &vec, const std::string &filename)
Definition MarketIO.h:194

References Eigen::SparseMatrix< _Scalar, _Options, _StorageIndex >::cols(), Eigen::MatrixMarketIterator< Scalar >::Fileexists(), Eigen::loadMarketVector(), Eigen::MatrixMarketIterator< Scalar >::m_folder, Eigen::MatrixMarketIterator< Scalar >::m_hasrefX, Eigen::MatrixMarketIterator< Scalar >::m_mat, Eigen::MatrixMarketIterator< Scalar >::m_matname, Eigen::MatrixMarketIterator< Scalar >::m_refX, and Eigen::PlainObjectBase< Derived >::resize().

+ Here is the call graph for this function:

◆ rhs()

template<typename Scalar >
VectorType & Eigen::MatrixMarketIterator< Scalar >::rhs ( )
inline

Return the right hand side corresponding to the current matrix. If the rhs file is not provided, a random rhs is generated

114 {
115 // Get the right hand side
116 if (m_hasRhs) return m_rhs;
117
118 std::string rhs_file;
119 rhs_file = m_folder + "/" + m_matname + "_b.mtx"; // The pattern is matname_b.mtx
120 m_hasRhs = Fileexists(rhs_file);
121 if (m_hasRhs)
122 {
124 m_hasRhs = loadMarketVector(m_rhs, rhs_file);
125 }
126 if (!m_hasRhs)
127 {
128 // Generate a random right hand side
129 if (!m_matIsLoaded) this->matrix();
132 m_rhs = m_mat * m_refX;
133 m_hasrefX = true;
134 m_hasRhs = true;
135 }
136 return m_rhs;
137 }
MatrixType & matrix()
Definition MatrixMarketIterator.h:74
VectorType m_rhs
Definition MatrixMarketIterator.h:232
Derived & setRandom(Index size)
Definition Random.h:151

References Eigen::SparseMatrix< _Scalar, _Options, _StorageIndex >::cols(), Eigen::MatrixMarketIterator< Scalar >::Fileexists(), Eigen::loadMarketVector(), Eigen::MatrixMarketIterator< Scalar >::m_folder, Eigen::MatrixMarketIterator< Scalar >::m_hasrefX, Eigen::MatrixMarketIterator< Scalar >::m_hasRhs, Eigen::MatrixMarketIterator< Scalar >::m_mat, Eigen::MatrixMarketIterator< Scalar >::m_matIsLoaded, Eigen::MatrixMarketIterator< Scalar >::m_matname, Eigen::MatrixMarketIterator< Scalar >::m_refX, Eigen::MatrixMarketIterator< Scalar >::m_rhs, Eigen::MatrixMarketIterator< Scalar >::matrix(), Eigen::PlainObjectBase< Derived >::resize(), and Eigen::PlainObjectBase< Derived >::setRandom().

+ Here is the call graph for this function:

◆ sym()

template<typename Scalar >
int Eigen::MatrixMarketIterator< Scalar >::sym ( )
inline

Member Data Documentation

◆ m_curs_id

template<typename Scalar >
struct dirent* Eigen::MatrixMarketIterator< Scalar >::m_curs_id
protected

◆ m_folder

◆ m_folder_id

◆ m_hasrefX

◆ m_hasRhs

◆ m_isvalid

◆ m_mat

◆ m_matIsLoaded

◆ m_matname

◆ m_refX

◆ m_rhs

template<typename Scalar >
VectorType Eigen::MatrixMarketIterator< Scalar >::m_rhs
protected

◆ m_sym


The documentation for this class was generated from the following file: