CppNoddy  0.92
Loading...
Searching...
No Matches
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::Sequential_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
 Get a row of the matrix. More...
 
void set_row (const std::size_t &row, const SparseVector< _Type > &row_vector)
 Set a row of the matrix. More...
 
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...
 
double one_norm () const
 Transpose the matrix in place. More...
 
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::Sequential_Matrix_base< _Type >
 Sequential_Matrix_base ()
 An empty constructor. More...
 
virtual ~Sequential_Matrix_base ()
 
virtual const _Type & operator() (const std::size_t &row, const std::size_t &col) const =0
 
virtual _Type & operator() (const std::size_t &row, const std::size_t &col)=0
 
virtual const _Type & get (const std::size_t &row, const std::size_t &col) const =0
 
virtual _Type & set (const std::size_t &row, const std::size_t &col)=0
 
virtual std::size_t nrows () const =0
 
virtual std::size_t ncols () const =0
 
virtual std::size_t nelts () const =0
 
virtual void scale (const _Type &mult)=0
 
virtual void dump () const =0
 

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 31 of file SparseMatrix.h.

Constructor & Destructor Documentation

◆ SparseMatrix() [1/3]

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 15 of file SparseMatrix.cpp.

16 : m_nr(rows), m_nc(cols) {
17 m_matrix.reserve(m_nr);
18 SparseVector<_Type> sparse_row(m_nc);
19 for(std::size_t i = 0; i < m_nr; ++i) {
20 m_matrix.push_back(sparse_row);
21 }
22 }

◆ SparseMatrix() [2/3]

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 25 of file SparseMatrix.cpp.

25 :
26 m_nr(source.nrows()), m_nc(source.ncols()) {
27 m_matrix.reserve(m_nr);
28 for(std::size_t i = 0; i < m_nr; ++i) {
29 m_matrix.push_back(source.get_row(source_rows[i]));
30 }
31 }
double source(const double &x, const double &y, const double &t)
Definition: IBVPLinear.cpp:26

◆ SparseMatrix() [3/3]

template<typename _Type >
CppNoddy::SparseMatrix< _Type >::SparseMatrix ( const SparseMatrix< _Type > &  source)

Copy constructor.

Parameters
sourceThe source object to be copied

Definition at line 34 of file SparseMatrix.cpp.

34 {
35 *this = source;
36 }

◆ ~SparseMatrix()

template<typename _Type >
CppNoddy::SparseMatrix< _Type >::~SparseMatrix ( )
inline

Default d-tor.

Definition at line 57 of file SparseMatrix.h.

57{}

Member Function Documentation

◆ blank()

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

Blank the contents of this matrix.

Definition at line 60 of file SparseMatrix.h.

60 {
61 m_matrix.clear();
62 m_matrix.reserve(m_nr);
63 SparseVector<_Type> sparse_row(m_nc);
64 for(std::size_t i = 0; i < m_nr; ++i) {
65 m_matrix.push_back(sparse_row);
66 }
67 }

◆ dump() [1/2]

template<typename _Type >
void CppNoddy::SparseMatrix< _Type >::dump
virtual

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

Implements CppNoddy::Sequential_Matrix_base< _Type >.

Definition at line 197 of file SparseMatrix.cpp.

197 {
198 std::cout << "SPARSE mtx size = " << m_nr << " x sparse \n";
199 std::cout << "- start matrix \n";
200 for(std::size_t row = 0; row < m_nr; ++row) {
201 std::cout << " row " << row << " : ";
202 m_matrix[ row ].dump();
203 std::cout << " \n";
204 }
205 std::cout << "- end matrix \n";
206 }

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

◆ dump() [2/2]

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.

137 {
138 std::ofstream dump;
139 dump.open(filename.c_str());
140 dump.precision(precision);
141 dump.setf(std::ios::showpoint);
142 dump.setf(std::ios::showpos);
143 dump.setf(std::ios::scientific);
144 for(std::size_t row = 0; row < m_nr; ++row) {
145 for(citer pos = m_matrix[row].begin(); pos != m_matrix[row].end(); ++pos) {
146 dump << row << " " << pos -> first << " " << pos -> second << "\n";
147 }
148 }
149 dump << "\n";
150 dump.close();
151 }
void dump() const
Output the contents of the matrix to std::cout.

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

◆ frob_norm()

template<typename _Type >
double CppNoddy::SparseMatrix< _Type >::frob_norm
Returns
The sum of the two_norm of all rows

Definition at line 112 of file SparseMatrix.cpp.

112 {
113 double sum(0.0);
114 for(std::size_t row = 0; row < m_nr; ++row) {
115 sum += m_matrix[ row ].two_norm();
116 }
117 return sum;
118 }

◆ get()

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::Sequential_Matrix_base< _Type >.

Definition at line 239 of file SparseMatrix.h.

239 {
240 return this -> operator()(row, col);
241 }
const _Type & operator()(const std::size_t &row, const std::size_t &col) const
Access operator.
Definition: SparseMatrix.h:229

◆ get_row()

template<typename _Type >
SparseVector< _Type > CppNoddy::SparseMatrix< _Type >::get_row ( const std::size_t &  row) const
inline

Get a row of the matrix.

Parameters
rowThe index of the row to be get
Returns
A sparse vector of values in the row

Definition at line 83 of file SparseMatrix.h.

83 {
84 return m_matrix[row];
85 }

◆ inf_norm()

template<typename _Type >
double CppNoddy::SparseMatrix< _Type >::inf_norm
Returns
The maximum inf_norm of all rows

Definition at line 103 of file SparseMatrix.cpp.

103 {
104 double max(0.0);
105 for(std::size_t row = 0; row < m_nr; ++row) {
106 max = std::max(max, m_matrix[ row ].inf_norm());
107 }
108 return max;
109 }
double inf_norm() const

◆ max_in_col()

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 58 of file SparseMatrix.cpp.

59 {
60 double maxelt(0.0);
61 // return outside of the array as default
62 std::size_t index = nrows();
63 for(std::size_t row = row_min; row < row_max ; ++row) {
64 // only bother looking up entries of rows with a first
65 // element in a column less than the one we're looking at
66 if(m_matrix[ row ].begin() -> first <= col) {
67 const double elt(std::abs(m_matrix[ row ].get(col)));
68 if(elt >= maxelt) {
69 maxelt = elt;
70 index = row;
71 }
72 }
73 }
74 return index;
75 }
const _Type & get(const std::size_t &row, const std::size_t &col) const
Access operator.
Definition: SparseMatrix.h:239
std::size_t nrows() const
Get the number of rows in the matrix.
Definition: SparseMatrix.h:272

◆ multiply()

template<typename _Type >
DenseVector< _Type > CppNoddy::SparseMatrix< _Type >::multiply ( const DenseVector< _Type > &  X) const

Right-multiply by a DENSE vector.

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

◆ ncols()

template<typename _Type >
std::size_t CppNoddy::SparseMatrix< _Type >::ncols
inlinevirtual

Get the number of columns in the matrix.

Returns
The number of columns

Implements CppNoddy::Sequential_Matrix_base< _Type >.

Definition at line 277 of file SparseMatrix.h.

277 {
278 return m_nc;
279 }

◆ nelts()

template<typename _Type >
std::size_t CppNoddy::SparseMatrix< _Type >::nelts
virtual

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

Returns
The number of (non-zero) elements

Implements CppNoddy::Sequential_Matrix_base< _Type >.

Definition at line 49 of file SparseMatrix.cpp.

49 {
50 std::size_t num_of_elts(0);
51 for(std::size_t row = 0; row < m_nr; ++row) {
52 num_of_elts += m_matrix[ row ].nelts();
53 }
54 return num_of_elts;
55 }

◆ nelts_in_row()

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 194 of file SparseMatrix.h.

194 {
195 return m_matrix[row].nelts();
196 }

◆ nrows()

template<typename _Type >
std::size_t CppNoddy::SparseMatrix< _Type >::nrows
inlinevirtual

Get the number of rows in the matrix.

Returns
The number of rows

Implements CppNoddy::Sequential_Matrix_base< _Type >.

Definition at line 272 of file SparseMatrix.h.

272 {
273 return m_nr;
274 }

◆ one_norm()

template<typename _Type >
double CppNoddy::SparseMatrix< _Type >::one_norm

Transpose the matrix in place.

Returns
The maximum one_norm of all rows

Definition at line 85 of file SparseMatrix.cpp.

85 {
86 double max(0.0);
87 for(std::size_t row = 0; row < m_nr; ++row) {
88 max = std::max(max, m_matrix[ row ].one_norm());
89 }
90 return max;
91 }
double one_norm() const
Transpose the matrix in place.

◆ operator()() [1/2]

template<typename _Type >
_Type & CppNoddy::SparseMatrix< _Type >::operator() ( const std::size_t &  row,
const std::size_t &  col 
)
inlinevirtual

Access operator.

Implements CppNoddy::Sequential_Matrix_base< _Type >.

Definition at line 234 of file SparseMatrix.h.

234 {
235 return m_matrix[ row ][ col ];
236 }

◆ operator()() [2/2]

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::Sequential_Matrix_base< _Type >.

Definition at line 229 of file SparseMatrix.h.

229 {
230 return m_matrix[ row ].get(col);
231 }

◆ operator=()

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 39 of file SparseMatrix.cpp.

39 {
40 if(this == &source)
41 return * this;
42 m_matrix = source.m_matrix;
43 m_nr = source.m_nr;
44 m_nc = source.m_nc;
45 return *this;
46 }

◆ operator[]() [1/2]

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 249 of file SparseMatrix.h.

249 {
250#ifdef PARANOID
251 if((row > m_nr) || (row < 0)) {
252 std::string problem("The SparseMatrix.get_row has a range error.\n");
253 throw ExceptionRange(problem, m_nr, row);
254 }
255#endif
256 return m_matrix[ row ];
257 }

◆ operator[]() [2/2]

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 261 of file SparseMatrix.h.

261 {
262#ifdef PARANOID
263 if((row > m_nr) || (row < 0)) {
264 std::string problem("The SparseMatrix.get_row has a range error.\n");
265 throw ExceptionRange(problem, m_nr, row);
266 }
267#endif
268 return m_matrix[ row ];
269 }

◆ row_swap()

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 282 of file SparseMatrix.h.

282 {
283 // actually do the swap, rather than keep a row permutation vector.
284 // std::swap<SparseVector<_Type> > ( matrix[ row1 ], matrix[ row2 ] );
285 m_matrix[ row1 ].swap(m_matrix[ row2 ]);
286 }

◆ scale()

template<typename _Type >
void CppNoddy::SparseMatrix< _Type >::scale ( const _Type &  mult)
virtual

Scale the matrix by a scalar.

Parameters
multThe scalar multiplier

Implements CppNoddy::Sequential_Matrix_base< _Type >.

Definition at line 78 of file SparseMatrix.cpp.

78 {
79 for(std::size_t row = 0; row < m_nr; ++row) {
80 m_matrix[ row ] *= mult;
81 }
82 }

Referenced by main().

◆ set()

template<typename _Type >
_Type & CppNoddy::SparseMatrix< _Type >::set ( const std::size_t &  row,
const std::size_t &  col 
)
inlinevirtual

Access operator.

Implements CppNoddy::Sequential_Matrix_base< _Type >.

Definition at line 244 of file SparseMatrix.h.

244 {
245 return this -> operator()(row, col);
246 }

◆ set_row()

template<typename _Type >
void CppNoddy::SparseMatrix< _Type >::set_row ( const std::size_t &  row,
const SparseVector< _Type > &  row_vector 
)
inline

Set a row of the matrix.

Parameters
rowThe index of the row to be set
Asparse vector of values to go into the row

Definition at line 90 of file SparseMatrix.h.

90 {
91 m_matrix[row] = row_vector;
92 }

◆ two_norm()

template<typename _Type >
double CppNoddy::SparseMatrix< _Type >::two_norm
Returns
The maximum two_norm of all rows

Definition at line 94 of file SparseMatrix.cpp.

94 {
95 double max(0.0);
96 for(std::size_t row = 0; row < m_nr; ++row) {
97 max = std::max(max, m_matrix[ row ].two_norm());
98 }
99 return max;
100 }
double two_norm() const

Friends And Related Function Documentation

◆ SparseLinearSystem

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

Definition at line 220 of file SparseMatrix.h.


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

© 2012

R.E. Hewitt