diff --git a/unsupported/Eigen/src/SparseExtra/MarketIO.h b/unsupported/Eigen/src/SparseExtra/MarketIO.h index cdc14f86e..9dbd0488d 100644 --- a/unsupported/Eigen/src/SparseExtra/MarketIO.h +++ b/unsupported/Eigen/src/SparseExtra/MarketIO.h @@ -12,38 +12,38 @@ #define EIGEN_SPARSE_MARKET_IO_H #include +#include namespace Eigen { namespace internal { - template - inline bool GetMarketLine (std::stringstream& line, Index& M, Index& N, Index& i, Index& j, Scalar& value) + template + inline void GetMarketLine (const char* line, StorageIndex& i, StorageIndex& j, Scalar& value) { - line >> i >> j >> value; - i--; - j--; - if(i>=0 && j>=0 && i> i >> j >> value; } - template - inline bool GetMarketLine (std::stringstream& line, Index& M, Index& N, Index& i, Index& j, std::complex& value) + + template<> inline void GetMarketLine (const char* line, int& i, int& j, float& value) + { std::sscanf(line, "%d %d %g", &i, &j, &value); } + + template<> inline void GetMarketLine (const char* line, int& i, int& j, double& value) + { std::sscanf(line, "%d %d %lg", &i, &j, &value); } + + template<> inline void GetMarketLine (const char* line, int& i, int& j, std::complex& value) + { std::sscanf(line, "%d %d %g %g", &i, &j, &numext::real_ref(value), &numext::imag_ref(value)); } + + template<> inline void GetMarketLine (const char* line, int& i, int& j, std::complex& value) + { std::sscanf(line, "%d %d %lg %lg", &i, &j, &numext::real_ref(value), &numext::imag_ref(value)); } + + template + inline void GetMarketLine (const char* line, StorageIndex& i, StorageIndex& j, std::complex& value) { + std::stringstream sline(line); Scalar valR, valI; - line >> i >> j >> valR >> valI; - i--; - j--; - if(i>=0 && j>=0 && i(valR, valI); - return true; - } - else - return false; + sline >> i >> j >> valR >> valI; + value = std::complex(valR,valI); } template @@ -81,13 +81,13 @@ namespace internal } } - template - inline void PutMatrixElt(Scalar value, int row, int col, std::ofstream& out) + template + inline void PutMatrixElt(Scalar value, StorageIndex row, StorageIndex col, std::ofstream& out) { out << row << " "<< col << " " << value << "\n"; } - template - inline void PutMatrixElt(std::complex value, int row, int col, std::ofstream& out) + template + inline void PutMatrixElt(std::complex value, StorageIndex row, StorageIndex col, std::ofstream& out) { out << row << " " << col << " " << value.real() << " " << value.imag() << "\n"; } @@ -133,17 +133,20 @@ template bool loadMarket(SparseMatrixType& mat, const std::string& filename) { typedef typename SparseMatrixType::Scalar Scalar; - typedef typename SparseMatrixType::Index Index; + typedef typename SparseMatrixType::StorageIndex StorageIndex; std::ifstream input(filename.c_str(),std::ios::in); if(!input) return false; + + char rdbuffer[4096]; + input.rdbuf()->pubsetbuf(rdbuffer, 4096); const int maxBuffersize = 2048; char buffer[maxBuffersize]; bool readsizes = false; - typedef Triplet T; + typedef Triplet T; std::vector elements; Index M(-1), N(-1), NNZ(-1); @@ -154,33 +157,36 @@ bool loadMarket(SparseMatrixType& mat, const std::string& filename) //NOTE An appropriate test should be done on the header to get the symmetry if(buffer[0]=='%') continue; - - std::stringstream line(buffer); - + if(!readsizes) { + std::stringstream line(buffer); line >> M >> N >> NNZ; if(M > 0 && N > 0 && NNZ > 0) { readsizes = true; - //std::cout << "sizes: " << M << "," << N << "," << NNZ << "\n"; mat.resize(M,N); mat.reserve(NNZ); } } else { - Index i(-1), j(-1); + StorageIndex i(-1), j(-1); Scalar value; - if( internal::GetMarketLine(line, M, N, i, j, value) ) + internal::GetMarketLine(buffer, i, j, value); + + i--; + j--; + if(i>=0 && j>=0 && i void sparse_extra(const SparseMatrixType& re } +template +void check_marketio() +{ + typedef Matrix DenseMatrix; + Index rows = internal::random(1,100); + Index cols = internal::random(1,100); + SparseMatrixType m1, m2; + m1 = DenseMatrix::Random(rows, cols).sparseView(); + saveMarket(m1, "sparse_extra.mtx"); + loadMarket(m2, "sparse_extra.mtx"); + VERIFY_IS_APPROX(m1,m2); +} + void test_sparse_extra() { for(int i = 0; i < g_repeat; i++) { @@ -143,5 +156,15 @@ void test_sparse_extra() CALL_SUBTEST_3( (sparse_product >()) ); CALL_SUBTEST_3( (sparse_product >()) ); + + CALL_SUBTEST_4( (check_marketio >()) ); + CALL_SUBTEST_4( (check_marketio >()) ); + CALL_SUBTEST_4( (check_marketio,ColMajor,int> >()) ); + CALL_SUBTEST_4( (check_marketio,ColMajor,int> >()) ); + CALL_SUBTEST_4( (check_marketio >()) ); + CALL_SUBTEST_4( (check_marketio >()) ); + CALL_SUBTEST_4( (check_marketio,ColMajor,long int> >()) ); + CALL_SUBTEST_4( (check_marketio,ColMajor,long int> >()) ); + TEST_SET_BUT_UNUSED_VARIABLE(s); } }