Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

newmat.h

Go to the documentation of this file.
00001 //$$ newmat.h           definition file for new version of matrix package
00002 
00003 // Copyright (C) 1991,2,3,4,7,2000,2,3: R B Davies
00004 
00005 #ifndef NEWMAT_LIB
00006 #define NEWMAT_LIB 0
00007 
00008 #include "include.h"
00009 
00010 #include "myexcept.h"
00011 
00012 
00013 #ifdef use_namespace
00014 namespace NEWMAT { using namespace RBD_COMMON; }
00015 namespace RBD_LIBRARIES { using namespace NEWMAT; }
00016 namespace NEWMAT {
00017 #endif
00018 
00019 //#define DO_REPORT                     // to activate REPORT
00020 
00021 #ifdef NO_LONG_NAMES
00022 #define UpperTriangularMatrix UTMatrix
00023 #define LowerTriangularMatrix LTMatrix
00024 #define SymmetricMatrix SMatrix
00025 #define DiagonalMatrix DMatrix
00026 #define BandMatrix BMatrix
00027 #define UpperBandMatrix UBMatrix
00028 #define LowerBandMatrix LBMatrix
00029 #define SymmetricBandMatrix SBMatrix
00030 #define BandLUMatrix BLUMatrix
00031 #endif
00032 
00033 // ************************** general utilities ****************************/
00034 
00035 class GeneralMatrix;
00036 
00037 void MatrixErrorNoSpace(const void*);                 // no space handler
00038 
00039 class LogAndSign
00040 // Return from LogDeterminant function
00041 //    - value of the log plus the sign (+, - or 0)
00042 {
00043    Real log_value;
00044    int sign;
00045 public:
00046    LogAndSign() { log_value=0.0; sign=1; }
00047    LogAndSign(Real);
00048    void operator*=(Real);
00049    void PowEq(int k);  // raise to power of k
00050    void ChangeSign() { sign = -sign; }
00051    Real LogValue() const { return log_value; }
00052    int Sign() const { return sign; }
00053    Real Value() const;
00054    FREE_CHECK(LogAndSign)
00055 };
00056 
00057 // the following class is for counting the number of times a piece of code
00058 // is executed. It is used for locating any code not executed by test
00059 // routines. Use turbo GREP locate all places this code is called and
00060 // check which ones are not accessed.
00061 // Somewhat implementation dependent as it relies on "cout" still being
00062 // present when ExeCounter objects are destructed.
00063 
00064 #ifdef DO_REPORT
00065 
00066 class ExeCounter
00067 {
00068    int line;                                    // code line number
00069    int fileid;                                  // file identifier
00070    long nexe;                                   // number of executions
00071    static int nreports;                         // number of reports
00072 public:
00073    ExeCounter(int,int);
00074    void operator++() { nexe++; }
00075    ~ExeCounter();                               // prints out reports
00076 };
00077 
00078 #endif
00079 
00080 
00081 // ************************** class MatrixType *****************************/
00082 
00083 // Is used for finding the type of a matrix resulting from the binary operations
00084 // +, -, * and identifying what conversions are permissible.
00085 // This class must be updated when new matrix types are added.
00086 
00087 class GeneralMatrix;                            // defined later
00088 class BaseMatrix;                               // defined later
00089 class MatrixInput;                              // defined later
00090 
00091 class MatrixType
00092 {
00093 public:
00094    enum Attribute {  Valid     = 1,
00095                      Diagonal  = 2,             // order of these is important
00096                      Symmetric = 4,
00097                      Band      = 8,
00098                      Lower     = 16,
00099                      Upper     = 32,
00100                      Square    = 64,
00101                      Skew      = 128,
00102                      LUDeco    = 256,
00103                      Ones      = 512 };
00104 
00105    enum            { US = 0,
00106                      UT = Valid + Upper + Square,
00107                      LT = Valid + Lower + Square,
00108                      Rt = Valid,
00109                      Sq = Valid + Square,
00110                      Sm = Valid + Symmetric + Square,
00111                      Sk = Valid + Skew + Square,
00112                      Dg = Valid + Diagonal + Band + Lower + Upper + Symmetric
00113                         + Square,
00114                      Id = Valid + Diagonal + Band + Lower + Upper + Symmetric
00115                         + Square + Ones,
00116                      RV = Valid,     //   do not separate out
00117                      CV = Valid,     //   vectors
00118                      BM = Valid + Band + Square,
00119                      UB = Valid + Band + Upper + Square,
00120                      LB = Valid + Band + Lower + Square,
00121                      SB = Valid + Band + Symmetric + Square,
00122                      Ct = Valid + LUDeco + Square,
00123                      BC = Valid + Band + LUDeco + Square,
00124                      Mask = ~Square
00125                    };
00126 
00127 
00128    static int nTypes() { return 12; }          // number of different types
00129                  // exclude Ct, US, BC
00130 public:
00131    int attribute;
00132    bool DataLossOK;                            // true if data loss is OK when
00133                                                // this represents a destination
00134 public:
00135    MatrixType () : DataLossOK(false) {}
00136    MatrixType (int i) : attribute(i), DataLossOK(false) {}
00137    MatrixType (int i, bool dlok) : attribute(i), DataLossOK(dlok) {}
00138    MatrixType (const MatrixType& mt)
00139       : attribute(mt.attribute), DataLossOK(mt.DataLossOK) {}
00140    void operator=(const MatrixType& mt)
00141       { attribute = mt.attribute; DataLossOK = mt.DataLossOK; }
00142    void SetDataLossOK() { DataLossOK = true; }
00143    int operator+() const { return attribute; }
00144    MatrixType operator+(MatrixType mt) const
00145       { return MatrixType(attribute & mt.attribute); }
00146    MatrixType operator*(const MatrixType&) const;
00147    MatrixType SP(const MatrixType&) const;
00148    MatrixType KP(const MatrixType&) const;
00149    MatrixType operator|(const MatrixType& mt) const
00150       { return MatrixType(attribute & mt.attribute & Valid); }
00151    MatrixType operator&(const MatrixType& mt) const
00152       { return MatrixType(attribute & mt.attribute & Valid); }
00153    bool operator>=(MatrixType mt) const
00154       { return ( attribute & ~mt.attribute & Mask ) == 0; }
00155    bool operator<(MatrixType mt) const         // for MS Visual C++ 4
00156       { return ( attribute & ~mt.attribute & Mask ) != 0; }
00157    bool operator==(MatrixType t) const
00158       { return (attribute == t.attribute); }
00159    bool operator!=(MatrixType t) const
00160       { return (attribute != t.attribute); }
00161    bool operator!() const { return (attribute & Valid) == 0; }
00162    MatrixType i() const;                       // type of inverse
00163    MatrixType t() const;                       // type of transpose
00164    MatrixType AddEqualEl() const               // Add constant to matrix
00165       { return MatrixType(attribute & (Valid + Symmetric + Square)); }
00166    MatrixType MultRHS() const;                 // type for rhs of multiply
00167    MatrixType sub() const                      // type of submatrix
00168       { return MatrixType(attribute & Valid); }
00169    MatrixType ssub() const                     // type of sym submatrix
00170       { return MatrixType(attribute); }        // not for selection matrix
00171    GeneralMatrix* New() const;                 // new matrix of given type
00172    GeneralMatrix* New(int,int,BaseMatrix*) const;
00173                                                // new matrix of given type
00174    const char* Value() const;                  // to print type
00175    friend bool Rectangular(MatrixType a, MatrixType b, MatrixType c);
00176    friend bool Compare(const MatrixType&, MatrixType&);
00177                                                // compare and check conv.
00178    bool IsBand() const { return (attribute & Band) != 0; }
00179    bool IsDiagonal() const { return (attribute & Diagonal) != 0; }
00180    bool IsSymmetric() const { return (attribute & Symmetric) != 0; }
00181    bool CannotConvert() const { return (attribute & LUDeco) != 0; }
00182                                                // used by operator== 
00183    FREE_CHECK(MatrixType)
00184 };
00185 
00186 
00187 // *********************** class MatrixBandWidth ***********************/
00188 
00189 class MatrixBandWidth
00190 {
00191 public:
00192    int lower;
00193    int upper;
00194    MatrixBandWidth(const int l, const int u) : lower(l), upper (u) {}
00195    MatrixBandWidth(const int i) : lower(i), upper(i) {}
00196    MatrixBandWidth operator+(const MatrixBandWidth&) const;
00197    MatrixBandWidth operator*(const MatrixBandWidth&) const;
00198    MatrixBandWidth minimum(const MatrixBandWidth&) const;
00199    MatrixBandWidth t() const { return MatrixBandWidth(upper,lower); }
00200    bool operator==(const MatrixBandWidth& bw) const
00201       { return (lower == bw.lower) && (upper == bw.upper); }
00202    bool operator!=(const MatrixBandWidth& bw) const { return !operator==(bw); }
00203    int Upper() const { return upper; }
00204    int Lower() const { return lower; }
00205    FREE_CHECK(MatrixBandWidth)
00206 };
00207 
00208 
00209 // ********************* Array length specifier ************************/
00210 
00211 // This class is introduced to avoid constructors such as
00212 //   ColumnVector(int)
00213 // being used for conversions
00214 
00215 class ArrayLengthSpecifier
00216 {
00217    int value;
00218 public:
00219    int Value() const { return value; }
00220    ArrayLengthSpecifier(int l) : value(l) {}
00221 };
00222 
00223 // ************************* Matrix routines ***************************/
00224 
00225 
00226 class MatrixRowCol;                             // defined later
00227 class MatrixRow;
00228 class MatrixCol;
00229 class MatrixColX;
00230 
00231 class GeneralMatrix;                            // defined later
00232 class AddedMatrix;
00233 class MultipliedMatrix;
00234 class SubtractedMatrix;
00235 class SPMatrix;
00236 class KPMatrix;
00237 class ConcatenatedMatrix;
00238 class StackedMatrix;
00239 class SolvedMatrix;
00240 class ShiftedMatrix;
00241 class NegShiftedMatrix;
00242 class ScaledMatrix;
00243 class TransposedMatrix;
00244 class ReversedMatrix;
00245 class NegatedMatrix;
00246 class InvertedMatrix;
00247 class RowedMatrix;
00248 class ColedMatrix;
00249 class DiagedMatrix;
00250 class MatedMatrix;
00251 class GetSubMatrix;
00252 class ReturnMatrix;
00253 class Matrix;
00254 class SquareMatrix;
00255 class nricMatrix;
00256 class RowVector;
00257 class ColumnVector;
00258 class SymmetricMatrix;
00259 class UpperTriangularMatrix;
00260 class LowerTriangularMatrix;
00261 class DiagonalMatrix;
00262 class CroutMatrix;
00263 class BandMatrix;
00264 class LowerBandMatrix;
00265 class UpperBandMatrix;
00266 class SymmetricBandMatrix;
00267 class LinearEquationSolver;
00268 class GenericMatrix;
00269 
00270 
00271 #define MatrixTypeUnSp 0
00272 //static MatrixType MatrixTypeUnSp(MatrixType::US);
00273 //            // AT&T needs this
00274 
00275 class BaseMatrix : public Janitor               // base of all matrix classes
00276 {
00277 protected:
00278    virtual int search(const BaseMatrix*) const = 0;
00279             // count number of times matrix
00280               // is referred to
00281 
00282 public:
00283    virtual GeneralMatrix* Evaluate(MatrixType mt=MatrixTypeUnSp) = 0;
00284             // evaluate temporary
00285    // for old version of G++
00286    //   virtual GeneralMatrix* Evaluate(MatrixType mt) = 0;
00287    //   GeneralMatrix* Evaluate() { return Evaluate(MatrixTypeUnSp); }
00288    AddedMatrix operator+(const BaseMatrix&) const;    // results of operations
00289    MultipliedMatrix operator*(const BaseMatrix&) const;
00290    SubtractedMatrix operator-(const BaseMatrix&) const;
00291    ConcatenatedMatrix operator|(const BaseMatrix&) const;
00292    StackedMatrix operator&(const BaseMatrix&) const;
00293    ShiftedMatrix operator+(Real) const;
00294    ScaledMatrix operator*(Real) const;
00295    ScaledMatrix operator/(Real) const;
00296    ShiftedMatrix operator-(Real) const;
00297    TransposedMatrix t() const;
00298 //   TransposedMatrix t;
00299    NegatedMatrix operator-() const;                   // change sign of elements
00300    ReversedMatrix Reverse() const;
00301    InvertedMatrix i() const;
00302 //   InvertedMatrix i;
00303    RowedMatrix AsRow() const;
00304    ColedMatrix AsColumn() const;
00305    DiagedMatrix AsDiagonal() const;
00306    MatedMatrix AsMatrix(int,int) const;
00307    GetSubMatrix SubMatrix(int,int,int,int) const;
00308    GetSubMatrix SymSubMatrix(int,int) const;
00309    GetSubMatrix Row(int) const;
00310    GetSubMatrix Rows(int,int) const;
00311    GetSubMatrix Column(int) const;
00312    GetSubMatrix Columns(int,int) const;
00313    Real AsScalar() const;                      // conversion of 1 x 1 matrix
00314    virtual LogAndSign LogDeterminant() const;
00315    Real Determinant() const;
00316    virtual Real SumSquare() const;
00317    Real NormFrobenius() const;
00318    virtual Real SumAbsoluteValue() const;
00319    virtual Real Sum() const;
00320    virtual Real MaximumAbsoluteValue() const;
00321    virtual Real MaximumAbsoluteValue1(int& i) const;
00322    virtual Real MaximumAbsoluteValue2(int& i, int& j) const;
00323    virtual Real MinimumAbsoluteValue() const;
00324    virtual Real MinimumAbsoluteValue1(int& i) const;
00325    virtual Real MinimumAbsoluteValue2(int& i, int& j) const;
00326    virtual Real Maximum() const;
00327    virtual Real Maximum1(int& i) const;
00328    virtual Real Maximum2(int& i, int& j) const;
00329    virtual Real Minimum() const;
00330    virtual Real Minimum1(int& i) const;
00331    virtual Real Minimum2(int& i, int& j) const;
00332    virtual Real Trace() const;
00333    Real Norm1() const;
00334    Real NormInfinity() const;
00335    virtual MatrixBandWidth BandWidth() const;  // bandwidths of band matrix
00336    virtual void CleanUp() {}                   // to clear store
00337    void IEQND() const;                         // called by ineq. ops
00338 //   virtual ReturnMatrix Reverse() const;       // reverse order of elements
00339 //protected:
00340 //   BaseMatrix() : t(this), i(this) {}
00341 
00342    friend class GeneralMatrix;
00343    friend class Matrix;
00344    friend class SquareMatrix;
00345    friend class nricMatrix;
00346    friend class RowVector;
00347    friend class ColumnVector;
00348    friend class SymmetricMatrix;
00349    friend class UpperTriangularMatrix;
00350    friend class LowerTriangularMatrix;
00351    friend class DiagonalMatrix;
00352    friend class CroutMatrix;
00353    friend class BandMatrix;
00354    friend class LowerBandMatrix;
00355    friend class UpperBandMatrix;
00356    friend class SymmetricBandMatrix;
00357    friend class AddedMatrix;
00358    friend class MultipliedMatrix;
00359    friend class SubtractedMatrix;
00360    friend class SPMatrix;
00361    friend class KPMatrix;
00362    friend class ConcatenatedMatrix;
00363    friend class StackedMatrix;
00364    friend class SolvedMatrix;
00365    friend class ShiftedMatrix;
00366    friend class NegShiftedMatrix;
00367    friend class ScaledMatrix;
00368    friend class TransposedMatrix;
00369    friend class ReversedMatrix;
00370    friend class NegatedMatrix;
00371    friend class InvertedMatrix;
00372    friend class RowedMatrix;
00373    friend class ColedMatrix;
00374    friend class DiagedMatrix;
00375    friend class MatedMatrix;
00376    friend class GetSubMatrix;
00377    friend class ReturnMatrix;
00378    friend class LinearEquationSolver;
00379    friend class GenericMatrix;
00380    NEW_DELETE(BaseMatrix)
00381 };
00382 
00383 
00384 // ***************************** working classes **************************/
00385 
00386 class GeneralMatrix : public BaseMatrix         // declarable matrix types
00387 {
00388    virtual GeneralMatrix* Image() const;        // copy of matrix
00389 protected:
00390    int tag;                                     // shows whether can reuse
00391    int nrows_value, ncols_value;                // dimensions
00392    int storage;                                 // total store required
00393    Real* store;                                 // point to store (0=not set)
00394    GeneralMatrix();                             // initialise with no store
00395    GeneralMatrix(ArrayLengthSpecifier);         // constructor getting store
00396    void Add(GeneralMatrix*, Real);              // sum of GM and Real
00397    void Add(Real);                              // add Real to this
00398    void NegAdd(GeneralMatrix*, Real);           // Real - GM
00399    void NegAdd(Real);                           // this = this - Real
00400    void Multiply(GeneralMatrix*, Real);         // product of GM and Real
00401    void Multiply(Real);                         // multiply this by Real
00402    void Negate(GeneralMatrix*);                 // change sign
00403    void Negate();                               // change sign
00404    void ReverseElements();                      // internal reverse of elements
00405    void ReverseElements(GeneralMatrix*);        // reverse order of elements
00406    void operator=(Real);                        // set matrix to constant
00407    Real* GetStore();                            // get store or copy
00408    GeneralMatrix* BorrowStore(GeneralMatrix*, MatrixType);
00409                                                 // temporarily access store
00410    void GetMatrix(const GeneralMatrix*);        // used by = and initialise
00411    void Eq(const BaseMatrix&, MatrixType);      // used by =
00412    void Eq(const GeneralMatrix&);               // version with no conversion
00413    void Eq(const BaseMatrix&, MatrixType, bool);// used by <<
00414    void Eq2(const BaseMatrix&, MatrixType);     // cut down version of Eq
00415    int search(const BaseMatrix*) const;
00416    virtual GeneralMatrix* Transpose(TransposedMatrix*, MatrixType);
00417    void CheckConversion(const BaseMatrix&);     // check conversion OK
00418    void ReSize(int, int, int);                  // change dimensions
00419    virtual short SimpleAddOK(const GeneralMatrix* /*gm*/) { return 0; }
00420              // see bandmat.cpp for explanation
00421    virtual void MiniCleanUp()
00422       { store = 0; storage = 0; nrows_value = 0; ncols_value = 0; tag = -1;}
00423              // CleanUp when the data array has already been deleted
00424    void PlusEqual(const GeneralMatrix& gm);
00425    void MinusEqual(const GeneralMatrix& gm);
00426    void PlusEqual(Real f);
00427    void MinusEqual(Real f);
00428    void swap(GeneralMatrix& gm);                // swap values
00429 public:
00430    GeneralMatrix* Evaluate(MatrixType mt=MatrixTypeUnSp);
00431    virtual MatrixType Type() const = 0;         // type of a matrix
00432    int Nrows() const { return nrows_value; }    // get dimensions
00433    int Ncols() const { return ncols_value; }
00434    int Storage() const { return storage; }
00435    Real* Store() const { return store; }
00436    // updated names
00437    int nrows() const { return nrows_value; }    // get dimensions
00438    int ncols() const { return ncols_value; }
00439    int size() const { return storage; }
00440    Real* data() { return store; }
00441    const Real* data() const { return store; }
00442    const Real* const_data() const { return store; }
00443    virtual ~GeneralMatrix();                    // delete store if set
00444    void tDelete();                              // delete if tag permits
00445    bool reuse();                                // true if tag allows reuse
00446    void Protect() { tag=-1; }                   // cannot delete or reuse
00447    int Tag() const { return tag; }
00448    bool IsZero() const;                         // test matrix has all zeros
00449    void Release() { tag=1; }                    // del store after next use
00450    void Release(int t) { tag=t; }               // del store after t accesses
00451    void ReleaseAndDelete() { tag=0; }           // delete matrix after use
00452    void operator<<(const Real*);                // assignment from an array
00453    void operator<<(const int*);                // assignment from an array
00454    void operator<<(const BaseMatrix& X)
00455       { Eq(X,this->Type(),true); }              // = without checking type
00456    void Inject(const GeneralMatrix&);           // copy stored els only
00457    void operator+=(const BaseMatrix&);
00458    void operator-=(const BaseMatrix&);
00459    void operator*=(const BaseMatrix&);
00460    void operator|=(const BaseMatrix&);
00461    void operator&=(const BaseMatrix&);
00462    void operator+=(Real);
00463    void operator-=(Real r) { operator+=(-r); }
00464    void operator*=(Real);
00465    void operator/=(Real r) { operator*=(1.0/r); }
00466    virtual GeneralMatrix* MakeSolver();         // for solving
00467    virtual void Solver(MatrixColX&, const MatrixColX&) {}
00468    virtual void GetRow(MatrixRowCol&) = 0;      // Get matrix row
00469    virtual void RestoreRow(MatrixRowCol&) {}    // Restore matrix row
00470    virtual void NextRow(MatrixRowCol&);         // Go to next row
00471    virtual void GetCol(MatrixRowCol&) = 0;      // Get matrix col
00472    virtual void GetCol(MatrixColX&) = 0;        // Get matrix col
00473    virtual void RestoreCol(MatrixRowCol&) {}    // Restore matrix col
00474    virtual void RestoreCol(MatrixColX&) {}      // Restore matrix col
00475    virtual void NextCol(MatrixRowCol&);         // Go to next col
00476    virtual void NextCol(MatrixColX&);           // Go to next col
00477    Real SumSquare() const;
00478    Real SumAbsoluteValue() const;
00479    Real Sum() const;
00480    Real MaximumAbsoluteValue1(int& i) const;
00481    Real MinimumAbsoluteValue1(int& i) const;
00482    Real Maximum1(int& i) const;
00483    Real Minimum1(int& i) const;
00484    Real MaximumAbsoluteValue() const;
00485    Real MaximumAbsoluteValue2(int& i, int& j) const;
00486    Real MinimumAbsoluteValue() const;
00487    Real MinimumAbsoluteValue2(int& i, int& j) const;
00488    Real Maximum() const;
00489    Real Maximum2(int& i, int& j) const;
00490    Real Minimum() const;
00491    Real Minimum2(int& i, int& j) const;
00492    LogAndSign LogDeterminant() const;
00493    virtual bool IsEqual(const GeneralMatrix&) const;
00494                                                 // same type, same values
00495    void CheckStore() const;                     // check store is non-zero
00496    virtual void SetParameters(const GeneralMatrix*) {}
00497                                                 // set parameters in GetMatrix
00498    operator ReturnMatrix() const;