11 #ifndef EIGEN_BROWSE_MATRICES_H
12 #define EIGEN_BROWSE_MATRICES_H
41 template <
typename Scalar>
45 typedef Matrix<Scalar,Dynamic,1> VectorType;
46 typedef SparseMatrix<Scalar,ColMajor> MatrixType;
49 MatrixMarketIterator(
const std::string folder):m_sym(0),m_isvalid(
false),m_matIsLoaded(
false),m_hasRhs(
false),m_hasrefX(
false),m_folder(folder)
51 m_folder_id = opendir(folder.c_str());
54 std::cerr <<
"The provided Matrix folder could not be opened \n\n";
62 if (m_folder_id) closedir(m_folder_id);
67 m_matIsLoaded =
false;
73 inline operator bool()
const {
return m_isvalid;}
79 if (m_matIsLoaded)
return m_mat;
81 std::string matrix_file = m_folder +
"/" + m_matname +
".mtx";
82 if ( !loadMarket(m_mat, matrix_file))
84 m_matIsLoaded =
false;
89 if (m_sym != NonSymmetric)
93 m_mat = B.template selfadjointView<Lower>();
104 if (m_hasRhs)
return m_rhs;
106 std::string rhs_file;
107 rhs_file = m_folder +
"/" + m_matname +
"_b.mtx";
108 m_hasRhs = Fileexists(rhs_file);
111 m_rhs.resize(m_mat.cols());
112 m_hasRhs = loadMarketVector(m_rhs, rhs_file);
117 if (!m_matIsLoaded) this->
matrix();
118 m_refX.resize(m_mat.cols());
120 m_rhs = m_mat * m_refX;
136 if (m_hasrefX)
return m_refX;
138 std::string lhs_file;
139 lhs_file = m_folder +
"/" + m_matname +
"_x.mtx";
140 m_hasrefX = Fileexists(lhs_file);
143 m_refX.resize(m_mat.cols());
144 m_hasrefX = loadMarketVector(m_refX, lhs_file);
149 inline std::string& matname() {
return m_matname; }
151 inline int sym() {
return m_sym; }
153 inline bool hasRhs() {
return m_hasRhs; }
154 inline bool hasrefX() {
return m_hasrefX; }
158 inline bool Fileexists(std::string file)
160 std::ifstream file_id(file.c_str());
161 if (!file_id.good() )
172 void Getnextvalidmatrix( )
176 while ( (m_curs_id = readdir(m_folder_id)) != NULL) {
179 curfile = m_folder +
"/" + m_curs_id->d_name;
181 if (m_curs_id->d_type == DT_DIR)
continue;
187 bool isvector,iscomplex=
false;
188 if(!getMarketHeader(curfile,m_sym,iscomplex,isvector))
continue;
189 if(isvector)
continue;
192 if(internal::is_same<Scalar, std::complex<float> >::value || internal::is_same<Scalar, std::complex<double> >::value)
197 if(internal::is_same<Scalar, float>::value || internal::is_same<Scalar, double>::value)
203 std::string filename = m_curs_id->d_name;
204 m_matname = filename.substr(0, filename.length()-4);
207 size_t found = m_matname.find(
"SPD");
208 if( (found!=std::string::npos) && (m_sym != NonSymmetric) )
219 std::string m_matname;
224 std::string m_folder;
226 struct dirent *m_curs_id;
Iterator to browse matrices from a specified folder.
Definition: MatrixMarketIterator.h:42
VectorType & rhs()
Definition: MatrixMarketIterator.h:101
MatrixType & matrix()
Definition: MatrixMarketIterator.h:76
VectorType & refX()
Definition: MatrixMarketIterator.h:133