📄 sparsemat.h
字号:
// ENERGY211/CME211//// sparsemat.h - header file for sparsemat project//#ifndef CLASS_SPARSEMAT#define CLASS_SPARSEMAT#include <map>#include <set>#include <iostream>#include <ostream>#include <stdexcept> // Need this to use std::runtime_error // class, for reporting errorstypedef std::map<std::pair<int, int>, double > matrix_map;typedef matrix_map::iterator mat_iter;typedef matrix_map::const_iterator mat_citer;typedef std::map<int, std::set<int> > iset_map;typedef iset_map::iterator iset_iter;typedef iset_map::const_iterator iset_citer;typedef std::set<int>::iterator elt_iter;typedef std::set<int>::const_iterator elt_citer;class SparseMatrix {public: // Constructor SparseMatrix( int rows = 0, int cols = 0 ); // Copy constructor SparseMatrix( const SparseMatrix& A ); enum ErrorCode { ERROR_SIZE_MISMATCH = 1, ERROR_INVALID_INDEX = 2 } ; ~SparseMatrix(); int get_rows() const { return m_rows; } int get_cols() const { return m_cols; } void Identity( int rows, int cols = 0 ); SparseMatrix operator+( const SparseMatrix& B ) const; SparseMatrix operator-() const; SparseMatrix operator-( const SparseMatrix& B ) const; SparseMatrix operator*( const SparseMatrix& B ) const; SparseMatrix operator*( double s ) const; SparseMatrix operator/( double s ) const; SparseMatrix Transpose() const; SparseMatrix& operator=( const SparseMatrix& A ); SparseMatrix& operator+=( const SparseMatrix& A ); SparseMatrix& operator-=( const SparseMatrix& A ); SparseMatrix& operator*=( double s ); SparseMatrix& operator/=( double s ); void Squeeze( double tol = 0.0 ); int nnz() const; elt_citer get_row_begin( int i ) const; elt_citer get_row_end( int i ) const; elt_citer get_col_begin( int i ) const; elt_citer get_col_end( int i ) const; double& operator()( int i, int j ); double operator()( int i, int j ) const; SparseMatrix TriU( int diag = 0 ) const; SparseMatrix TriL( int diag = 0 ) const; private: int m_rows; int m_cols; matrix_map m_data; iset_map m_row_indices; iset_map m_col_indices; double Get( int i, int j ) const; void Set( int i, int j, double x ); void SetSize( int rows, int cols ); // Leave these alone! They are used for error reporting static void ReportError( ErrorCode errorcode ); static char *ErrorMessages[];} ;std::ostream& operator<<( std::ostream& os, const SparseMatrix& A );SparseMatrix operator*( double s, const SparseMatrix& A );#endif // CLASS_SPARSEMAT
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -