Cantera  2.0
Public Member Functions | Public Attributes | Protected Attributes | Private Member Functions | List of all members
BandMatrix Class Reference

A class for banded matrices, involving matrix inversion processes. More...

#include <BandMatrix.h>

Inheritance diagram for BandMatrix:
[legend]
Collaboration diagram for BandMatrix:
[legend]

Public Member Functions

 BandMatrix ()
 Base Constructor.
 
 BandMatrix (size_t n, size_t kl, size_t ku, doublereal v=0.0)
 Creates a banded matrix and sets all elements to zero.
 
 BandMatrix (const BandMatrix &y)
 Copy constructor.
 
virtual ~BandMatrix ()
 Destructor. Does nothing.
 
BandMatrixoperator= (const BandMatrix &y)
 assignment operator
 
void resize (size_t n, size_t kl, size_t ku, doublereal v=0.0)
 Resize the matrix problem.
 
void bfill (doublereal v=0.0)
 Fill or zero the matrix.
 
doublereal & operator() (size_t i, size_t j)
 Index into the (i,j) element.
 
doublereal operator() (size_t i, size_t j) const
 Constant index into the (i,j) element.
 
doublereal & value (size_t i, size_t j)
 Return a changeable reference to element (i,j).
 
doublereal value (size_t i, size_t j) const
 Return the value of element (i,j).
 
size_t index (size_t i, size_t j) const
 Returns the location in the internal 1D array corresponding to the (i,j) element in the banded array.
 
doublereal _value (size_t i, size_t j) const
 Return the value of the (i,j) element for (i,j) within the bandwidth.
 
virtual size_t nRows () const
 Returns the number of rows.
 
virtual size_t nRowsAndStruct (size_t *const iStruct=0) const
 Return the size and structure of the matrix.
 
size_t nColumns () const
 Number of columns.
 
size_t nSubDiagonals () const
 Number of subdiagonals.
 
size_t nSuperDiagonals () const
 Number of superdiagonals.
 
size_t ldim () const
 Return the number of rows of storage needed for the band storage.
 
vector_intipiv ()
 Return a reference to the pivot vector.
 
virtual void mult (const doublereal *b, doublereal *prod) const
 Multiply A*b and write result to prod.
 
virtual void leftMult (const doublereal *const b, doublereal *const prod) const
 Multiply b*A and write result to prod.
 
int factor ()
 Perform an LU decomposition, the LAPACK routine DGBTRF is used.
 
int solve (const doublereal *const b, doublereal *const x)
 Solve the matrix problem Ax = b.
 
int solve (doublereal *b)
 Solve the matrix problem Ax = b.
 
virtual vector_fp::iterator begin ()
 Returns an iterator for the start of the band storage data.
 
vector_fp::iterator end ()
 Returns an iterator for the end of the band storage data.
 
vector_fp::const_iterator begin () const
 Returns a const iterator for the start of the band storage data.
 
vector_fp::const_iterator end () const
 Returns a const iterator for the end of the band storage data.
 
virtual void zero ()
 Zero the matrix.
 
virtual int factorQR ()
 Factors the A matrix using the QR algorithm, overwriting A.
 
virtual doublereal rcondQR ()
 Returns an estimate of the inverse of the condition number for the matrix.
 
virtual doublereal rcond (doublereal a1norm)
 Returns an estimate of the inverse of the condition number for the matrix.
 
virtual void useFactorAlgorithm (int fAlgorithm)
 Change the way the matrix is factored.
 
virtual int factorAlgorithm () const
 Returns the factor algorithm used.
 
virtual doublereal oneNorm () const
 Returns the one norm of the matrix.
 
virtual GeneralMatrixduplMyselfAsGeneralMatrix () const
 Duplicate this object as a GeneralMatrix pointer.
 
virtual bool factored () const
 Report whether the current matrix has been factored.
 
virtual doublereal * ptrColumn (size_t j)
 Return a pointer to the top of column j, column values are assumed to be contiguous in memory.
 
virtual doublereal *const * colPts ()
 Return a vector of const pointers to the columns.
 
virtual void copyData (const GeneralMatrix &y)
 Copy the data from one array into another without doing any checking.
 
virtual void clearFactorFlag ()
 Clear the factored flag.
 
virtual size_t checkRows (doublereal &valueSmall) const
 Check to see if we have any zero rows in the jacobian.
 
virtual size_t checkColumns (doublereal &valueSmall) const
 Check to see if we have any zero columns in the jacobian.
 

Public Attributes

int matrixType_
 Matrix type.
 

Protected Attributes

vector_fp data
 Matrix data.
 
vector_fp ludata
 Factorized data.
 
bool m_factored
 Boolean indicating whether the matrix is factored.
 
size_t m_n
 Number of rows and columns of the matrix.
 
size_t m_kl
 Number of subdiagonals of the matrix.
 
size_t m_ku
 Number of super diagonals of the matrix.
 
doublereal m_zero
 value of zero
 
vector_int m_ipiv
 Pivot vector.
 
std::vector< doublereal * > m_colPtrs
 Vector of column pointers.
 
vector_int iwork_
 Extra work array needed - size = n.
 
vector_fp work_
 Extra dp work array needed - size = 3n.
 

Private Member Functions

void err (std::string msg) const
 Error function that gets called for unhandled cases.
 

Detailed Description

A class for banded matrices, involving matrix inversion processes.

The class is based upon the LAPACK banded storage matrix format.

An important issue with this class is that it stores both the original data and the LU factorization of the data. This means that the banded matrix typically will take up twice the room that it is expected to take.

QR factorizations of banded matrices are not included in the original LAPACK work. Add-ons are available. However, they are not included here. Instead we just use the stock LU decompositions.

This class is a derived class of the base class GeneralMatrix. However, within the oneD directory, the class is used as is, without reference to the GeneralMatrix base type.

Definition at line 38 of file BandMatrix.h.

Constructor & Destructor Documentation

Base Constructor.

  • Create an 0 by 0 matrix, and initialize all elements to 0.

Definition at line 25 of file BandMatrix.cpp.

References BandMatrix::data, and BandMatrix::ludata.

Referenced by BandMatrix::duplMyselfAsGeneralMatrix().

BandMatrix ( size_t  n,
size_t  kl,
size_t  ku,
doublereal  v = 0.0 
)

Creates a banded matrix and sets all elements to zero.

Create an n by n banded matrix, and initialize all elements to v.

Parameters
nsize of the square matrix
klband size on the lower portion of the matrix
kuband size on the upper portion of the matrix
vinitial value of all matrix components.

Definition at line 37 of file BandMatrix.cpp.

References BandMatrix::data, BandMatrix::ludata, BandMatrix::m_colPtrs, BandMatrix::m_ipiv, and BandMatrix::m_n.

BandMatrix ( const BandMatrix y)

Copy constructor.

Parameters
yMatrix to be copied

Definition at line 57 of file BandMatrix.cpp.

References BandMatrix::data, BandMatrix::ludata, BandMatrix::m_colPtrs, BandMatrix::m_factored, BandMatrix::m_ipiv, BandMatrix::m_kl, BandMatrix::m_ku, and BandMatrix::m_n.

~BandMatrix ( )
virtual

Destructor. Does nothing.

Definition at line 79 of file BandMatrix.cpp.

Member Function Documentation

BandMatrix & operator= ( const BandMatrix y)

assignment operator

Parameters
yreference to the matrix to be copied

Definition at line 84 of file BandMatrix.cpp.

References BandMatrix::data, BandMatrix::ludata, BandMatrix::m_colPtrs, BandMatrix::m_factored, BandMatrix::m_ipiv, BandMatrix::m_kl, BandMatrix::m_ku, BandMatrix::m_n, and GeneralMatrix::operator=().

void resize ( size_t  n,
size_t  kl,
size_t  ku,
doublereal  v = 0.0 
)

Resize the matrix problem.

All data is lost

Parameters
nsize of the square matrix
klband size on the lower portion of the matrix
kuband size on the upper portion of the matrix
vinitial value of all matrix components.

Definition at line 105 of file BandMatrix.cpp.

References BandMatrix::data, BandMatrix::ludata, BandMatrix::m_colPtrs, BandMatrix::m_factored, BandMatrix::m_ipiv, BandMatrix::m_kl, BandMatrix::m_ku, and BandMatrix::m_n.

void bfill ( doublereal  v = 0.0)

Fill or zero the matrix.

Parameters
vFill value, defaults to zero.

Definition at line 122 of file BandMatrix.cpp.

References BandMatrix::data, and BandMatrix::m_factored.

Referenced by MultiJac::eval().

doublereal & operator() ( size_t  i,
size_t  j 
)
virtual

Index into the (i,j) element.

Parameters
irow
jcolumn

Returns a changeable reference to the matrix entry

Implements GeneralMatrix.

Definition at line 134 of file BandMatrix.cpp.

References BandMatrix::value().

doublereal operator() ( size_t  i,
size_t  j 
) const
virtual

Constant index into the (i,j) element.

Parameters
irow
jcolumn

Returns an unchangeable reference to the matrix entry

Implements GeneralMatrix.

Definition at line 139 of file BandMatrix.cpp.

References BandMatrix::value().

doublereal & value ( size_t  i,
size_t  j 
)

Return a changeable reference to element (i,j).

Since this method may alter the element value, it may need to be refactored, so the flag m_factored is set to false.

Parameters
irow
jcolumn
Returns
Returns a reference to the value of the matrix entry

Definition at line 144 of file BandMatrix.cpp.

References BandMatrix::data, BandMatrix::index(), BandMatrix::m_factored, BandMatrix::m_kl, and BandMatrix::m_zero.

Referenced by BandMatrix::checkColumns(), BandMatrix::checkRows(), MultiJac::eval(), BandMatrix::oneNorm(), and BandMatrix::operator()().

doublereal value ( size_t  i,
size_t  j 
) const

Return the value of element (i,j).

This method does not alter the array.

Parameters
irow
jcolumn
Returns
Returns the value of the matrix entry

Definition at line 153 of file BandMatrix.cpp.

References BandMatrix::data, BandMatrix::index(), and BandMatrix::m_kl.

size_t index ( size_t  i,
size_t  j 
) const

Returns the location in the internal 1D array corresponding to the (i,j) element in the banded array.

Parameters
irow
jcolumn
Returns
Returns the index of the matrix entry

Definition at line 161 of file BandMatrix.cpp.

References BandMatrix::m_kl, and BandMatrix::m_ku.

Referenced by BandMatrix::_value(), and BandMatrix::value().

doublereal _value ( size_t  i,
size_t  j 
) const

Return the value of the (i,j) element for (i,j) within the bandwidth.

For efficiency, this method does not check that (i,j) are within the bandwidth; it is up to the calling program to insure that this is true.

Parameters
irow
jcolumn
Returns
Returns the value of the matrix entry

Definition at line 169 of file BandMatrix.cpp.

References BandMatrix::data, and BandMatrix::index().

Referenced by BandMatrix::leftMult(), and BandMatrix::mult().

size_t nRows ( ) const
virtual

Returns the number of rows.

Implements GeneralMatrix.

Definition at line 175 of file BandMatrix.cpp.

References BandMatrix::m_n.

Referenced by BandMatrix::factor(), BandMatrix::mult(), and Cantera::operator<<().

size_t nRowsAndStruct ( size_t *const  iStruct = 0) const
virtual

Return the size and structure of the matrix.

This is inherited from GeneralMatrix

Parameters
iStructOUTPUT Pointer to a vector of ints that describe the structure of the matrix. istruct[0] = kl istruct[1] = ku
Returns
returns the number of rows and columns in the matrix.

Implements GeneralMatrix.

Definition at line 181 of file BandMatrix.cpp.

References BandMatrix::m_kl, BandMatrix::m_ku, and BandMatrix::m_n.

size_t nColumns ( ) const

Number of columns.

Definition at line 191 of file BandMatrix.cpp.

References BandMatrix::m_n.

Referenced by BandMatrix::factor(), BandMatrix::leftMult(), Cantera::operator<<(), and BandMatrix::solve().

size_t nSubDiagonals ( ) const

Number of subdiagonals.

Definition at line 197 of file BandMatrix.cpp.

References BandMatrix::m_kl.

Referenced by BandMatrix::factor(), and BandMatrix::solve().

size_t nSuperDiagonals ( ) const

Number of superdiagonals.

Definition at line 203 of file BandMatrix.cpp.

References BandMatrix::m_ku.

Referenced by BandMatrix::factor(), and BandMatrix::solve().

size_t ldim ( ) const

Return the number of rows of storage needed for the band storage.

Definition at line 208 of file BandMatrix.cpp.

References BandMatrix::m_kl, and BandMatrix::m_ku.

Referenced by BandMatrix::factor(), and BandMatrix::solve().

vector_int & ipiv ( )

Return a reference to the pivot vector.

Returns
return a reference to the pivot vector

Definition at line 213 of file BandMatrix.cpp.

References BandMatrix::m_ipiv.

Referenced by BandMatrix::factor(), and BandMatrix::solve().

void mult ( const doublereal *  b,
doublereal *  prod 
) const
virtual

Multiply A*b and write result to prod.

Parameters
bVector to do the rh multiplication
prodOUTPUT vector to receive the result

Implements GeneralMatrix.

Definition at line 221 of file BandMatrix.cpp.

References BandMatrix::_value(), BandMatrix::m_kl, BandMatrix::m_ku, BandMatrix::m_n, and BandMatrix::nRows().

void leftMult ( const doublereal *const  b,
doublereal *const  prod 
) const
virtual

Multiply b*A and write result to prod.

Parameters
bVector to do the lh multiplication
prodOUTPUT vector to receive the result

Implements GeneralMatrix.

Definition at line 241 of file BandMatrix.cpp.

References BandMatrix::_value(), BandMatrix::m_kl, BandMatrix::m_ku, BandMatrix::m_n, and BandMatrix::nColumns().

int factor ( )
virtual

Perform an LU decomposition, the LAPACK routine DGBTRF is used.

The factorization is saved in ludata.

Returns
Return a success flag. 0 indicates a success ~0 Some error occurred, see the LAPACK documentation

Implements GeneralMatrix.

Definition at line 263 of file BandMatrix.cpp.

References BandMatrix::data, DATA_PTR, BandMatrix::ipiv(), BandMatrix::ldim(), BandMatrix::ludata, BandMatrix::m_factored, BandMatrix::nColumns(), BandMatrix::nRows(), BandMatrix::nSubDiagonals(), and BandMatrix::nSuperDiagonals().

Referenced by BandMatrix::factorQR(), and BandMatrix::solve().

int solve ( const doublereal *const  b,
doublereal *const  x 
)

Solve the matrix problem Ax = b.

Parameters
bINPUT rhs of the problem
xOUTPUT solution to the problem
Returns
Return a success flag 0 indicates a success ~0 Some error occurred, see the LAPACK documentation

Definition at line 282 of file BandMatrix.cpp.

References BandMatrix::m_n.

Referenced by MultiNewton::step().

int solve ( doublereal *  b)
virtual

Solve the matrix problem Ax = b.

Parameters
bINPUT rhs of the problem OUTPUT solution to the problem
Returns
Return a success flag 0 indicates a success ~0 Some error occurred, see the LAPACK documentation

Implements GeneralMatrix.

Definition at line 288 of file BandMatrix.cpp.

References DATA_PTR, BandMatrix::factor(), BandMatrix::ipiv(), BandMatrix::ldim(), BandMatrix::ludata, BandMatrix::m_factored, BandMatrix::nColumns(), BandMatrix::nSubDiagonals(), and BandMatrix::nSuperDiagonals().

vector_fp::iterator begin ( )
virtual

Returns an iterator for the start of the band storage data.

Iterator points to the beginning of the data, and it is changeable.

Implements GeneralMatrix.

Definition at line 308 of file BandMatrix.cpp.

References BandMatrix::data, and BandMatrix::m_factored.

vector_fp::iterator end ( )

Returns an iterator for the end of the band storage data.

Iterator points to the end of the data, and it is changeable.

Definition at line 314 of file BandMatrix.cpp.

References BandMatrix::data, and BandMatrix::m_factored.

vector_fp::const_iterator begin ( ) const
virtual

Returns a const iterator for the start of the band storage data.

Iterator points to the beginning of the data, and it is not changeable.

Implements GeneralMatrix.

Definition at line 320 of file BandMatrix.cpp.

References BandMatrix::data.

vector_fp::const_iterator end ( ) const

Returns a const iterator for the end of the band storage data.

Iterator points to the end of the data, and it is not changeable.

Definition at line 325 of file BandMatrix.cpp.

References BandMatrix::data.

void zero ( )
virtual

Zero the matrix.

Implements GeneralMatrix.

Definition at line 128 of file BandMatrix.cpp.

References BandMatrix::data, and BandMatrix::m_factored.

int factorQR ( )
virtual

Factors the A matrix using the QR algorithm, overwriting A.

we set m_factored to 2 to indicate the matrix is now QR factored

Returns
Returns the info variable from lapack

Implements GeneralMatrix.

Definition at line 354 of file BandMatrix.cpp.

References BandMatrix::factor().

doublereal rcondQR ( )
virtual

Returns an estimate of the inverse of the condition number for the matrix.

The matrix must have been previously factored using the QR algorithm

Returns
returns the inverse of the condition number

Implements GeneralMatrix.

Definition at line 367 of file BandMatrix.cpp.

References BandMatrix::oneNorm(), and BandMatrix::rcond().

doublereal rcond ( doublereal  a1norm)
virtual

Returns an estimate of the inverse of the condition number for the matrix.

The matrix must have been previously factored using the LU algorithm

Parameters
a1normNorm of the matrix
Returns
returns the inverse of the condition number

Implements GeneralMatrix.

Definition at line 381 of file BandMatrix.cpp.

References DATA_PTR, Cantera::int2str(), BandMatrix::iwork_, BandMatrix::ludata, BandMatrix::m_factored, BandMatrix::m_ipiv, BandMatrix::m_kl, BandMatrix::m_ku, BandMatrix::m_n, BandMatrix::work_, and Cantera::writelogf().

Referenced by BandMatrix::rcondQR().

void useFactorAlgorithm ( int  fAlgorithm)
virtual

Change the way the matrix is factored.

Parameters
fAlgorithminteger 0 LU factorization 1 QR factorization

Implements GeneralMatrix.

Definition at line 418 of file BandMatrix.cpp.

int factorAlgorithm ( ) const
virtual

Returns the factor algorithm used.

0 LU decomposition
1 QR decomposition

This routine will always return 0

Implements GeneralMatrix.

Definition at line 423 of file BandMatrix.cpp.

doublereal oneNorm ( ) const
virtual

Returns the one norm of the matrix.

Implements GeneralMatrix.

Definition at line 429 of file BandMatrix.cpp.

References BandMatrix::m_colPtrs, BandMatrix::m_kl, BandMatrix::m_ku, BandMatrix::m_n, and BandMatrix::value().

Referenced by BandMatrix::rcondQR().

GeneralMatrix * duplMyselfAsGeneralMatrix ( ) const
virtual

Duplicate this object as a GeneralMatrix pointer.

Implements GeneralMatrix.

Definition at line 499 of file BandMatrix.cpp.

References BandMatrix::BandMatrix().

bool factored ( ) const
virtual

Report whether the current matrix has been factored.

Implements GeneralMatrix.

Definition at line 505 of file BandMatrix.cpp.

References BandMatrix::m_factored.

doublereal * ptrColumn ( size_t  j)
virtual

Return a pointer to the top of column j, column values are assumed to be contiguous in memory.

The LAPACK bandstructure has column values which are contiguous in memory:

    On entry, the matrix A in band storage, in rows KL+1 to
    2*KL+KU+1; rows 1 to KL of the array need not be set.
    The j-th column of A is stored in the j-th column of the
    array AB as follows:
    AB(KL + KU + 1 + i - j,j) = A(i,j) for max(1, j - KU) <= i <= min(m, j + KL)

This routine returns the position of AB(1,j) (fortran-1 indexing) in the above format

So to address the (i,j) position, you use the following indexing:

double *colP_j = matrix.ptrColumn(j); double a_i_j = colP_j[kl + ku + i - j];

Parameters
jValue of the column
Returns
Returns a pointer to the top of the column

Implements GeneralMatrix.

Definition at line 516 of file BandMatrix.cpp.

References BandMatrix::m_colPtrs.

doublereal *const * colPts ( )
virtual

Return a vector of const pointers to the columns.

Note the value of the pointers are protected by their being const. However, the value of the matrix is open to being changed.

Returns
returns a vector of pointers to the top of the columns of the matrices.

Implements GeneralMatrix.

Definition at line 529 of file BandMatrix.cpp.

References BandMatrix::m_colPtrs.

void copyData ( const GeneralMatrix y)
virtual

Copy the data from one array into another without doing any checking.

This differs from the assignment operator as no resizing is done and memcpy() is used.

Parameters
yArray to be copied

Implements GeneralMatrix.

Definition at line 539 of file BandMatrix.cpp.

References BandMatrix::data, DATA_PTR, BandMatrix::m_factored, BandMatrix::m_kl, BandMatrix::m_ku, BandMatrix::m_n, and GeneralMatrix::ptrColumn().

void clearFactorFlag ( )
virtual

Clear the factored flag.

Implements GeneralMatrix.

Definition at line 550 of file BandMatrix.cpp.

References BandMatrix::m_factored.

size_t checkRows ( doublereal &  valueSmall) const
virtual

Check to see if we have any zero rows in the jacobian.

This utility routine checks to see if any rows are zero. The smallest row is returned along with the largest coefficient in that row

Parameters
valueSmallOUTPUT value of the largest coefficient in the smallest row
Returns
index of the row that is most nearly zero

Implements GeneralMatrix.

Definition at line 447 of file BandMatrix.cpp.

References BandMatrix::m_kl, BandMatrix::m_ku, BandMatrix::m_n, Cantera::npos, and BandMatrix::value().

size_t checkColumns ( doublereal &  valueSmall) const
virtual

Check to see if we have any zero columns in the jacobian.

This utility routine checks to see if any columns are zero. The smallest column is returned along with the largest coefficient in that column

Parameters
valueSmallOUTPUT value of the largest coefficient in the smallest column
Returns
index of the column that is most nearly zero

Implements GeneralMatrix.

Definition at line 473 of file BandMatrix.cpp.

References BandMatrix::m_kl, BandMatrix::m_ku, BandMatrix::m_n, Cantera::npos, and BandMatrix::value().

void err ( std::string  msg) const
private

Error function that gets called for unhandled cases.

Parameters
msgString containing the message.

Definition at line 343 of file BandMatrix.cpp.

Member Data Documentation

vector_fp data
protected
vector_fp ludata
protected
bool m_factored
protected
size_t m_n
protected
size_t m_kl
protected
size_t m_ku
protected
doublereal m_zero
protected

value of zero

Definition at line 406 of file BandMatrix.h.

Referenced by BandMatrix::value().

vector_int m_ipiv
protected
std::vector<doublereal*> m_colPtrs
protected
vector_int iwork_
protected

Extra work array needed - size = n.

Definition at line 415 of file BandMatrix.h.

Referenced by BandMatrix::rcond().

vector_fp work_
protected

Extra dp work array needed - size = 3n.

Definition at line 418 of file BandMatrix.h.

Referenced by BandMatrix::rcond().

int matrixType_
inherited

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