Module: Linear Algebra Group: Sparse Matrix Classes
Does not inherit
#include <rw/lapack/bandmat.h> RWBandMat<double> d;
The class RWBandMat<T> encapsulates a banded matrix. A banded matrix is nonzero only near the diagonal. Specifically, if u is the upper bandwidth and l the lower, the matrix entry Aij is defined as 0 if either j-i>u or i-j>l. The total bandwidth, simply called bandwidth, is the sum of the upper and lower bandwidths plus 1, for the diagonal. See the Storage Scheme section of this entry for an example of a banded matrix.
#include <iostream> #include <rw/lapack/bandmat.h> int main(){ RWBandMat<double> d(9, 9, 2, 1); d.diagonal(-2) = 1; d.diagonal(-1) = 2; d.diagonal( ) = 3; d.diagonal( 1) = 4; std::cout << d; return 0; }
The matrix is stored column-by-column. There are some unused locations in the vector of data so that each column takes up the same number of entries. For example, the following 9x9 matrix has an upper bandwidth of 1, a lower bandwidth of 2, and thus a total bandwidth of 4:
This matrix is stored as follows:
[ XXX A11 A21 A31 A12 A22 A32 A42 A23 A33 A43 A53
A34 A44 A54 A64 A45 A55 A65 A75 A56 A66 A76 A86
A67 A77 A87 A97 A78 A88 A98 XXX A89 A99 XXX XXX ]
where XXX indicates an unused storage location. The mapping between array entry Aij and the storage vector is as follows:
RWBandMat();
Default constructor. Builds a matrix of size 0 by 0. Necessary for declaring a matrix with no explicit constructor, or an array of matrices.
RWBandMat(const RWBandMat<T>& A);
Builds a copy of the argument, A. Note that the new matrix references A's data. To construct a matrix with its own copy of the data, use either the copy or deepenShallowCopy member functions.
RWBandMat(unsigned n, unsigned n, unsigned lb,unsigned ub);
Defines an uninitialized matrix of size n x n with lower bandwidth lb and upper bandwidth ub.
RWBandMat(const RWMathVec<T>& vd, unsigned n,
unsigned n, unsigned lb, unsigned ub);
Constructs a size n x n matrix, with lower bandwidth lb and upper bandwidth ub, using the data in the passed vector. This data must be stored in the format described in the Storage Scheme section. The resultant matrix references the data in vector vd.
RWBandMat<DComplex>(const RWBandMat<double>& re); RWBandMat<DComplex>(const RWBandMat<double>& re, const RWBandMat<double>& im);
Constructs a complex matrix from the real and imaginary parts supplied. If no imaginary part is supplied, it is assumed to be 0.
RWBandMat<double>(const RWBandMat<float>&);
Constructs a copy of the argument matrix with double precision entries.
RWBandMat<float>(const RWSymBandMat<float>&); RWBandMat<double>(const RWSymBandMat<float>&); RWBandMat<DComplex>(const RWSymBandMat<DComplex>&); RWBandMat<DComplex>(const RWHermBandMat<DComplex>&); RWBandMat<T>(const RWTriDiagMat<T>&);
Constructs a banded matrix from a symmetric banded, Hermitian banded, or tridiagonal matrix.
unsigned bandwidth();
Returns the bandwidth of the matrix. The bandwidth is the sum of the upper and lower bandwidths plus 1 for the main diagonal.
RWMathVec<T> bcdiagonal(int j=0);
Returns a reference to the jth diagonal of the matrix, after doing bounds checking. The main diagonal is indexed by 0, diagonals in the upper triangle are indexed with positive integers, and diagonals in the lower triangle are indexed with negative integers.
RWRORef<T> bcref(int i, int j);
Returns a reference to the ijth element of the matrix, after doing bounds checking.
void bcset(int i, int j, T x);
Sets the ijth element of the matrix equal to x, after doing bounds checking.
T bcval(int i, int j);
Returns the value of the ijth element of the matrix, after doing bounds checking.
unsigned binaryStoreSize();
Returns the number of bytes that it would take to write the matrix to a file using saveOn.
unsigned cols();
Returns the number of columns in the matrix.
RWBandMat<T> copy();
Creates a copy of this matrix with distinct data. The stride of the data vector in the new matrix is guaranteed to be 1.
T* data();
Returns a pointer to the first item of data in the vector storing the matrix's data. You can use this (with caution!) to pass the matrix's data to C or FORTRAN subroutines. Be aware that the stride of the data vector may not be 1.
RWMathVec<T> dataVec();
Returns the matrix's data vector, where the explicitly stored entries in the matrix are kept.
RWBandMat<T> deepCopy();
Creates a copy of this matrix with distinct data. The stride of the data vector in the new matrix is guaranteed to be 1.
void deepenShallowCopy();
Ensures that the data in the matrix is not shared by any other matrix or vector. Also ensure that the stride in the data vector is equal to 1. If necessary, a new copy of the data vector is made.
RWMathVec<T> diagonal(int j=0);
Returns a reference to the jth diagonal of the matrix. The main diagonal is indexed by 0, diagonals in the upper triangle are indexed with positive integers, and diagonals in the lower triangle are indexed with negative integers. Bounds checking is done if the preprocessor symbol BOUNDS_CHECK is defined when the header file is read. The member function bcdiagonal does the same thing with guaranteed bounds checking.
RWBandMat<T> leadingSubmatrix(int k);
Returns the k x k upper left corner of the matrix. The submatrix and the matrix share the same data.
unsigned lowerBandwidth();
Returns the lower bandwidth of the matrix.
void printOn(ostream&);
Prints the matrix to an output stream in human readable format.
RWRORef<T> ref(int i, int j);
Returns a reference to the ijth element of the matrix. Bounds checking is done if the preprocessor symbol BOUNDS_CHECK is defined when the header file is read. The member function bcref does the same thing with guaranteed bounds checking.
RWBandMat<T> reference(RWBandMat<T>&);
Makes this matrix a reference to the argument matrix. The two matrices share the same data. The matrices do not have to be the same size before calling reference. To copy a matrix into another of the same size, use the operator= member operator.
void resize(unsigned n, unsigned n); void resize(unsigned n, unsigned n, unsigned lb, unsigned ub);
Resizes the matrix or changes both its size and lower and upper bandwidths. Any new entries in the matrix are set to 0.
void restoreFrom(RWFile&);
Reads in a matrix from an RWFile. The matrix must have been stored to the file using the saveOn member function.
void restoreFrom(RWvistream&);
Reads in a matrix from an RWvistream, the Rogue Wave virtual input stream class. The matrix must have been stored to the stream using the saveOn member function.
unsigned rows();
Returns the number of rows in the matrix.
void saveOn(RWFile&);
Stores a matrix to an RWFile. The matrix can be read using the restoreFrom member function.
void saveOn(RWvostream&);
Stores a matrix to an RWvostream, the Rogue Wave virtual output stream class. The matrix can be read using the restoreFrom member void
scanFrom(istream&);
Reads a matrix from an input stream. The format of the matrix is the same as the format output by the printTo member function: first the lower bandwidth, followed by the upper bandwidth, followed by the matrix itself. The sample matrix below shows the format. Note that extra white space and text preceding the bandwidth specification are ignored. Only the relevant band of the matrix is used.
2 1 5x5 [ 1 1 0 0 0 7 4 3 0 0 5 5 4 5 0 0 1 3 1 1 0 0 8 1 4 ]
void set(int i, int j, T x);
Sets the ijth element of the matrix equal to x. Bounds checking is done if the preprocessor symbol BOUNDS_CHECK is defined when the header file is read. The member function bcset does the same thing with guaranteed bounds checking.
unsigned upperBandwidth();
Returns the upper bandwidth of the matrix.
T val(int i, int j);
Returns the value of the ijth element of the matrix. Bounds checking is done if the preprocessor symbol BOUNDS_CHECK is defined when the header file is read. The member function bcval does the same thing with guaranteed bounds checking.
RWBandMat<T> zero();
Sets every element of the matrix to 0.
RWRORef<T> RWBandMat<T>::operator() (int i, int j); T RWBandMat<T>::operator() (int i, int j) const;
Accesses the ijth element. If the matrix is not a const matrix, a reference type is returned, so this operator can be used for assigning or accessing an element. In these cases, using this operator is equivalent to calling the ref member function. If the matrix is a const matrix, a value is returned, so this operator can be used only for accessing an element. In this case, using this operator is equivalent to calling the val member function. Bounds checking is done if the preprocessor symbol BOUNDS_CHECK is defined before including the header file.
RWBandMat<T>& operator=(const RWBandMat<T>& A);
Sets the matrix elements equal to the elements of A. The two matrices must be the same size. To make the matrix reference the same data as A, use the reference member function.
bool operator==(const RWBandMat<T>& A); bool operator!=(const RWBandMat<T>& A);
Boolean operators. Two matrices are considered equal if they have the same size and their elements are all exactly the same. Be aware that floating point arithmetic is not exact; matrices which are theoretically equal are not always numerically equal.
RWBandMat<T>& operator*=(T x); RWBandMat<T>& operator/=(T x);
Performs the indicated operation on each element of the matrix.
RWBandMat<T>& operator+=(const RWBandMat<T>& A); RWBandMat<T>& operator-=(const RWBandMat<T>& A); RWBandMat<T>& operator*=(const RWBandMat<T>& A); RWBandMat<T>& operator/=(const RWBandMat<T>& A);
Performs element-by-element arithmetic on the data in the matrices. In particular, note that operator*= does element-by-element multiplication, not inner product style matrix multiplication. You can use the product global function to do matrix-matrix inner product multiplication.
RWBandMat<T> operator+(const RWBandMat<T>&); RWBandMat<T> operator-(const RWBandMat<T>&);
Unary plus and minus operators. Each operator returns a copy of the matrix or its negation.
RWBandMat<T> operator+(const RWBandMat<T>&,
const RWBandMat<T>&); RWBandMat<T> operator-(const RWBandMat<T>&,
const RWBandMat<T>&); RWBandMat<T> operator*(const RWBandMat<T>&,
const RWBandMat<T>&); RWBandMat<T> operator/(const RWBandMat<T>&,
const RWBandMat<T>&);
Performs element-by-element operations on the arguments. To do inner product matrix multiplication, use the product global function.
RWBandMat<T> operator*(T,const RWBandMat<T>&); RWBandMat<T> operator*(const RWBandMat<T>&,T); RWBandMat<T> operator/(const RWBandMat<T>&,T);
Performs element-by-element operations on the arguments.
ostream& operator<<(ostream& s, const RWBandMat<T>&);
Writes the matrix to the stream. This is equivalent to calling the printOn member function.
istream& operator>>(istream& s, const RWBandMat<T>&);
Reads the matrix from the stream. This is equivalent to calling the scanFrom member function.
RWBandMat<T> abs(const RWBandMat<T>&);
Returns a matrix whose entries are the absolute value of the argument. The absolute value of a complex number is considered to be the sum of the absolute values of its real and imaginary parts. To get the norm of a complex matrix, you can use the norm function.
RWBandMat<double> arg(const RWBandMat<DComplex>& A);
Returns a matrix where each element is the argument of the corresponding element in the matrix A.
RWBandMat<DComplex> conj(const RWBandMat<DComplex>& A);
Returns a matrix where each element is the complex conjugate of the corresponding element in the matrix A.
RWBandMat<DComplex> conjTransposeProduct(const RWBandMat<DComplex>& A,
const RWBandMat<DComplex>& B);
Returns the inner product (matrix product) of the complex conjugate transpose of A and the matrix B. This product is a banded matrix whose lower bandwidth is the sum of the upper bandwidth of A and the lower bandwidth of B, and whose upper bandwidth is the sum of the lower bandwidth of A and the upper bandwidth of B.
RWBandMat<double> imag(const RWBandMat<DComplex>& A);
Returns a matrix where each element is the imaginary part of the corresponding element in the matrix A.
double maxValue(const RWBandMat<double>&); float maxValue(const RWBandMat<float>&); double minValue(const RWBandMat<double>&); float minValue(const RWBandMat<float>&);
Returns the maximum or minimum entry in the matrix.
RWBandMat<double> norm(const RWBandMat<DComplex>& A);
Returns a matrix where each element is the norm (magnitude) of the corresponding element in the matrix A.
RWBandMat<T> product(const RWBandMat<T>& A, const RWBandMat<T>& B);
Returns the inner product (matrix product) of A and B. This product is a banded matrix whose lower bandwidth is the sum of the lower bandwidths of A and B, and whose upper bandwidth is the sum of the upper bandwidths of A and B.
RWMathVec<T> product(const RWBandMat<T>& A, const RWMathVec<T>& x);
Returns the inner product (matrix-vector product) of A and x.
RWMathVec<T> product(const RWMathVec<T>& x, const RWBandMat<T>& A);
Returns the inner product (matrix-vector product) of x and A. This is equal to the product of A transpose and x.
RWBandMat<double> real(const RWBandMat<DComplex>& A);
Returns a matrix where each element is the real part of the corresponding element in the matrix A.
RWBandMat<T> toBandMat(const RWGenMat<T>&, unsigned bl, unsigned
bu);
Extracts a band of entries from a square matrix. The main diagonal is extracted, along with bl diagonals from the upper triangle of the matrix, and bl diagonals from the lower triangle of the matrix.
RWBandMat<float> toFloat(const RWBandMat<double>&);
Converts a matrix from double to float precision. The conversion is done using a constructor.
RWBandMat<DComplex> toHermBandMat(const RWBandMat<DComplex>& A);
Extracts the Hermitian part of a banded matrix. The Hermitian part of the banded matrix A is (A+conj(AT))/2.
RWSymBandMat<T> toSymBandMat(const RWBandMat<T>& A);
Extracts the symmetric part of a banded matrix. The symmetric part of the banded matrix A is (A+AT)/2.
RWTriDiagMat<T> toTriDiagMat(const RWBandMat<T>& A);
Extracts the tridiagonal part of a general banded matrix. The tridiagonal part of matrix A consists of the main diagonal, the subdiagonal, and the superdiagonal.
RWBandMat<T> transpose(const RWBandMat<T>&); FloatBandMat
Returns the transpose of the argument matrix.
RWBandMat<T> transposeProduct(const RWBandMat<T>& A,
const RWBandMat<T>& B);
Returns the inner product (matrix product) of the transpose of A and the matrix B. This product is a banded matrix whose lower bandwidth is the sum of the upper bandwidth of A and the lower bandwidth of B, and whose upper bandwidth is the sum of the lower bandwidth of A and the upper bandwidth of B.
© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.