CppNoddy  0.90
Public Member Functions | Friends | List of all members
CppNoddy::SparseMatrix< _Type > Class Template Reference

A matrix class that constructs a SPARSE matrix as a row major std::vector of SparseVectors. More...

#include <SparseMatrix.h>

Inheritance diagram for CppNoddy::SparseMatrix< _Type >:
CppNoddy::Matrix_base< _Type >

Public Member Functions

 SparseMatrix (const std::size_t &rows, const std::size_t &cols)
 Construct with a set number of rows. More...
 
 SparseMatrix (const SparseMatrix< _Type > &source, const std::vector< std::size_t > &source_rows)
 Construct from a row permutation of another sparse matrix. More...
 
 SparseMatrix (const SparseMatrix &source)
 Copy constructor. More...
 
SparseMatrixoperator= (const SparseMatrix &source)
 Assignment operator. More...
 
 ~SparseMatrix ()
 Default d-tor. More...
 
void blank ()
 Blank the contents of this matrix. More...
 
const _Type & operator() (const std::size_t &row, const std::size_t &col) const
 Access operator. More...
 
_Type & operator() (const std::size_t &row, const std::size_t &col)
 Access operator. More...
 
const _Type & get (const std::size_t &row, const std::size_t &col) const
 Access operator. More...
 
_Type & set (const std::size_t &row, const std::size_t &col)
 Access operator. More...
 
SparseVector< _Type > get_row (const std::size_t &row) const
 
void set_row (const std::size_t &row, const SparseVector< _Type > &row_vector)
 
std::size_t nrows () const
 Get the number of rows in the matrix. More...
 
std::size_t ncols () const
 Get the number of columns in the matrix. More...
 
std::size_t nelts () const
 Get the number of (non-zero) elements in the matrix. More...
 
void scale (const _Type &mult)
 Scale the matrix by a scalar. More...
 
void transpose ()
 Transpose the matrix in place. More...
 
double one_norm () const
 
double two_norm () const
 
double inf_norm () const
 
double frob_norm () const
 
DenseVector< _Type > multiply (const DenseVector< _Type > &X) const
 Right-multiply by a DENSE vector. More...
 
void dump () const
 Output the contents of the matrix to std::cout. More...
 
void dump (std::string filename, int precision=10) const
 A simple method for dumping the matrix to a file. More...
 
SparseVector< _Type > & operator[] (const std::size_t &row)
 Operator overloading for ROW access. More...
 
const SparseVector< _Type > & operator[] (const std::size_t &row) const
 Operator overloading for ROW access. More...
 
std::size_t nelts_in_row (int row)
 The number of non-zero elements in a specified row. More...
 
std::size_t max_in_col (const std::size_t &col, const std::size_t &row_min, const std::size_t &row_max) const
 Find the maximum entry in a column – used in the native solver. More...
 
void row_swap (const std::size_t &row1, const std::size_t &row2)
 Swap two rows in the matrix – used in the native solver. More...
 
- Public Member Functions inherited from CppNoddy::Matrix_base< _Type >
 Matrix_base ()
 An empty constructor. More...
 
virtual ~Matrix_base ()
 

Friends

template<typename _SystemType >
class SparseLinearSystem
 

Detailed Description

template<typename _Type>
class CppNoddy::SparseMatrix< _Type >

A matrix class that constructs a SPARSE matrix as a row major std::vector of SparseVectors.

Definition at line 36 of file SparseMatrix.h.

Constructor & Destructor Documentation

template<typename _Type >
CppNoddy::SparseMatrix< _Type >::SparseMatrix ( const std::size_t &  rows,
const std::size_t &  cols 
)

Construct with a set number of rows.

Parameters
rowsThe number of rows in the matrix
colsThe number of columns in the matrix

Definition at line 17 of file SparseMatrix.cpp.

18  : Matrix_base<_Type>(), NR( rows ), NC( cols )
19  {
20  MATRIX.reserve( NR );
21  SparseVector<_Type> sparse_row( NC );
22  for ( std::size_t i = 0; i < NR; ++i )
23  {
24  MATRIX.push_back( sparse_row );
25  }
26  }
template<typename _Type >
CppNoddy::SparseMatrix< _Type >::SparseMatrix ( const SparseMatrix< _Type > &  source,
const std::vector< std::size_t > &  source_rows 
)

Construct from a row permutation of another sparse matrix.

Parameters
source_rowsDefines the permutation, row i of this matrix is row source_rows[i] of the source

Definition at line 29 of file SparseMatrix.cpp.

References CppNoddy::SparseMatrix< _Type >::get_row().

29  :
30  Matrix_base<_Type>(), NR( source.nrows() ), NC( source.ncols() )
31  {
32  MATRIX.reserve( NR );
33  for ( std::size_t i = 0; i < NR; ++i )
34  {
35  MATRIX.push_back( source.get_row( source_rows[i] ) );
36  }
37  }
double source(const double &x, const double &y, const double &t)
Definition: IBVP_linear.cpp:25
template<typename _Type >
CppNoddy::SparseMatrix< _Type >::SparseMatrix ( const SparseMatrix< _Type > &  source)

Copy constructor.

Parameters
sourceThe source object to be copied

Definition at line 40 of file SparseMatrix.cpp.

References CppNoddy::Example::source().

40  :
41  Matrix_base<_Type>( source )
42  {
43  *this = source;
44  }
double source(const double &x, const double &y, const double &t)
Definition: IBVP_linear.cpp:25
template<typename _Type>
CppNoddy::SparseMatrix< _Type >::~SparseMatrix ( )
inline

Default d-tor.

Definition at line 62 of file SparseMatrix.h.

62 {}

Member Function Documentation

template<typename _Type>
void CppNoddy::SparseMatrix< _Type >::blank ( )
inline

Blank the contents of this matrix.

Definition at line 65 of file SparseMatrix.h.

References CppNoddy::SparseMatrix< _Type >::operator()().

66  {
67  MATRIX.clear();
68  MATRIX.reserve( NR );
69  SparseVector<_Type> sparse_row( NC );
70  for ( std::size_t i = 0; i < NR; ++i )
71  {
72  MATRIX.push_back( sparse_row );
73  }
74  }
template<typename _Type >
void CppNoddy::SparseMatrix< _Type >::dump ( ) const
virtual

Output the contents of the matrix to std::cout.

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 298 of file SparseMatrix.cpp.

Referenced by CppNoddy::SparseMatrix< _Type >::dump(), main(), and CppNoddy::SparseMatrix< _Type >::set_row().

299  {
300  std::cout << "SPARSE mtx size = " << NR << " x sparse \n";
301  std::cout.precision( 4 );
302  std::cout << "- start matrix \n";
303  for ( std::size_t row = 0; row < NR; ++row )
304  {
305  std::cout << " row " << row << " : ";
306  MATRIX[ row ].dump();
307  std::cout << " \n";
308  }
309  std::cout << "- end matrix \n";
310  }
template<typename _Type>
void CppNoddy::SparseMatrix< _Type >::dump ( std::string  filename,
int  precision = 10 
) const
inline

A simple method for dumping the matrix to a file.

Parameters
filenameThe filename to write the data to (will overwrite)
precisionPrecision of the output strings

Definition at line 137 of file SparseMatrix.h.

References CppNoddy::SparseMatrix< _Type >::dump(), and CppNoddy::SparseMatrix< _Type >::operator[]().

138  {
139  std::ofstream dump;
140  dump.open( filename.c_str() );
141  dump.precision( precision );
142  dump.setf( std::ios::showpoint );
143  dump.setf( std::ios::showpos );
144  dump.setf( std::ios::scientific );
145  for ( std::size_t row = 0; row < NR; ++row )
146  {
147  for ( citer pos = MATRIX[row].begin(); pos != MATRIX[row].end(); ++pos )
148  {
149  dump << row << " " << pos -> first << " " << pos -> second << "\n";
150  }
151  }
152  dump << "\n";
153  dump.close();
154  }
void dump() const
Output the contents of the matrix to std::cout.
template<typename _Type >
double CppNoddy::SparseMatrix< _Type >::frob_norm ( ) const
virtual
Returns
The sum of the two_norm of all rows

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 141 of file SparseMatrix.cpp.

Referenced by CppNoddy::SparseMatrix< _Type >::set_row().

142  {
143  double sum ( 0.0 );
144  for ( std::size_t row = 0; row < NR; ++row )
145  {
146  sum += MATRIX[ row ].two_norm();
147  }
148  return sum;
149  }
template<typename _Type >
const _Type & CppNoddy::SparseMatrix< _Type >::get ( const std::size_t &  row,
const std::size_t &  col 
) const
inlinevirtual

Access operator.

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 252 of file SparseMatrix.h.

References CppNoddy::SparseMatrix< _Type >::operator()().

253  {
254  return this -> operator() ( row, col );
255  }
const _Type & operator()(const std::size_t &row, const std::size_t &col) const
Access operator.
Definition: SparseMatrix.h:240
template<typename _Type>
SparseVector<_Type> CppNoddy::SparseMatrix< _Type >::get_row ( const std::size_t &  row) const
inline

Definition at line 85 of file SparseMatrix.h.

Referenced by CppNoddy::SparseMatrix< _Type >::SparseMatrix().

86  {
87  return MATRIX[row];
88  }
template<typename _Type >
double CppNoddy::SparseMatrix< _Type >::inf_norm ( ) const
virtual
Returns
The maximum inf_norm of all rows

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 130 of file SparseMatrix.cpp.

Referenced by CppNoddy::SparseMatrix< _Type >::set_row().

131  {
132  double max( 0.0 );
133  for ( std::size_t row = 0; row < NR; ++row )
134  {
135  max = std::max( max, MATRIX[ row ].inf_norm() );
136  }
137  return max;
138  }
double inf_norm() const
template<typename _Type >
std::size_t CppNoddy::SparseMatrix< _Type >::max_in_col ( const std::size_t &  col,
const std::size_t &  row_min,
const std::size_t &  row_max 
) const

Find the maximum entry in a column – used in the native solver.

Parameters
colThe column to search through
row_minThe start row for the search
row_maxThe end row for the search (NOT INCLUSIVE)

Definition at line 69 of file SparseMatrix.cpp.

References CppNoddy::SparseMatrix< _Type >::nrows().

Referenced by CppNoddy::SparseMatrix< _Type >::nelts_in_row().

71  {
72  double maxelt( 0.0 );
73  // return outside of the array as default
74  std::size_t index = nrows();
75  for ( std::size_t row = row_min; row < row_max ; ++row )
76  {
77  // only bother looking up entries of rows with a first
78  // element in a column less than the one we're looking at
79  if ( MATRIX[ row ].begin() -> first <= col )
80  {
81  const double elt( std::abs( MATRIX[ row ].get( col ) ) );
82  if ( elt >= maxelt )
83  {
84  maxelt = elt;
85  index = row;
86  }
87  }
88  }
89  return index;
90  }
std::size_t nrows() const
Get the number of rows in the matrix.
Definition: SparseMatrix.h:291
template<typename _Type >
DenseVector< _Type > CppNoddy::SparseMatrix< _Type >::multiply ( const DenseVector< _Type > &  X) const
virtual

Right-multiply by a DENSE vector.

Parameters
XThe DENSE vector to be multiplied by
Returns
A DENSE vector of the result of the multiplication

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 152 of file SparseMatrix.cpp.

References CppNoddy::Utility::imag(), CppNoddy::SparseMatrix< _Type >::nelts(), and CppNoddy::Utility::real().

Referenced by CppNoddy::SparseMatrix< _Type >::set_row().

153  {
154  throw ExceptionRuntime( "SparseMatrix.multiply has not been implemented" );
155  }
template<typename _Type >
std::size_t CppNoddy::SparseMatrix< _Type >::ncols ( ) const
inlinevirtual

Get the number of columns in the matrix.

Returns
The number of columns

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 297 of file SparseMatrix.h.

Referenced by CppNoddy::SparseMatrix< _Type >::set_row().

298  {
299  return NC;
300  }
template<typename _Type >
std::size_t CppNoddy::SparseMatrix< _Type >::nelts ( ) const
virtual

Get the number of (non-zero) elements in the matrix.

Returns
The number of (non-zero) elements

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 58 of file SparseMatrix.cpp.

Referenced by main(), CppNoddy::SparseMatrix< _Type >::multiply(), and CppNoddy::SparseMatrix< _Type >::set_row().

59  {
60  std::size_t num_of_elts( 0 );
61  for ( std::size_t row = 0; row < NR; ++row )
62  {
63  num_of_elts += MATRIX[ row ].nelts();
64  }
65  return num_of_elts;
66  }
template<typename _Type>
std::size_t CppNoddy::SparseMatrix< _Type >::nelts_in_row ( int  row)
inline

The number of non-zero elements in a specified row.

Parameters
rowThe row index to return the number of non-zero elts for

Definition at line 205 of file SparseMatrix.h.

References CppNoddy::SparseMatrix< _Type >::max_in_col(), and CppNoddy::SparseMatrix< _Type >::row_swap().

206  {
207  return MATRIX[row].nelts();
208  }
template<typename _Type >
std::size_t CppNoddy::SparseMatrix< _Type >::nrows ( ) const
inlinevirtual

Get the number of rows in the matrix.

Returns
The number of rows

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 291 of file SparseMatrix.h.

Referenced by CppNoddy::SparseMatrix< _Type >::max_in_col(), and CppNoddy::SparseMatrix< _Type >::set_row().

292  {
293  return NR;
294  }
template<typename _Type >
double CppNoddy::SparseMatrix< _Type >::one_norm ( ) const
virtual
Returns
The maximum one_norm of all rows

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 108 of file SparseMatrix.cpp.

Referenced by CppNoddy::SparseMatrix< _Type >::set_row().

109  {
110  double max( 0.0 );
111  for ( std::size_t row = 0; row < NR; ++row )
112  {
113  max = std::max( max, MATRIX[ row ].one_norm() );
114  }
115  return max;
116  }
double one_norm() const
template<typename _Type >
const _Type & CppNoddy::SparseMatrix< _Type >::operator() ( const std::size_t &  row,
const std::size_t &  col 
) const
inlinevirtual

Access operator.

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 240 of file SparseMatrix.h.

Referenced by CppNoddy::SparseMatrix< _Type >::blank(), CppNoddy::SparseMatrix< _Type >::get(), and CppNoddy::SparseMatrix< _Type >::set().

241  {
242  return MATRIX[ row ].get( col );
243  }
template<typename _Type >
_Type & CppNoddy::SparseMatrix< _Type >::operator() ( const std::size_t &  row,
const std::size_t &  col 
)
inlinevirtual

Access operator.

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 246 of file SparseMatrix.h.

247  {
248  return MATRIX[ row ][ col ];
249  }
template<typename _Type >
SparseMatrix< _Type > & CppNoddy::SparseMatrix< _Type >::operator= ( const SparseMatrix< _Type > &  source)
inline

Assignment operator.

Parameters
sourceThe source object for the assignment
Returns
The newly assigned object

Definition at line 47 of file SparseMatrix.cpp.

48  {
49  if ( this == &source )
50  return * this;
51  MATRIX = source.MATRIX;
52  NR = source.NR;
53  NC = source.NC;
54  return *this;
55  }
double source(const double &x, const double &y, const double &t)
Definition: IBVP_linear.cpp:25
template<typename _Type >
SparseVector< _Type > & CppNoddy::SparseMatrix< _Type >::operator[] ( const std::size_t &  row)
inline

Operator overloading for ROW access.

Parameters
rowThe row to access
Returns
The DENSE vector of the row data

Definition at line 264 of file SparseMatrix.h.

Referenced by CppNoddy::SparseMatrix< _Type >::dump().

265  {
266 #ifdef PARANOID
267  if ( ( row > NR ) || ( row < 0 ) )
268  {
269  std::string problem( "The SparseMatrix.get_row has a range error.\n" );
270  throw ExceptionRange( problem, NR, row );
271  }
272 #endif
273  return MATRIX[ row ];
274  }
template<typename _Type >
const SparseVector< _Type > & CppNoddy::SparseMatrix< _Type >::operator[] ( const std::size_t &  row) const
inline

Operator overloading for ROW access.

Parameters
rowThe row to access
Returns
The DENSE vector of the row data

Definition at line 278 of file SparseMatrix.h.

279  {
280 #ifdef PARANOID
281  if ( ( row > NR ) || ( row < 0 ) )
282  {
283  std::string problem( "The SparseMatrix.get_row has a range error.\n" );
284  throw ExceptionRange( problem, NR, row );
285  }
286 #endif
287  return MATRIX[ row ];
288  }
template<typename _Type >
void CppNoddy::SparseMatrix< _Type >::row_swap ( const std::size_t &  row1,
const std::size_t &  row2 
)
inline

Swap two rows in the matrix – used in the native solver.

Parameters
row1The first row to be exchanged
row2The second row to be exchanged

Definition at line 303 of file SparseMatrix.h.

Referenced by CppNoddy::SparseMatrix< _Type >::nelts_in_row().

304  {
305  // actually do the swap, rather than keep a row permutation vector.
306  // std::swap<SparseVector<_Type> > ( matrix[ row1 ], matrix[ row2 ] );
307  MATRIX[ row1 ].swap( MATRIX[ row2 ] );
308  }
template<typename _Type >
void CppNoddy::SparseMatrix< _Type >::scale ( const _Type &  mult)
virtual

Scale the matrix by a scalar.

Parameters
multThe scalar multiplier

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 93 of file SparseMatrix.cpp.

Referenced by main(), and CppNoddy::SparseMatrix< _Type >::set_row().

94  {
95  for ( std::size_t row = 0; row < NR; ++row )
96  {
97  MATRIX[ row ] *= mult;
98  }
99  }
template<typename _Type >
_Type & CppNoddy::SparseMatrix< _Type >::set ( const std::size_t &  row,
const std::size_t &  col 
)
inlinevirtual

Access operator.

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 258 of file SparseMatrix.h.

References CppNoddy::SparseMatrix< _Type >::operator()().

259  {
260  return this -> operator() ( row, col );
261  }
const _Type & operator()(const std::size_t &row, const std::size_t &col) const
Access operator.
Definition: SparseMatrix.h:240
template<typename _Type>
void CppNoddy::SparseMatrix< _Type >::set_row ( const std::size_t &  row,
const SparseVector< _Type > &  row_vector 
)
inline
template<typename _Type >
void CppNoddy::SparseMatrix< _Type >::transpose ( )
virtual

Transpose the matrix in place.

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 102 of file SparseMatrix.cpp.

Referenced by CppNoddy::SparseMatrix< _Type >::set_row().

103  {
104  throw ExceptionRuntime( "SparseMatrix.transpose has not been implemented" );
105  }
template<typename _Type >
double CppNoddy::SparseMatrix< _Type >::two_norm ( ) const
virtual
Returns
The maximum two_norm of all rows

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 119 of file SparseMatrix.cpp.

Referenced by CppNoddy::SparseMatrix< _Type >::set_row().

120  {
121  double max( 0.0 );
122  for ( std::size_t row = 0; row < NR; ++row )
123  {
124  max = std::max( max, MATRIX[ row ].two_norm() );
125  }
126  return max;
127  }
double two_norm() const

Friends And Related Function Documentation

template<typename _Type>
template<typename _SystemType >
friend class SparseLinearSystem
friend

Definition at line 231 of file SparseMatrix.h.


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

© 2012

R.E. Hewitt