📄 mmatrix.h
字号:
// file: $isip/class/math/matrix/MMatrix/MMatrix.h// version: $Id: MMatrix.h,v 1.98 2002/03/05 15:15:15 gao Exp $//// make sure definitions are only made once//#ifndef ISIP_MMATRIX#define ISIP_MMATRIX// isip include files//#ifndef ISIP_MMATRIX_METHODS#include "MMatrixMethods.h"#endif#ifndef ISIP_VECTOR_LONG#include <VectorLong.h>#endif#ifndef ISIP_NAME_MAP#include <NameMap.h>#endif// MMatrix: this is a matrix template class which is inherited by// other matrix classes. TScalar is a Scalar class, TIntegral is an// Integral type.//// this class supports 6 types of matrices:// FULL: a rectangular matrix.// DIAGONAL: a diagonal matrix // SYMMETRIC: a symmetric matrix.// LOWER_TRIANGULAR: a lower triangular matrix. // UPPER_TRIANGULAR: an upper triangular matrix. // SPARSE: a sparse matrix (has very few non-zero elements).//// these are defined by the constant Integral::MTYPE. this important// constant is located in Integral.h so that the notion of a matrix// storage type is available to both MMatrix.h and MMatrixMethods.h,// as well as the specific instantiations of this template.//template <class TScalar, class TIntegral>class MMatrix { //--------------------------------------------------------------------------- // // public constants // //---------------------------------------------------------------------------public: // define the class name // static const String CLASS_NAME; //---------------------------------------- // // i/o related constants // //---------------------------------------- static const String DEF_PARAM; static const String PARAM_DATA; static const String PARAM_TYPE; static const String PARAM_NROWS; static const String PARAM_NCOLS; static const String PARAM_ROW_IND; static const String PARAM_COL_IND; //---------------------------------------- // // matrix type-related constants // //---------------------------------------- static const NameMap TYPE_MAP; //---------------------------------------- // // other numeric constants // //---------------------------------------- // define a conditioning factor to be used to determine singularity // static const double THRESH_SINGULAR = 1e-10; // define a conditioning factor to be used to determine threshhold // for rank method // static const double THRESH_SINGULAR_DOUBLE = 2.22e-16; // for double type static const double THRESH_SINGULAR_FLOAT = 1.49e-08; // for float type // define a threshold used to determine sparseness // static const double THRESH_SPARSE = 0.9; // define a threshold used to determine balance // static const double THRESH_BALANCE = 0.95; // define a threshold used to stabilize matrices // static const double THRESH_STABLE = 1e-20; //---------------------------------------- // // default values and arguments // //---------------------------------------- // default arguments to methods // static const unichar DEF_ROW_DELIM = L','; static const unichar DEF_DELIM = DEF_ROW_DELIM; // default value(s) of the class data // static const TIntegral DEF_VALUE; static const long DEF_SIZE = 0; //---------------------------------------- // // error codes // //---------------------------------------- static const long ERR = 24000; static const long ERR_OPTYPE = 24001; static const long ERR_DIM = 24002; static const long ERR_UNKTYP = 24003; static const long ERR_SINGLR = 24004; static const long ERR_POSDEF = 24005; //--------------------------------------------------------------------------- // // protected data // //---------------------------------------------------------------------------protected: typedef MVector<TScalar, TIntegral> TVector; // define the type // Integral::MTYPE type_d; // number of rows and columns in the matrix // Long nrows_d; Long ncols_d; // the values of this matrix are stored in a vector for two reasons. // first, we want to make use of all the math operations supported // in the vector classes. second, we want to make access and manipulation // of the matrix fast. // TVector m_d; // for the sparse matrix implementation, in addition to the values, // we need two vectors to hold row and column indices. // MVector<Long, long> row_index_d; MVector<Long, long> col_index_d; // declare a static debug level // static Integral::DEBUG debug_level_d; //--------------------------------------------------------------------------- // // required public methods // //---------------------------------------------------------------------------public: // method: name // static const String& name() { return CLASS_NAME; } // method: diagnose // static boolean diagnose(Integral::DEBUG debug_level) { return MMatrixMethods::diagnose<MMatrix<TScalar,TIntegral>, TScalar, TIntegral>(debug_level); } // method: setDebug // boolean setDebug(Integral::DEBUG level) { debug_level_d = level; return true; } // method: debug // boolean debug(const unichar* message) const { return debug(name(), message); } // method: destructor // note that since there are no pointers in the protected data, // we don't need to clean up anything. // ~MMatrix() {} // method: default constructor // MMatrix(); // method: copy constructor // MMatrix(const MMatrix& arg); // method: assign // this method is templatized so it can also do conversions without // generating class-specific code. // template <class TAScalar, class TAIntegral> boolean assign(const MMatrix<TAScalar, TAIntegral>& arg) { return MMatrixMethods::assign(*this, arg); } // operator= methods: // these methods are omitted because they are defined in the // classes that instantiate this template // // i/o methods // long sofSize() const { return MMatrixMethods::sofSize<TScalar,TIntegral>(*this); } boolean read(Sof& sof, long tag, const String& name) { return MMatrixMethods::read<TScalar,TIntegral>(*this, sof, tag, name); } boolean write(Sof& sof, long tag, const String& name) const { return MMatrixMethods::write<TScalar,TIntegral>(*this, sof, tag, name); } boolean readData(Sof& sof, const String& pname, long size, boolean param, boolean nested) { return MMatrixMethods::readData<TScalar,TIntegral>(*this, sof, pname, size, param, nested); } boolean writeData(Sof& sof, const String& name) const { return MMatrixMethods::writeData<TScalar,TIntegral>(*this, sof, name); } // equality methods // boolean eq(const MMatrix& arg) const; // memory management methods: // the new and delete methods are omitted because they are defined in the // classes that instantiate this template // boolean clear(Integral::CMODE cmode = Integral::DEF_CMODE); //--------------------------------------------------------------------------- // // class-specific public methods: // extensions to required methods // //--------------------------------------------------------------------------- // debug methods: // this second debug method is used so that the template // instantiations can use a common debug method. // boolean debug(const String& name, const unichar* message) const; // constructor(s) // MMatrix(long nrows, long ncols = DEF_SIZE, Integral::MTYPE type = Integral::DEF_MTYPE); // method: eq // boolean eq(TIntegral value) const { return MMatrixMethods::eq<TScalar,TIntegral>(*this, value); } // method: assign // this method is an overload of the required assign method that // allows the user to set the type. note that we do not use a default // value for the type argument so we avoid a conflict with the standard // assign method. also // template <class TAScalar, class TAIntegral> boolean assign(const MMatrix<TAScalar, TAIntegral>& arg, Integral::MTYPE type) { return ((((void*)&arg == (void*)this) && changeType(type)) || (setDimensions(0, 0, false, type) && copy(arg))); } // other scalar assign methods // boolean assign(TIntegral value); // method: assign // array conversion methods // template<class TAIntegral> boolean assign(long num_rows, long num_cols, const TAIntegral* arg, Integral::MTYPE type = Integral::DEF_MTYPE) { return MMatrixMethods::assign<TScalar,TIntegral>(*this, num_rows, num_cols, arg, type); } // method: assign // boolean assign(long num_rows, long num_cols, const String* arg, Integral::MTYPE type = Integral::DEF_MTYPE) { return MMatrixMethods::assign<TScalar,TIntegral>(*this, num_rows, num_cols, arg, type); } // method: assign // create a matrix from a string with given delimiter // boolean assign(long nrows, long ncols, const String& arg, Integral::MTYPE type = Integral::DEF_MTYPE, const Char delim = DEF_DELIM) { return MMatrixMethods::assign<TScalar,TIntegral>(*this, nrows, ncols, arg, type, delim); } // method: assign // create a matrix from an array of unichars with given delimiter // boolean assign(long nrows, long ncols, const unichar* arg, Integral::MTYPE type = Integral::DEF_MTYPE, const Char delim = DEF_DELIM) { String temp((unichar*)arg); return MMatrixMethods::assign<TScalar,TIntegral>(*this, nrows, ncols, temp, type, delim); } // method: copy // copy the values from arg to "this" without changing the type of "this" // template <class TAScalar, class TAIntegral> boolean copy(const MMatrix<TAScalar, TAIntegral>& arg) { return MMatrixMethods::copy(*this, arg); } // method: swap // boolean swap(MMatrix& arg) { return MMatrixMethods::swap<TScalar,TIntegral>(*this, arg); } //--------------------------------------------------------------------------- // // class-specific public methods: // operator overloads, get, set, find, resize methods // //--------------------------------------------------------------------------- // method: operator() // TIntegral operator()(long i, long j) const { return getValue(i, j); } // method: getType // Integral::MTYPE getType() const { return type_d; } // method: getValue // TIntegral getValue(long row_index, long col_index) const { return MMatrixMethods::getValue<TScalar,TIntegral>(*this, row_index, col_index); } // method: getValue // boolean getValue(TScalar& value, long row_index, long col_index) const { return MMatrixMethods::getValue<TScalar,TIntegral>(*this, value, row_index, col_index); } // method: getNumRows // long getNumRows() const { return nrows_d; } // method: getNumColumns // long getNumColumns() const { return ncols_d; } // method: getRow // boolean getRow(TVector& vector, long row_index) const { return MMatrixMethods::getRow<TScalar,TIntegral>(*this, vector, row_index); } // method: getColumn // boolean getColumn(TVector& vector, long col_index) const { return MMatrixMethods::getColumn<TScalar,TIntegral>(*this, vector, col_index); } // method: getDiagonal // boolean getDiagonal(TVector& vector) const { return MMatrixMethods::getDiagonal<TScalar,TIntegral>(*this, vector); } // method: getLower // boolean getLower(MMatrix& arg) const { return MMatrixMethods::getLower<TScalar,TIntegral>(*this, arg); } // method: getUpper // boolean getUpper(MMatrix& arg) const { return MMatrixMethods::getUpper<TScalar,TIntegral>(*this, arg); } // method: getMinor // boolean getMinor(MMatrix& arg, long row_index, long col_index) const { return MMatrixMethods::getMinor<TScalar, TIntegral>(*this, arg, row_index, col_index); } // method: setValue // boolean setValue(long row_index, long col_index, const TScalar& value) { return MMatrixMethods::setValue<TScalar,TIntegral>(*this, row_index, col_index, (TIntegral)value); } // method: setRow // boolean setRow(long row_index, const TVector& vector) { return MMatrixMethods::setRow<TScalar,TIntegral>(*this, row_index, vector); } // method: setColumn // boolean setColumn(long col_index, const TVector& vector) { return MMatrixMethods::setColumn<TScalar,TIntegral>(*this, col_index, vector); } // method: setDiagonal // boolean setDiagonal(const TVector& values) { return MMatrixMethods::setDiagonal<TScalar,TIntegral>(*this, values); } // method: setDiagonal // boolean setDiagonal(const MMatrix& source) { TVector tmp; source.getDiagonal(tmp); return MMatrixMethods::setDiagonal<TScalar,TIntegral>(*this, tmp); } // method: setDiagonal // boolean setDiagonal(TIntegral value) { TVector tmp(nrows_d); tmp.assign(value); return MMatrixMethods::setDiagonal<TScalar,TIntegral>(*this, tmp); } // method: setLower // boolean setLower(const MMatrix& source) { return MMatrixMethods::setLower<TScalar,TIntegral>(*this, source); } // method: setUpper // boolean setUpper(const MMatrix& source) { return MMatrixMethods::setUpper<TScalar,TIntegral>(*this, source); } // method: setBlock // boolean setBlock(long start_row, long start_col, long num_rows, long num_cols, TIntegral value) { return MMatrixMethods::setBlock<TScalar, TIntegral>(*this, start_row, start_col, num_rows, num_cols, value); } // method: makeDiagonal // boolean makeDiagonal(const TVector& values, Integral::MTYPE type = Integral::DIAGONAL) { return MMatrixMethods::makeDiagonal<TScalar,TIntegral>(*this, values, type); } // method: makeDiagonal // boolean makeDiagonal(TIntegral value, long dim, Integral::MTYPE type = Integral::DIAGONAL) { TVector tmp(dim); tmp.assign(value); return MMatrixMethods::makeDiagonal<TScalar,TIntegral>(*this, tmp, type); } // method: makeIdentity // boolean makeIdentity(long dim, Integral::MTYPE type = Integral::DIAGONAL) { TVector tmp(dim); tmp.assign((TIntegral)1.0); return MMatrixMethods::makeDiagonal<TScalar,TIntegral>(*this, tmp, type); } // method: makeLower // boolean makeLower(const MMatrix& source, Integral::MTYPE type = Integral::LOWER_TRIANGULAR) { return MMatrixMethods::makeLower<TScalar,TIntegral>(*this, source, type); } // method: makeUpper // boolean makeUpper(const MMatrix& source, Integral::MTYPE type = Integral::UPPER_TRIANGULAR) { return MMatrixMethods::makeUpper<TScalar,TIntegral>(*this, source, type); } // method: nextZero //
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -