CppNoddy  0.85
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups Pages
Public Types | Public Member Functions | Public Attributes | List of all members
CppNoddy::BandedMatrix< _Type > Class Template Reference

A matrix class that constructs a BANDED matrix. More...

#include <BandedMatrix.h>

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

Public Types

typedef DenseVector< _Type >
::elt_iter 
elt_iter

Public Member Functions

 BandedMatrix ()
 Empty constructor .. should we stop this?
 BandedMatrix (const std::size_t &rows, const std::size_t &offdiag, const _Type &fill)
 Noddy Banded Matrix constructor.
 BandedMatrix (const BandedMatrix &source)
 Copy constructor.
BandedMatrixoperator= (const BandedMatrix &source)
 Assignment operator.
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.
std::size_t ncols () const
 Get the number of columns.
std::size_t nelts () const
 Get the number of elements.
void scale (const _Type &mult)
 Scale all entries in 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 the matrix by a DENSE vector.
void dump () const
 Output the matrix contents to std::cout.
void assign (_Type elt)
 Assign a value the matrix so that it has the same geometry, but zero entries in all locations including those reserved for pivotting.
std::size_t noffdiag () const
 Get the number of off-diagonal elements where the total INPUT band width is 2*noffdiag+1 since the band structure is symmetric.
void row_swap (const std::size_t &row1, const std::size_t &row2)
 Exchange rows in the matrix.
double * base ()
 Allow direct access to the vector STORAGE.
elt_iter get_elt_iter (std::size_t row, std::size_t col)
template<>
double * base ()
template<>
double * base ()
- Public Member Functions inherited from CppNoddy::Matrix_base< _Type >
 Matrix_base ()
 An empty constructor.
virtual ~Matrix_base ()

Public Attributes

DenseVector< _Type > STORAGE
 A contiguous vector.
std::size_t N
 The number of rows/cols in the matrix.
std::size_t L
 Max number of (INPUT) bands above OR below the main diagonal.

Detailed Description

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

A matrix class that constructs a BANDED matrix.

Definition at line 17 of file BandedMatrix.h.

Member Typedef Documentation

template<typename _Type>
typedef DenseVector<_Type>::elt_iter CppNoddy::BandedMatrix< _Type >::elt_iter

Definition at line 22 of file BandedMatrix.h.

Constructor & Destructor Documentation

template<typename _Type>
CppNoddy::BandedMatrix< _Type >::BandedMatrix ( )
inline

Empty constructor .. should we stop this?

Definition at line 25 of file BandedMatrix.h.

{}
template<typename _Type>
CppNoddy::BandedMatrix< _Type >::BandedMatrix ( const std::size_t &  rows,
const std::size_t &  offdiag,
const _Type &  fill 
)

Noddy Banded Matrix constructor.

Parameters
rowsThe number of rows in the matrix.
offdiagThe maximum number of bands above OR below the diagonal. The total number stored will be 3 * offdiag + 1; i.e. assumes the number of offdiagonal elts is the same both above and below, and we must an extra 'offdiag' because of fill-in during pivotting.
fillThe initial value to be placed in each element of the banded matrix.

Definition at line 18 of file BandedMatrix.cpp.

References CppNoddy::BandedMatrix< _Type >::L, CppNoddy::BandedMatrix< _Type >::N, and CppNoddy::BandedMatrix< _Type >::STORAGE.

:
Matrix_base<_Type>(),
N( rows ),
L( upper_offdiag_bands )
{
// we'll assume that the upper & lower offdiags are of equal size.
// logical STORAGE is the main diagonal + upper offdiagonal + lower offdiagonal
// = 2 * L + 1.
// However, allowing for row interchanging during pivotting leads to additional
// padding of L, to make a total of 3*L+1.
STORAGE = DenseVector<_Type>( N * ( 3 * L + 1 ), 0.0 );
}
template<typename _Type>
CppNoddy::BandedMatrix< _Type >::BandedMatrix ( const BandedMatrix< _Type > &  source)

Copy constructor.

Parameters
sourceThe source object to be copied

Definition at line 34 of file BandedMatrix.cpp.

References CppNoddy::Example::source().

:
Matrix_base<_Type>( source )
{
*this = source;
}

Member Function Documentation

template<typename _Type>
void CppNoddy::BandedMatrix< _Type >::assign ( _Type  elt)
inline

Assign a value the matrix so that it has the same geometry, but zero entries in all locations including those reserved for pivotting.

Parameters
eltThe value to be assigned to all entries

Definition at line 104 of file BandedMatrix.h.

Referenced by CppNoddy::PDE_IBVP< _Type >::assemble_matrix_problem().

{
STORAGE.assign( STORAGE.size(), elt );
}
template<typename _Type>
double* CppNoddy::BandedMatrix< _Type >::base ( )

Allow direct access to the vector STORAGE.

Dangerous, but used for passing to LAPACK.

template<>
double * CppNoddy::BandedMatrix< double >::base ( )

Definition at line 187 of file BandedMatrix.cpp.

{
return &( STORAGE[0] );
}
template<>
double * CppNoddy::BandedMatrix< std::complex< double > >::base ( )

Definition at line 193 of file BandedMatrix.cpp.

{
return &( STORAGE[0].real() );
}
template<typename _Type >
void CppNoddy::BandedMatrix< _Type >::dump ( ) const
virtual

Output the matrix contents to std::cout.

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 158 of file BandedMatrix.cpp.

{
std::cout << "BANDED mtx size = " << nrows() << " x " << ncols()
<< " with INPUT no. of bands = " << 2 * L + 1
<< " and total bands STORED (for pivotting) = " << 3 * L + 1 << "\n";
std::cout.precision( 4 );
std::cout << "- start matrix \n";
for ( std::size_t i = 0; i < nrows(); ++i )
{
std::cout << " row " << i << " = ";
for ( std::size_t j = 0; j < nrows(); ++j )
{
if ( i == j )
std::cout << "*";
if ( ( ( int ) j < ( int ) i - ( int ) L ) || ( ( int ) j > ( int ) i + ( int ) ( 2*L ) ) )
{
std::cout << 0 << ", ";
}
else
{
std::cout << operator() ( i, j ) << ", ";
}
}
std::cout << "\n";
}
std::cout << "- end matrix \n";
}
template<typename _Type >
double CppNoddy::BandedMatrix< _Type >::frob_norm ( ) const
virtual
Returns
The sum of the two_norm of all rows

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 145 of file BandedMatrix.cpp.

References CppNoddy::DenseVector< _Type >::two_norm().

{
double sum( 0.0 );
for ( unsigned row = 0; row < N; ++row )
{
// copy the row into a temp vector
DenseVector<_Type> temp( 3 * L + 1, &STORAGE[ row * ( 3 * L + 1 ) ] );
sum += temp.two_norm();
}
return sum;
}
template<typename _Type >
const _Type & CppNoddy::BandedMatrix< _Type >::get ( const std::size_t &  row,
const std::size_t &  col 
) const
inlinevirtual

Access operator.

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 212 of file BandedMatrix.h.

{
return operator() ( row, col );
}
template<typename _Type>
elt_iter CppNoddy::BandedMatrix< _Type >::get_elt_iter ( std::size_t  row,
std::size_t  col 
)
inline

Definition at line 126 of file BandedMatrix.h.

Referenced by CppNoddy::PDE_IBVP< _Type >::assemble_matrix_problem().

{
return STORAGE.begin() + L * ( 3 * col + 2 ) + row;
}
template<typename _Type >
double CppNoddy::BandedMatrix< _Type >::inf_norm ( ) const
virtual
Returns
The maximum inf_norm of all rows

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 131 of file BandedMatrix.cpp.

References CppNoddy::DenseVector< _Type >::inf_norm().

{
double max( 0.0 );
for ( unsigned row = 0; row < N; ++row )
{
// copy the row into a temp vector
DenseVector<_Type> temp( 3 * L + 1, &STORAGE[ row * ( 3 * L + 1 ) ] );
max = std::max( max, temp.inf_norm() );
}
return max;
}
template<typename _Type>
DenseVector< _Type > CppNoddy::BandedMatrix< _Type >::multiply ( const DenseVector< _Type > &  X) const
virtual

Right multiply the matrix by a DENSE vector.

Parameters
XThe DENSE vector to multiply by
Returns
A DENSE vector of length ncols() produced from the multiplication

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 65 of file BandedMatrix.cpp.

{
std::string problem;
problem = " The multiply method of the BandedMatrix class has \n";
problem += " not been implemented. \n";
throw ExceptionRuntime( problem );
return STORAGE; // dummy return
}
template<typename _Type >
std::size_t CppNoddy::BandedMatrix< _Type >::ncols ( ) const
inlinevirtual

Get the number of columns.

Returns
The number of columns in the matrix

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 231 of file BandedMatrix.h.

Referenced by CppNoddy::Utility::fill_random(), and CppNoddy::Utility::fill_row().

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

Get the number of elements.

Returns
The number of elements

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 52 of file BandedMatrix.cpp.

{
return STORAGE.size();
}
template<typename _Type >
std::size_t CppNoddy::BandedMatrix< _Type >::noffdiag ( ) const
inline

Get the number of off-diagonal elements where the total INPUT band width is 2*noffdiag+1 since the band structure is symmetric.

Returns
The number of upper or lower offdiagonal bands; NOTE: the number of offdiagonals STORED will typically be three times this to allow for pivotting

Definition at line 237 of file BandedMatrix.h.

Referenced by CppNoddy::PDE_IBVP< _Type >::assemble_matrix_problem(), CppNoddy::DenseMatrix< D_complex >::DenseMatrix(), CppNoddy::Utility::fill_random(), CppNoddy::Utility::fill_row(), and main().

{
return L;
}
template<typename _Type >
std::size_t CppNoddy::BandedMatrix< _Type >::nrows ( ) const
inlinevirtual

Get the number of rows.

Returns
The number of rows in the matrix

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 225 of file BandedMatrix.h.

Referenced by CppNoddy::DenseMatrix< D_complex >::DenseMatrix(), CppNoddy::Utility::fill(), and CppNoddy::Utility::fill_random().

{
return N;
}
template<typename _Type >
double CppNoddy::BandedMatrix< _Type >::one_norm ( ) const
virtual
Returns
The maximum one_norm of all rows

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 103 of file BandedMatrix.cpp.

References CppNoddy::DenseVector< _Type >::one_norm().

{
double max( 0.0 );
for ( unsigned row = 0; row < N; ++row )
{
// copy the row into a temp vector
DenseVector<_Type> temp( 3 * L + 1, &STORAGE[ row * ( 3 * L + 1 ) ] );
max = std::max( max, temp.one_norm() );
}
return max;
}
template<typename _Type >
const _Type & CppNoddy::BandedMatrix< _Type >::operator() ( const std::size_t &  row,
const std::size_t &  col 
) const
inlinevirtual

Access operator.

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 147 of file BandedMatrix.h.

{
#ifdef PARANOID
// if outside the NxN matrix
if ( ( row >= N ) || ( row < 0 ) || ( col >= N ) || ( col < 0 ) )
{
std::string problem;
problem = " The const operator() of BandedMatrix has been called \n";
problem += " with a (row, col) index that is outside \n";
problem += " the square NxN matrix.\n";
throw ExceptionGeom( problem, N, N, row, col );
}
// check if the subscripts are out of the band
if ( !( ( col + L >= row ) && ( col <= 2*L + row ) ) )
{
std::string problem;
problem = " The const operator() of BandedMatrix has been called \n";
problem += " with a (row, col) index that is outside \n";
problem += " the band structure. Bandwidth and offset from\n";
problem += " the diagonal information follows.\n";
throw ExceptionGeom( problem, L, col - row );
}
#endif
// MOSTLY WE PUSH BANDED MATRICES TO LAPACK - so we'll keep column major
// internal storage ... a la Fortran
// return STORAGE[ col * ( 3 * L + 1 ) + ( row - col ) + 2 * L ];
// or equiv.
return STORAGE[ L * ( 3 * col + 2 ) + row ];
}
template<typename _Type >
_Type & CppNoddy::BandedMatrix< _Type >::operator() ( const std::size_t &  row,
const std::size_t &  col 
)
inlinevirtual

Access operator.

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 178 of file BandedMatrix.h.

{
#ifdef PARANOID
// if outside the NxN matrix
if ( ( row >= N ) || ( row < 0 ) || ( col >= N ) || ( col < 0 ) )
{
std::string problem;
problem = " The operator() of BandedMatrix has been called \n";
problem += " with a (row, col) index that is outside \n";
problem += " the square NxN matrix.\n";
throw ExceptionRange( problem, N, N, row, col );
}
// check if the subscripts are out of the band
if ( !( ( col + L >= row ) && ( col <= 2*L + row ) ) )
{
std::string problem;
problem = " The operator() of BandedMatrix has been called \n";
problem += " with a (row, col) index that is outside \n";
problem += " the band structure.\n";
std::cout << " L = " << L << "\n";
std::cout << " row = " << row << "\n";
std::cout << " col = " << col << "\n";
throw ExceptionRange( problem, N, L, row, col );
}
#endif
// MOSTLY WE PUSH BANDED MATRICES TO LAPACK - so we'll keep column major
// internal storage ... a la Fortran
// return STORAGE[ col * ( 3 * L + 1 ) + ( row - col ) + 2 * L ];
// or equiv.
return STORAGE[ L * ( 3 * col + 2 ) + row ];
}
template<typename _Type >
BandedMatrix< _Type > & CppNoddy::BandedMatrix< _Type >::operator= ( const BandedMatrix< _Type > &  source)
inline

Assignment operator.

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

Definition at line 41 of file BandedMatrix.cpp.

References CppNoddy::BandedMatrix< _Type >::L, CppNoddy::BandedMatrix< _Type >::N, and CppNoddy::BandedMatrix< _Type >::STORAGE.

{
if ( this == &source )
return * this;
STORAGE = source.STORAGE;
N = source.N;
L = source.L;
return *this;
}
template<typename _Type >
void CppNoddy::BandedMatrix< _Type >::row_swap ( const std::size_t &  row1,
const std::size_t &  row2 
)

Exchange rows in the matrix.

Parameters
row1First row to be swapped
row2Second row to be swapped
Todo:
MAKE ME PRIVATE OR ELSE!

Definition at line 90 of file BandedMatrix.cpp.

{
// WE MUST HAVE row2 > row1 & row1 must have no elements left of the diagonal!
// only of any use to native Gaussian elimination solver
/// \todo MAKE ME PRIVATE OR ELSE!
for ( std::size_t col = row1; col <= std::min( row1 + 2 * L, N - 1 ); ++col )
{
std::swap( operator()( row1, col ), operator()( row2, col ) );
}
}
template<typename _Type>
void CppNoddy::BandedMatrix< _Type >::scale ( const _Type &  mult)
virtual

Scale all entries in the matrix by a scalar.

Parameters
multThe scalar multiplier

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 58 of file BandedMatrix.cpp.

{
STORAGE *= mult;
}
template<typename _Type >
_Type & CppNoddy::BandedMatrix< _Type >::set ( const std::size_t &  row,
const std::size_t &  col 
)
inlinevirtual

Access operator.

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 219 of file BandedMatrix.h.

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

Transpose the matrix.

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 76 of file BandedMatrix.cpp.

{
for ( std::size_t row = 0; row < N; ++row )
{
for ( std::size_t col = std::max( 0, int( row ) - int( L ) ); col < row; ++col )
{
// swap elements
std::swap( operator()( row, col ), operator()( col, row ) );
}
}
}
template<typename _Type >
double CppNoddy::BandedMatrix< _Type >::two_norm ( ) const
virtual
Returns
The maximum two_norm of all rows

Implements CppNoddy::Matrix_base< _Type >.

Definition at line 117 of file BandedMatrix.cpp.

References CppNoddy::DenseVector< _Type >::two_norm().

{
double max( 0.0 );
for ( unsigned row = 0; row < N; ++row )
{
// copy the row into a temp vector
DenseVector<_Type> temp( 3 * L + 1, &STORAGE[ row * ( 3 * L + 1 ) ] );
max = std::max( max, temp.two_norm() );
}
return max;
}

Member Data Documentation

template<typename _Type>
std::size_t CppNoddy::BandedMatrix< _Type >::L

Max number of (INPUT) bands above OR below the main diagonal.

Definition at line 138 of file BandedMatrix.h.

Referenced by CppNoddy::BandedMatrix< _Type >::BandedMatrix(), CppNoddy::BandedMatrix< double >::get_elt_iter(), and CppNoddy::BandedMatrix< _Type >::operator=().

template<typename _Type>
std::size_t CppNoddy::BandedMatrix< _Type >::N

The number of rows/cols in the matrix.

Definition at line 136 of file BandedMatrix.h.

Referenced by CppNoddy::BandedMatrix< _Type >::BandedMatrix(), and CppNoddy::BandedMatrix< _Type >::operator=().

template<typename _Type>
DenseVector<_Type> CppNoddy::BandedMatrix< _Type >::STORAGE

A contiguous vector.

Definition at line 134 of file BandedMatrix.h.

Referenced by CppNoddy::BandedMatrix< double >::assign(), CppNoddy::BandedMatrix< _Type >::BandedMatrix(), CppNoddy::BandedMatrix< double >::get_elt_iter(), and CppNoddy::BandedMatrix< _Type >::operator=().


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

© 2012

R.E. Hewitt