#ifndef MATRIXDEF_H #define MATRIXDEF_H /****************************************************************************** * Common definitions for Matrix and Vector classes * This header file contains macros and inline functions needed for the matrix * classes. All error checking should be defined here as a macro so that it is * neatly disabled when EXTENDED_ERROR_CHECKING is not defined ******************************************************************************/ /****************************************************************************** * Headers and namespaces used by Matrix and Vector classes ******************************************************************************/ #include #include #include #include #include #include #include #include #include "StringManip.h" #include "Utility.h" using std::cout; using std::ostream; using std::fstream; using std::map; using std::vector; using std::string; using std::scientific; using std::showbase; /****************************************************************************** * Typedefs used by Matrix and Vector classes ******************************************************************************/ //* @typedef INDEX //* @brief indexing type (default: unsigned) for matrix classes typedef unsigned INDEX; //* @typedef CLONE_TYPE //* @brief dimension of matrix to clone enum CLONE_TYPE { CLONE_ROW=0, CLONE_COL=1, CLONE_DIAG=2 }; //* @struct TRIPLET //* @brief Triplet output entity template struct TRIPLET { TRIPLET(int _i=0, int _j=0, T _v=T(0)) : i(_i), j(_j), v(_v) {} INDEX i, j; T v; }; /****************************************************************************** * Definitions for row/column major storage ******************************************************************************/ #define COL_STORAGE /* <--- comment out this line for row-major storage*/ #ifdef COL_STORAGE #define DATA(i,j) _data[(i)+_nRows*(j)] #else #define ROW_STORAGE #define DATA(i,j) _data[(i)*_nCols+(j)] #endif /****************************************************************************** * error checking macros * MICK: checks if index (i,j) is in range MATRIX ONLY * VICK: checks if index (i) is in range VECTOR ONLY * MICM: checks if index (i,j) is in range, displays message MATRIX ONLY * VICM: checks if index (i) is in range, displays message VECTOR ONLY * SQCK: checks if matrix is square, displays message MATRIX ONLY * SSCK: checks if a has the same size as b VECTOR/MATRIX * GCK: generic two object check, checks if c is true VECTOR/MATRIX * GCHK: generic check, checks if c is true ANYTHING ******************************************************************************/ #define MICK(i,j) /**/ #define VICK(i) /**/ #define MICM(i,j,m) /**/ #define VICM(i,m) /**/ #define SQCK(a,m) /**/ #define SSCK(a,b,m) /**/ #define SSCK(a,b,m) /**/ #define GCK(a,b,c,m) /**/ #define GCHK(c,m) /**/ // the following two convert __LINE__ to a string #define STRING2(x) #x #define STRING(x) STRING2(x) // prints file and line number for error messages #define ERROR(x) __FILE__":"STRING(__LINE__)" "x /****************************************************************************** * Shortcuts for Vector and Matrix indexing ******************************************************************************/ #define MIDX(i,j) (*this)(i,j) #define VIDX(i) (*this)[i] /****************************************************************************** * Shortcuts for Vector and Matrix loops over all elements ******************************************************************************/ #define FORi for(INDEX i=0; isize(); i++) #define FORij for(INDEX i=0; i class Matrix; template class DenseMatrix; template class SparseMatrix; template class SparseVector; template class DiagonalMatrix; template class Vector; template class DenseVector; template class CloneVector; //* forward declaration of operations //@{ template DenseVector operator*(const Matrix &M, const SparseVector &v); template DenseVector operator*(const SparseVector &v, const Matrix &M); template SparseVector operator*(const SparseMatrix &M, const SparseVector &v); template SparseVector operator*(const SparseVector &v, const SparseMatrix &M); template DenseVector operator*(const SparseMatrix &A, const Vector& x); template DenseVector operator*(const Vector &A, const SparseMatrix& x); template DenseMatrix operator*(const SparseMatrix &A, const Matrix& D); template SparseMatrix operator*(const SparseMatrix &A, const DiagonalMatrix& D); template SparseMatrix operator*(const SparseMatrix &A, const SparseMatrix &B); template T dot(const SparseVector &a, const SparseVector &b); //@} template CloneVector column(Matrix &c, INDEX i) { return CloneVector(c, CLONE_COL, i); } template CloneVector row(Matrix &c, INDEX i) { return CloneVector(c, CLONE_ROW, i); } template CloneVector diagonal(Matrix &c) { return CloneVector(c, CLONE_DIAG); } template const CloneVector column(const Matrix &c, INDEX i) { return CloneVector(c, CLONE_COL, i); } template const CloneVector row(const Matrix &c, INDEX i) { return CloneVector(c, CLONE_ROW, i); } template const CloneVector diagonal(const Matrix &c) { return CloneVector(c, CLONE_DIAG); } template const SparseMatrix *sparse_cast(const Matrix *m); template const DiagonalMatrix *diag_cast(const Matrix *m); template void copy_sparse_to_matrix(const SparseMatrix *s, Matrix &m); // double precision shortcuts typedef Matrix MATRIX; // matrix of double typedef Vector VECTOR; // vector of double typedef DenseMatrix DENS_MAT; // dense matrix of double type typedef CloneVector CLON_VEC; // cloned vector of double type typedef DenseVector DENS_VEC; // dense vector of double type typedef DiagonalMatrix DIAG_MAT; // diagonal matrix of double type typedef SparseMatrix SPAR_MAT; // sparse matrix of double type typedef SparseVector SPAR_VEC; // sparse matrix of double type typedef Vector IVECTOR; // general Vector of INDEX type // forward declaration of error messages template void ierror(const Matrix &a, const char *FILE, int LINE, INDEX i, INDEX j=0); template void ierror(const Matrix &a, INDEX i, INDEX j, const string m); template void merror(const Matrix &a, const Matrix &b, const string m); inline void gerror(const string m) { cout<<"Error: "<