43 #ifndef __H__UG__LIB_ALGEBRA__MATRIX_IO_MTX_H
44 #define __H__UG__LIB_ALGEBRA__MATRIX_IO_MTX_H
48 #include <boost/algorithm/string.hpp>
49 #include <boost/lexical_cast.hpp>
175 void set_mat_dims(
size_t rows,
size_t cols,
size_t lines );
224 template<
typename matrix_type>
229 "MatrixMarket matrix dimensions seem not be reasonable: (m, n, nnz)=("
244 for(
size_t i = 0; i <
m_lines; i++ ) {
247 matrix( x - 1, y - 1 ) = val;
249 matrix( y - 1, x - 1 ) = val;
251 matrix( y - 1, x - 1 ) = -val;
255 UG_THROW(
"Other than sparse MatrixMarket matrices are not yet implemented." );
286 template<
typename matrix_type>
288 std::string comment=
"%Generated with ug4." )
299 open_file( std::ios_base::out | std::ios_base::app );
302 if ( !comment.empty() ) {
303 if ( comment.find_first_of(
'%' ) != 0 ) {
304 UG_WARNING(
"Given comment did not start with '%'. Prepending it to make it valid." );
305 comment.insert( 0,
"%" );
315 for (
size_t col = 0; col <
m_cols; col++ ) {
316 for (
size_t row = 0; row < rowIndexPerCol.at(col).size(); row++ ) {
318 write_entry( rowIndexPerCol.at(col).at(row) + 1, col + 1,
319 matrix(rowIndexPerCol.at(col).at(row), col) );
328 void open_file( std::ios_base::openmode mode=std::ios_base::in );
400 template<
typename matrix_type>
406 size_t rows = matrix.num_rows();
407 size_t cols = matrix.num_cols();
408 size_t offDiagEntries = 0;
409 size_t diagEntries = 0;
415 std::vector< std::vector<size_t> > rowIndexPerCol;
416 rowIndexPerCol.resize(cols);
417 bool isSymmetric =
true;
419 bool changed =
false;
420 for (
size_t r = 0; r < rows; r++ ) {
421 for (
typename matrix_type::const_row_iterator conn = matrix.begin_row(r);
422 conn != matrix.end_row(r); ++conn ) {
423 if ( conn.value() != 0.0 ) {
425 if ( isSymmetric || isSkew ) {
426 if ( conn.index() <= r ) {
427 rowIndexPerCol.at( conn.index() ).push_back(r);
430 rowIndexPerCol.at( conn.index() ).push_back(r);
434 (conn.index() == r) ? diagEntries++ : offDiagEntries++ ;
437 if ( r != conn.index() ) {
438 if ( matrix(r, conn.index() ) != matrix( conn.index(), r ) ) {
444 if ( matrix(r, conn.index() ) != -1.0 * matrix( conn.index(), r ) ) {
457 rowIndexPerCol.clear();
458 rowIndexPerCol.resize( cols );
468 UG_ASSERT( ( ( isSymmetric && !isSkew ) ||
469 ( isSkew && !isSymmetric ) ||
470 ( !isSkew && !isSymmetric ) ),
471 "Error on detecting symmetry of matrix. (skew:" << isSkew
472 <<
" sym:" << isSymmetric <<
")" );
479 entries = ( offDiagEntries / 2 )+ diagEntries;
481 }
else if ( isSkew ) {
482 entries = ( offDiagEntries / 2 ) + diagEntries;
485 entries = offDiagEntries + diagEntries;
492 return rowIndexPerCol;
Type representation for MatrixMarket matrix exchange files.
Definition: mm_type_code.h:60
void set_class_type(int type)
Sets a new class type from an enum value.
Definition: mm_type_code.h:202
bool is_skew_symmetric() const
Tells whether MMTypeCode is skew-symmetric.
Definition: mm_type_code.h:192
void set_numeric_type(int type)
Sets a new numeric type from an enum value.
Definition: mm_type_code.h:236
void set_algebraic_type(int type)
Sets a new algebraic type from an enum value.
Definition: mm_type_code.h:281
bool is_symmetric() const
Tells whether MMTypeCode is symmetric.
Definition: mm_type_code.h:184
bool is_sparse() const
Tells whether MMTypeCode is sparse.
Definition: mm_type_code.h:158
@ REAL
Definition: mm_type_code.h:78
@ GENERAL
Definition: mm_type_code.h:90
@ SYMMETRIC
Definition: mm_type_code.h:91
@ SKEW
Definition: mm_type_code.h:92
@ COORDINATE
Definition: mm_type_code.h:68
Representation of a matrix exchange file format.
Definition: matrix_io.h:66
Generic matrix I/O functionality.
Definition: matrix_io.h:126
@ EXISTING
Only existing files are valid files (i.e. non-existing file result in error)
Definition: matrix_io.h:131
Provides I/O functionality for MatrixMarket exchange file format.
Definition: matrix_io_mtx.h:84
void read_into(matrix_type &matrix)
Reads data from associated exchange file into ug matrix.
Definition: matrix_io_mtx.h:225
size_t m_lines
Definition: matrix_io_mtx.h:107
~MatrixIOMtx()
Destructor.
Definition: matrix_io_mtx.cpp:65
size_t m_firstDataLine
Line number of the first data line (0 based)
Definition: matrix_io_mtx.h:93
size_t m_rows
Definition: matrix_io_mtx.h:101
void query_matrix_type()
Parses banner of MatrixMarket file.
Definition: matrix_io_mtx.cpp:152
std::fstream m_matFileStream
Definition: matrix_io_mtx.h:91
MatrixFileType m_matFileType
Definition: matrix_io_mtx.h:96
MatrixIOMtx()
Default constructor.
Definition: matrix_io_mtx.cpp:40
size_t get_num_lines() const
Retrieves number of data lines as specified in the exchange file.
Definition: matrix_io_mtx.cpp:119
void open_file(std::ios_base::openmode mode=std::ios_base::in)
Definition: matrix_io_mtx.cpp:132
void query_matrix_characteristics()
Retreives matrix size and number non-zeros from exchange file.
Definition: matrix_io_mtx.cpp:199
void write_banner()
Writes the banner to the associated MatrixMarket file.
Definition: matrix_io_mtx.cpp:267
MMTypeCode m_mmTypeCode
Characteristics of MatrixMarket matrix.
Definition: matrix_io_mtx.h:98
void write_from(matrix_type &matrix, std::string comment="%Generated with ug4.")
Writes a ug matrix into the associated MatrixMarket exchange file.
Definition: matrix_io_mtx.h:287
std::vector< std::vector< size_t > > determine_matrix_characteristics(const matrix_type &matrix)
Determines characteristics of given matrix.
Definition: matrix_io_mtx.h:401
void set_mat_dims(size_t rows, size_t cols, size_t lines)
Sets the size of the matrix and number of data lines.
Definition: matrix_io_mtx.cpp:97
void close_file()
Definition: matrix_io_mtx.cpp:144
std::string get_mat_file_name() const
Retreive associated exchange file path and name.
Definition: matrix_io_mtx.cpp:92
void write_entry(size_t m, size_t n, CPUAlgebra::matrix_type::value_type val)
Appends a data line to the associated file.
Definition: matrix_io_mtx.cpp:287
void set_mat_file_name(std::string mFile, int openMode=MatrixIO::EXISTING)
Sets associated file to another one.
Definition: matrix_io_mtx.cpp:73
size_t get_num_rows() const
Retrieves number of rows as specified in the exchange file.
Definition: matrix_io_mtx.cpp:109
void read_entry(size_t *m, size_t *n, CPUAlgebra::matrix_type::value_type *val)
Reads and parses the next data line.
Definition: matrix_io_mtx.cpp:241
std::string * m_pMatFileName
Definition: matrix_io_mtx.h:88
size_t get_num_cols() const
Retrieves number of columns as specified in the exchange file.
Definition: matrix_io_mtx.cpp:114
size_t m_cols
Definition: matrix_io_mtx.h:104
bool is_sparse() const
Tells whether the associated exchange file holds a sparse matrix.
Definition: matrix_io_mtx.cpp:124
Wrapper for sequential matrices to handle them in parallel.
Definition: parallel_matrix.h:65
#define UG_ASSERT(expr, msg)
Definition: assert.h:70
#define UG_THROW(msg)
Definition: error.h:57
#define UG_WARNING(msg)
Definition: log.h:328
#define PROFILE_FUNC()
Definition: profiler.h:257
T value_type
Definition: sparsematrix_interface.h:2