CppNoddy  0.85
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups Pages
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.
 SparseMatrix (const SparseMatrix &source)
 Copy constructor.
SparseMatrixoperator= (const SparseMatrix &source)
 Assignment operator.
 ~SparseMatrix ()
const _Type & operator() (const std::size_t &row, const std::size_t &col) const
 Access operator.
_Type & operator() (const std::size_t &row, const std::size_t &col)
 Access operator.
const _Type & get (const std::size_t &row, const std::size_t &col) const
 Access operator.
_Type & set (const std::size_t &row, const std::size_t &col)
 Access operator.
std::size_t nrows () const
 Get the number of rows in the matrix.
std::size_t ncols () const
 Get the number of columns in the matrix.
std::size_t nelts () const
 Get the number of elements in the matrix.
void scale (const _Type &mult)
 Scale the matrix by a scalar.
void transpose ()
 Transpose the matrix.
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.
void dump () const
 Output the contents of the matrix to std::cout.
SparseVector< _Type > & operator[] (const std::size_t &row)
 Operator overloading for ROW access.
const SparseVector< _Type > & operator[] (const std::size_t &row) const
 Operator overloading for ROW access.
void get_row_compressed (_Type *storage, int *cols, int *rows)
 Take the contents of the SparseMatrix and convert it to a standard compressed row format that can be fed directly into the SuperLU library to solve.
- Public Member Functions inherited from CppNoddy::Matrix_base< _Type >
 Matrix_base ()
 An empty constructor.
virtual ~Matrix_base ()

Friends

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

: Matrix_base<_Type>(), NR( rows ), NC( cols )
{
MATRIX.reserve( NR );
SparseVector<_Type> sparse_row( NC );
for ( std::size_t i = 0; i < NR; ++i )
{
MATRIX.push_back( sparse_row );
}
}
template<typename _Type >
CppNoddy::SparseMatrix< _Type >::SparseMatrix ( const SparseMatrix< _Type > &  source)

Copy constructor.

Parameters
sourceThe source object to be copied

Definition at line 28 of file SparseMatrix.cpp.

References CppNoddy::Example::source().

:
Matrix_base<_Type>( source )
{
*this = source;
}
template<typename _Type>
CppNoddy::SparseMatrix< _Type >::~SparseMatrix ( )
inline

Definition at line 45 of file SparseMatrix.h.

{}

Member Function Documentation

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

{
std::cout << "SPARSE mtx size = " << NR << " x sparse \n";
std::cout.precision( 4 );
std::cout << "- start matrix \n";
for ( std::size_t row = 0; row < NR; ++row )
{
std::cout << " row " << row << " : ";
MATRIX[ row ].dump();
std::cout << " \n";
}
std::cout << "- end matrix \n";
}
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 129 of file SparseMatrix.cpp.

{
double sum ( 0.0 );
for ( std::size_t row = 0; row < NR; ++row )
{
sum += MATRIX[ row ].two_norm();
}
return sum;
}
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 169 of file SparseMatrix.h.

{
return this -> operator() ( row, col );
}
template<typename _Type >
void CppNoddy::SparseMatrix< _Type >::get_row_compressed ( _Type *  storage,
int *  cols,
int *  rows 
)

Take the contents of the SparseMatrix and convert it to a standard compressed row format that can be fed directly into the SuperLU library to solve.

Parameters
storageA contiguous vector of the non-zero elts
colsThe col indices of each entry in the storage vector
rowsThe indices of the elements in the storage vector that begin a new row

Definition at line 146 of file SparseMatrix.cpp.

{
// iterator to the maps that are used in SparseVector
// this is bad form as it exposes the internals of the SparseVector storage
citer pos;
//std::size_t last_col( NC + 1 ); // no column should have NC + 1, so this is a dummy start value
std::size_t i( 0 ); // where we are in the storage vector
//
for ( std::size_t row = 0; row < NR; ++row )
{
// flag to indicate that we're on a new coloumn
bool new_row( true );
pos = MATRIX[ row ].begin();
do
{
_Type elt( pos -> second );
int col( pos -> first );
storage[ i ] = elt;
cols[ i ] = col;
++pos;
if ( new_row )
{
rows[ row ] = i;
new_row = false;
}
++i;
}
while ( pos != MATRIX[ row ].end() );
}
// last entry points to end + 1
rows[ NR ] = nelts();
}
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 118 of file SparseMatrix.cpp.

{
double max( 0.0 );
for ( std::size_t row = 0; row < NR; ++row )
{
max = std::max( max, MATRIX[ row ].inf_norm() );
}
return max;
}
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 matrix of the result of the multiplication

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 140 of file SparseMatrix.cpp.

{
throw ExceptionRuntime( "SparseMatrix.multiply has not been implemented" );
}
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 214 of file SparseMatrix.h.

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

Get the number of elements in the matrix.

Returns
The number of elements

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 46 of file SparseMatrix.cpp.

Referenced by main().

{
std::size_t num_of_elts( 0 );
for ( std::size_t row = 0; row < NR; ++row )
{
num_of_elts += MATRIX[ row ].nelts();
}
return num_of_elts;
}
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 208 of file SparseMatrix.h.

{
return NR;
}
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 96 of file SparseMatrix.cpp.

{
double max( 0.0 );
for ( std::size_t row = 0; row < NR; ++row )
{
max = std::max( max, MATRIX[ row ].one_norm() );
}
return max;
}
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 157 of file SparseMatrix.h.

{
return MATRIX[ row ].get( col );
}
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 163 of file SparseMatrix.h.

{
return MATRIX[ row ][ col ];
}
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 35 of file SparseMatrix.cpp.

{
if ( this == &source )
return * this;
MATRIX = source.MATRIX;
NR = source.NR;
NC = source.NC;
return *this;
}
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 181 of file SparseMatrix.h.

{
#ifdef PARANOID
if ( ( row > NR ) || ( row < 0 ) )
{
std::string problem( "The SparseMatrix.get_row has a range error.\n" );
throw ExceptionRange( problem, NR, row );
}
#endif
return MATRIX[ row ];
}
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 195 of file SparseMatrix.h.

{
#ifdef PARANOID
if ( ( row > NR ) || ( row < 0 ) )
{
std::string problem( "The SparseMatrix.get_row has a range error.\n" );
throw ExceptionRange( problem, NR, row );
}
#endif
return MATRIX[ row ];
}
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 81 of file SparseMatrix.cpp.

{
for ( std::size_t row = 0; row < NR; ++row )
{
MATRIX[ row ] *= mult;
}
}
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 175 of file SparseMatrix.h.

{
return this -> operator() ( row, col );
}
template<typename _Type >
void CppNoddy::SparseMatrix< _Type >::transpose ( )
virtual

Transpose the matrix.

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 90 of file SparseMatrix.cpp.

{
throw ExceptionRuntime( "SparseMatrix.transpose has not been implemented" );
}
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 107 of file SparseMatrix.cpp.

{
double max( 0.0 );
for ( std::size_t row = 0; row < NR; ++row )
{
max = std::max( max, MATRIX[ row ].two_norm() );
}
return max;
}

Friends And Related Function Documentation

template<typename _Type>
friend class SparseLinearSystem
friend

Definition at line 148 of file SparseMatrix.h.


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

© 2012

R.E. Hewitt