newmat.h

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 sum_square_columns() const;
00339    virtual ReturnMatrix sum_square_rows() const;
00340 //   virtual ReturnMatrix Reverse() const;       // reverse order of elements
00341 //protected:
00342 //   BaseMatrix() : t(this), i(this) {}
00343 
00344    friend class GeneralMatrix;
00345    friend class Matrix;
00346    friend class SquareMatrix;
00347    friend class nricMatrix;
00348    friend class RowVector;
00349    friend class ColumnVector;
00350    friend class SymmetricMatrix;
00351    friend class UpperTriangularMatrix;
00352    friend class LowerTriangularMatrix;
00353    friend class DiagonalMatrix;
00354    friend class CroutMatrix;
00355    friend class BandMatrix;
00356    friend class LowerBandMatrix;
00357    friend class UpperBandMatrix;
00358    friend class SymmetricBandMatrix;
00359    friend class AddedMatrix;
00360    friend class MultipliedMatrix;
00361    friend class SubtractedMatrix;
00362    friend class SPMatrix;
00363    friend class KPMatrix;
00364    friend class ConcatenatedMatrix;
00365    friend class StackedMatrix;
00366    friend class SolvedMatrix;
00367    friend class ShiftedMatrix;
00368    friend class NegShiftedMatrix;
00369    friend class ScaledMatrix;
00370    friend class TransposedMatrix;
00371    friend class ReversedMatrix;
00372    friend class NegatedMatrix;
00373    friend class InvertedMatrix;
00374    friend class RowedMatrix;
00375    friend class ColedMatrix;
00376    friend class DiagedMatrix;
00377    friend class MatedMatrix;
00378    friend class GetSubMatrix;
00379    friend class ReturnMatrix;
00380    friend class LinearEquationSolver;
00381    friend class GenericMatrix;
00382    NEW_DELETE(BaseMatrix)
00383 };
00384 
00385 
00386 // ***************************** working classes **************************/
00387 
00388 class GeneralMatrix : public BaseMatrix         // declarable matrix types
00389 {
00390    virtual GeneralMatrix* Image() const;        // copy of matrix
00391 protected:
00392    int tag;                                     // shows whether can reuse
00393    int nrows_value, ncols_value;                // dimensions
00394    int storage;                                 // total store required
00395    Real* store;                                 // point to store (0=not set)
00396    GeneralMatrix();                             // initialise with no store
00397    GeneralMatrix(ArrayLengthSpecifier);         // constructor getting store
00398    void Add(GeneralMatrix*, Real);              // sum of GM and Real
00399    void Add(Real);                              // add Real to this
00400    void NegAdd(GeneralMatrix*, Real);           // Real - GM
00401    void NegAdd(Real);                           // this = this - Real
00402    void Multiply(GeneralMatrix*, Real);         // product of GM and Real
00403    void Multiply(Real);                         // multiply this by Real
00404    void Negate(GeneralMatrix*);                 // change sign
00405    void Negate();                               // change sign
00406    void ReverseElements();                      // internal reverse of elements
00407    void ReverseElements(GeneralMatrix*);        // reverse order of elements
00408    void operator=(Real);                        // set matrix to constant
00409    Real* GetStore();                            // get store or copy
00410    GeneralMatrix* BorrowStore(GeneralMatrix*, MatrixType);
00411                                                 // temporarily access store
00412    void GetMatrix(const GeneralMatrix*);        // used by = and initialise
00413    void Eq(const BaseMatrix&, MatrixType);      // used by =
00414    void Eq(const GeneralMatrix&);               // version with no conversion
00415    void Eq(const BaseMatrix&, MatrixType, bool);// used by <<
00416    void Eq2(const BaseMatrix&, MatrixType);     // cut down version of Eq
00417    int search(const BaseMatrix*) const;
00418    virtual GeneralMatrix* Transpose(TransposedMatrix*, MatrixType);
00419    void CheckConversion(const BaseMatrix&);     // check conversion OK
00420    void ReSize(int, int, int);                  // change dimensions
00421    virtual short SimpleAddOK(const GeneralMatrix* gm) { return 0; }
00422              // see bandmat.cpp for explanation
00423    virtual void MiniCleanUp()
00424       { store = 0; storage = 0; nrows_value = 0; ncols_value = 0; tag = -1;}
00425              // CleanUp when the data array has already been deleted
00426    void PlusEqual(const GeneralMatrix& gm);
00427    void MinusEqual(const GeneralMatrix& gm);
00428    void PlusEqual(Real f);
00429    void MinusEqual(Real f);
00430    void swap(GeneralMatrix& gm);                // swap values
00431 public:
00432    GeneralMatrix* Evaluate(MatrixType mt=MatrixTypeUnSp);
00433    virtual MatrixType Type() const = 0;         // type of a matrix
00434    int Nrows() const { return nrows_value; }    // get dimensions
00435    int Ncols() const { return ncols_value; }
00436    int Storage() const { return storage; }
00437    Real* Store() const { return store; }
00438    // updated names
00439    int nrows() const { return nrows_value; }    // get dimensions
00440    int ncols() const { return ncols_value; }
00441    int size() const { return storage; }
00442    Real* data() { return store; }
00443    const Real* data() const { return store; }
00444    const Real* const_data() const { return store; }
00445    virtual ~GeneralMatrix();                    // delete store if set
00446    void tDelete();                              // delete if tag permits
00447    bool reuse();                                // true if tag allows reuse
00448    void Protect() { tag=-1; }                   // cannot delete or reuse
00449    int Tag() const { return tag; }
00450    bool IsZero() const;                         // test matrix has all zeros
00451    void Release() { tag=1; }                    // del store after next use
00452    void Release(int t) { tag=t; }               // del store after t accesses
00453    void ReleaseAndDelete() { tag=0; }           // delete matrix after use
00454    void operator<<(const double*);              // assignment from an array
00455    void operator<<(const float*);               // assignment from an array
00456    void operator<<(const int*);                 // assignment from an array
00457    void operator<<(const BaseMatrix& X)
00458       { Eq(X,this->Type(),true); }              // = without checking type
00459    void Inject(const GeneralMatrix&);           // copy stored els only
00460    void operator+=(const BaseMatrix&);
00461    void operator-=(const BaseMatrix&);
00462    void operator*=(const BaseMatrix&);
00463    void operator|=(const BaseMatrix&);
00464    void operator&=(const BaseMatrix&);
00465    void operator+=(Real);
00466    void operator-=(Real r) { operator+=(-r); }
00467    void operator*=(Real);
00468    void operator/=(Real r) { operator*=(1.0/r); }
00469    virtual GeneralMatrix* MakeSolver();         // for solving
00470    virtual void Solver(MatrixColX&, const MatrixColX&) {}
00471    virtual void GetRow(MatrixRowCol&) = 0;      // Get matrix row
00472    virtual void RestoreRow(MatrixRowCol&) {}    // Restore matrix row
00473    virtual void NextRow(MatrixRowCol&);         // Go to next row
00474    virtual void GetCol(MatrixRowCol&) = 0;      // Get matrix col
00475    virtual void GetCol(MatrixColX&) = 0;        // Get matrix col
00476    virtual void RestoreCol(MatrixRowCol&) {}    // Restore matrix col
00477    virtual void RestoreCol(MatrixColX&) {}      // Restore matrix col
00478    virtual void NextCol(MatrixRowCol&);         // Go to next col
00479    virtual void NextCol(MatrixColX&);           // Go to next col
00480    Real SumSquare() const;
00481    Real SumAbsoluteValue() const;
00482    Real Sum() const;
00483    Real MaximumAbsoluteValue1(int& i) const;
00484    Real MinimumAbsoluteValue1(int& i) const;
00485    Real Maximum1(int& i) const;
00486    Real Minimum1(int& i) const;
00487    Real MaximumAbsoluteValue() const;
00488    Real MaximumAbsoluteValue2(int& i, int& j) const;
00489    Real MinimumAbsoluteValue() const;
00490    Real MinimumAbsoluteValue2(int& i, int& j) const;
00491    Real Maximum() const;
00492    Real Maximum2(int& i, int& j) const;
00493    Real Minimum() const;
00494    Real Minimum2(int& i, int& j) const;
00495    LogAndSign LogDeterminant() const;
00496    virtual bool IsEqual(const GeneralMatrix&) const;
00497                                                 // same type, same values
00498    void CheckStore() const;                     // check store is non-zero
00499    virtual void SetParameters(const GeneralMatrix*) {}
00500                                                 // set parameters in GetMatrix
00501    operator ReturnMatrix() const;               // for building a ReturnMatrix
00502    ReturnMatrix ForReturn() const;
00503    //virtual bool SameStorageType(const GeneralMatrix& A) const;
00504    //virtual void ReSizeForAdd(const GeneralMatrix& A, const GeneralMatrix& B);
00505    //virtual void ReSizeForSP(const GeneralMatrix& A, const GeneralMatrix& B);
00506    virtual void ReSize(const GeneralMatrix& A);
00507    MatrixInput operator<<(double);                // for loading a list
00508    MatrixInput operator<<(float);                // for loading a list
00509    MatrixInput operator<<(int f);
00510 //   ReturnMatrix Reverse() const;                // reverse order of elements
00511    void CleanUp();                              // to clear store
00512 
00513    friend class Matrix;
00514    friend class SquareMatrix;
00515    friend class nricMatrix;
00516    friend class SymmetricMatrix;
00517    friend class UpperTriangularMatrix;
00518    friend class LowerTriangularMatrix;
00519    friend class DiagonalMatrix;
00520    friend class CroutMatrix;
00521    friend class RowVector;
00522    friend class ColumnVector;
00523    friend class BandMatrix;
00524    friend class LowerBandMatrix;
00525    friend class UpperBandMatrix;
00526    friend class SymmetricBandMatrix;
00527    friend class BaseMatrix;
00528    friend class AddedMatrix;
00529    friend class MultipliedMatrix;
00530    friend class SubtractedMatrix;
00531    friend class SPMatrix;
00532    friend class KPMatrix;
00533    friend class ConcatenatedMatrix;
00534    friend class StackedMatrix;
00535    friend class SolvedMatrix;
00536    friend class ShiftedMatrix;
00537    friend class NegShiftedMatrix;
00538    friend class ScaledMatrix;
00539    friend class TransposedMatrix;
00540    friend class ReversedMatrix;
00541    friend class NegatedMatrix;
00542    friend class InvertedMatrix;
00543    friend class RowedMatrix;
00544    friend class ColedMatrix;
00545    friend class DiagedMatrix;
00546    friend class MatedMatrix;
00547    friend class GetSubMatrix;
00548    friend class ReturnMatrix;
00549    friend class LinearEquationSolver;
00550    friend class GenericMatrix;
00551    NEW_DELETE(GeneralMatrix)
00552 };
00553 
00554 
00555 
00556 class Matrix : public GeneralMatrix             // usual rectangular matrix
00557 {
00558    GeneralMatrix* Image() const;                // copy of matrix
00559 public:
00560    Matrix() {}
00561    ~Matrix() {}
00562    Matrix(int, int);                            // standard declaration
00563    Matrix(const BaseMatrix&);                   // evaluate BaseMatrix
00564    void operator=(const BaseMatrix&);
00565    void operator=(Real f) { GeneralMatrix::operator=(f); }
00566    void operator=(const Matrix& m) { Eq(m); }
00567    MatrixType Type() const;
00568    Real& operator()(int, int);                  // access element
00569    Real& element(int, int);                     // access element
00570    Real operator()(int, int) const;            // access element
00571    Real element(int, int) const;               // access element
00572 #ifdef SETUP_C_SUBSCRIPTS
00573    Real* operator[](int m) { return store+m*ncols_value; }
00574    const Real* operator[](int m) const { return store+m*ncols_value; }
00575    // following for Numerical Recipes in C++
00576    Matrix(Real, int, int);
00577    Matrix(const Real*, int, int);
00578 #endif
00579    Matrix(const Matrix& gm) { GetMatrix(&gm); }
00580    GeneralMatrix* MakeSolver();
00581    Real Trace() const;
00582    void GetRow(MatrixRowCol&);
00583    void GetCol(MatrixRowCol&);
00584    void GetCol(MatrixColX&);
00585    void RestoreCol(MatrixRowCol&);
00586    void RestoreCol(MatrixColX&);
00587    void NextRow(MatrixRowCol&);
00588    void NextCol(MatrixRowCol&);
00589    void NextCol(MatrixColX&);
00590    virtual void ReSize(int,int);           // change dimensions
00591       // virtual so we will catch it being used in a vector called as a matrix
00592    void ReSize(const GeneralMatrix& A);
00593    Real MaximumAbsoluteValue2(int& i, int& j) const;
00594    Real MinimumAbsoluteValue2(int& i, int& j) const;
00595    Real Maximum2(int& i, int& j) const;
00596    Real Minimum2(int& i, int& j) const;
00597    void operator+=(const Matrix& M) { PlusEqual(M); }
00598    void operator-=(const Matrix& M) { MinusEqual(M); }
00599    void operator+=(Real f) { GeneralMatrix::Add(f); }
00600    void operator-=(Real f) { GeneralMatrix::Add(-f); }
00601    void swap(Matrix& gm) { GeneralMatrix::swap((GeneralMatrix&)gm); }
00602    friend Real DotProduct(const Matrix& A, const Matrix& B);
00603    NEW_DELETE(Matrix)
00604 };
00605 
00606 class SquareMatrix : public Matrix              // square matrix
00607 {
00608    GeneralMatrix* Image() const;                // copy of matrix
00609 public:
00610    SquareMatrix() {}
00611    ~SquareMatrix() {}
00612    SquareMatrix(ArrayLengthSpecifier);          // standard declaration
00613    SquareMatrix(const BaseMatrix&);             // evaluate BaseMatrix
00614    void operator=(const BaseMatrix&);
00615    void operator=(Real f) { GeneralMatrix::operator=(f); }
00616    void operator=(const SquareMatrix& m) { Eq(m); }
00617    void operator=(const Matrix& m);
00618    MatrixType Type() const;
00619    SquareMatrix(const SquareMatrix& gm) { GetMatrix(&gm); }
00620    SquareMatrix(const Matrix& gm);
00621    void ReSize(int);                            // change dimensions
00622    virtual void ReSize(int,int);                // change dimensions
00623       // virtual so we will catch it being used in a vector called as a matrix
00624    void ReSize(const GeneralMatrix& A);
00625    void operator+=(const Matrix& M) { PlusEqual(M); }
00626    void operator-=(const Matrix& M) { MinusEqual(M); }
00627    void operator+=(Real f) { GeneralMatrix::Add(f); }
00628    void operator-=(Real f) { GeneralMatrix::Add(-f); }
00629    void swap(SquareMatrix& gm) { GeneralMatrix::swap((GeneralMatrix&)gm); }
00630    NEW_DELETE(SquareMatrix)
00631 };
00632 
00633 class nricMatrix : public Matrix                // for use with Numerical
00634                                                 // Recipes in C
00635 {
00636    GeneralMatrix* Image() const;                // copy of matrix
00637    Real** row_pointer;                          // points to rows
00638    void MakeRowPointer();                       // build rowpointer
00639    void DeleteRowPointer();
00640 public:
00641    nricMatrix() {}
00642    nricMatrix(int m, int n)                     // standard declaration
00643       :  Matrix(m,n) { MakeRowPointer(); }
00644    nricMatrix(const BaseMatrix& bm)             // evaluate BaseMatrix
00645       :  Matrix(bm) { MakeRowPointer(); }
00646    void operator=(const BaseMatrix& bm)
00647       { DeleteRowPointer(); Matrix::operator=(bm); MakeRowPointer(); }
00648    void operator=(Real f) { GeneralMatrix::operator=(f); }
00649    void operator=(const nricMatrix& m)
00650       { DeleteRowPointer(); Eq(m); MakeRowPointer(); }
00651    void operator<<(const BaseMatrix& X)
00652       { DeleteRowPointer(); Eq(X,this->Type(),true); MakeRowPointer(); }
00653    nricMatrix(const nricMatrix& gm) { GetMatrix(&gm); MakeRowPointer(); }
00654    void ReSize(int m, int n)               // change dimensions
00655       { DeleteRowPointer(); Matrix::ReSize(m,n); MakeRowPointer(); }
00656    void ReSize(const GeneralMatrix& A);
00657    ~nricMatrix() { DeleteRowPointer(); }
00658    Real** nric() const { CheckStore(); return row_pointer-1; }
00659    void CleanUp();                                // to clear store
00660    void MiniCleanUp();
00661    void operator+=(const Matrix& M) { PlusEqual(M); }
00662    void operator-=(const Matrix& M) { MinusEqual(M); }
00663    void operator+=(Real f) { GeneralMatrix::Add(f); }
00664    void operator-=(Real f) { GeneralMatrix::Add(-f); }
00665    void swap(nricMatrix& gm);
00666    NEW_DELETE(nricMatrix)
00667 };
00668 
00669 class SymmetricMatrix : public GeneralMatrix
00670 {
00671    GeneralMatrix* Image() const;                // copy of matrix
00672 public:
00673    SymmetricMatrix() {}
00674    ~SymmetricMatrix() {}
00675    SymmetricMatrix(ArrayLengthSpecifier);
00676    SymmetricMatrix(const BaseMatrix&);
00677    void operator=(const BaseMatrix&);
00678    void operator=(Real f) { GeneralMatrix::operator=(f); }
00679    void operator=(const SymmetricMatrix& m) { Eq(m); }
00680    Real& operator()(int, int);                  // access element
00681    Real& element(int, int);                     // access element
00682    Real operator()(int, int) const;             // access element
00683    Real element(int, int) const;                // access element
00684 #ifdef SETUP_C_SUBSCRIPTS
00685    Real* operator[](int m) { return store+(m*(m+1))/2; }
00686    const Real* operator[](int m) const { return store+(m*(m+1))/2; }
00687 #endif
00688    MatrixType Type() const;
00689    SymmetricMatrix(const SymmetricMatrix& gm) { GetMatrix(&gm); }
00690    Real SumSquare() const;
00691    Real SumAbsoluteValue() const;
00692    Real Sum() const;
00693    Real Trace() const;
00694    void GetRow(MatrixRowCol&);
00695    void GetCol(MatrixRowCol&);
00696    void GetCol(MatrixColX&);
00697    void RestoreCol(MatrixRowCol&) {}
00698    void RestoreCol(MatrixColX&);
00699         GeneralMatrix* Transpose(TransposedMatrix*, MatrixType);
00700    void ReSize(int);                       // change dimensions
00701    void ReSize(const GeneralMatrix& A);
00702    void operator+=(const SymmetricMatrix& M) { PlusEqual(M); }
00703    void operator-=(const SymmetricMatrix& M) { MinusEqual(M); }
00704    void operator+=(Real f) { GeneralMatrix::Add(f); }
00705    void operator-=(Real f) { GeneralMatrix::Add(-f); }
00706    void swap(SymmetricMatrix& gm) { GeneralMatrix::swap((GeneralMatrix&)gm); }
00707    NEW_DELETE(SymmetricMatrix)
00708 };
00709 
00710 class UpperTriangularMatrix : public GeneralMatrix
00711 {
00712    GeneralMatrix* Image() const;                // copy of matrix
00713 public:
00714    UpperTriangularMatrix() {}
00715    ~UpperTriangularMatrix() {}
00716    UpperTriangularMatrix(ArrayLengthSpecifier);
00717    void operator=(const BaseMatrix&);
00718    void operator=(const UpperTriangularMatrix& m) { Eq(m); }
00719    UpperTriangularMatrix(const BaseMatrix&);
00720    UpperTriangularMatrix(const UpperTriangularMatrix& gm) { GetMatrix(&gm); }
00721    void operator=(Real f) { GeneralMatrix::operator=(f); }
00722    Real& operator()(int, int);                  // access element
00723    Real& element(int, int);                     // access element
00724    Real operator()(int, int) const;             // access element
00725    Real element(int, int) const;                // access element
00726 #ifdef SETUP_C_SUBSCRIPTS
00727    Real* operator[](int m) { return store+m*ncols_value-(m*(m+1))/2; }
00728    const Real* operator[](int m) const
00729       { return store+m*ncols_value-(m*(m+1))/2; }
00730 #endif
00731    MatrixType Type() const;
00732    GeneralMatrix* MakeSolver() { return this; } // for solving
00733    void Solver(MatrixColX&, const MatrixColX&);
00734    LogAndSign LogDeterminant() const;
00735    Real Trace() const;
00736    void GetRow(MatrixRowCol&);
00737    void GetCol(MatrixRowCol&);
00738    void GetCol(MatrixColX&);
00739    void RestoreCol(MatrixRowCol&);
00740    void RestoreCol(MatrixColX& c) { RestoreCol((MatrixRowCol&)c); }
00741    void NextRow(MatrixRowCol&);
00742    void ReSize(int);                       // change dimensions
00743    void ReSize(const GeneralMatrix& A);
00744    MatrixBandWidth BandWidth() const;
00745    void operator+=(const UpperTriangularMatrix& M) { PlusEqual(M); }
00746    void operator-=(const UpperTriangularMatrix& M) { MinusEqual(M); }
00747    void operator+=(Real f) { GeneralMatrix::operator+=(f); }
00748    void operator-=(Real f) { GeneralMatrix::operator-=(f); }
00749    void swap(UpperTriangularMatrix& gm)
00750       { GeneralMatrix::swap((GeneralMatrix&)gm); }
00751    NEW_DELETE(UpperTriangularMatrix)
00752 };
00753 
00754 class LowerTriangularMatrix : public GeneralMatrix
00755 {
00756    GeneralMatrix* Image() const;                // copy of matrix
00757 public:
00758    LowerTriangularMatrix() {}
00759    ~LowerTriangularMatrix() {}
00760    LowerTriangularMatrix(ArrayLengthSpecifier);
00761    LowerTriangularMatrix(const LowerTriangularMatrix& gm) { GetMatrix(&gm); }
00762    LowerTriangularMatrix(const BaseMatrix& M);
00763    void operator=(const BaseMatrix&);
00764    void operator=(Real f) { GeneralMatrix::operator=(f); }
00765    void operator=(const LowerTriangularMatrix& m) { Eq(m); }
00766    Real& operator()(int, int);                  // access element
00767    Real& element(int, int);                     // access element
00768    Real operator()(int, int) const;             // access element
00769    Real element(int, int) const;                // access element
00770 #ifdef SETUP_C_SUBSCRIPTS
00771    Real* operator[](int m) { return store+(m*(m+1))/2; }
00772    const Real* operator[](int m) const { return store+(m*(m+1))/2; }
00773 #endif
00774    MatrixType Type() const;
00775    GeneralMatrix* MakeSolver() { return this; } // for solving
00776    void Solver(MatrixColX&, const MatrixColX&);
00777    LogAndSign LogDeterminant() const;
00778    Real Trace() const;
00779    void GetRow(MatrixRowCol&);
00780    void GetCol(MatrixRowCol&);
00781    void GetCol(MatrixColX&);
00782    void RestoreCol(MatrixRowCol&);
00783    void RestoreCol(MatrixColX& c) { RestoreCol((MatrixRowCol&)c); }
00784    void NextRow(MatrixRowCol&);
00785    void ReSize(int);                       // change dimensions
00786    void ReSize(const GeneralMatrix& A);
00787    MatrixBandWidth BandWidth() const;
00788    void operator+=(const LowerTriangularMatrix& M) { PlusEqual(M); }
00789    void operator-=(const LowerTriangularMatrix& M) { MinusEqual(M); }
00790    void operator+=(Real f) { GeneralMatrix::operator+=(f); }
00791    void operator-=(Real f) { GeneralMatrix::operator-=(f); }
00792    void swap(LowerTriangularMatrix& gm)
00793       { GeneralMatrix::swap((GeneralMatrix&)gm); }
00794    NEW_DELETE(LowerTriangularMatrix)
00795 };
00796 
00797 class DiagonalMatrix : public GeneralMatrix
00798 {
00799    GeneralMatrix* Image() const;                // copy of matrix
00800 public:
00801    DiagonalMatrix() {}
00802    ~DiagonalMatrix() {}
00803    DiagonalMatrix(ArrayLengthSpecifier);
00804    DiagonalMatrix(const BaseMatrix&);
00805    DiagonalMatrix(const DiagonalMatrix& gm) { GetMatrix(&gm); }
00806    void operator=(const BaseMatrix&);
00807    void operator=(Real f) { GeneralMatrix::operator=(f); }
00808    void operator=(const DiagonalMatrix& m) { Eq(m); }
00809    Real& operator()(int, int);                  // access element
00810    Real& operator()(int);                       // access element
00811    Real operator()(int, int) const;             // access element
00812    Real operator()(int) const;
00813    Real& element(int, int);                     // access element
00814    Real& element(int);                          // access element
00815    Real element(int, int) const;                // access element
00816    Real element(int) const;                     // access element
00817 #ifdef SETUP_C_SUBSCRIPTS
00818    Real& operator[](int m) { return store[m]; }
00819    const Real& operator[](int m) const { return store[m]; }
00820 #endif
00821    MatrixType Type() const;
00822 
00823    LogAndSign LogDeterminant() const;
00824    Real Trace() const;
00825    void GetRow(MatrixRowCol&);
00826    void GetCol(MatrixRowCol&);
00827    void GetCol(MatrixColX&);
00828    void NextRow(MatrixRowCol&);
00829    void NextCol(MatrixRowCol&);
00830    void NextCol(MatrixColX&);
00831    GeneralMatrix* MakeSolver() { return this; } // for solving
00832    void Solver(MatrixColX&, const MatrixColX&);
00833    GeneralMatrix* Transpose(TransposedMatrix*, MatrixType);
00834    void ReSize(int);                       // change dimensions
00835    void ReSize(const GeneralMatrix& A);
00836    Real* nric() const
00837       { CheckStore(); return store-1; }         // for use by NRIC
00838    MatrixBandWidth BandWidth() const;
00839 //   ReturnMatrix Reverse() const;                // reverse order of elements
00840    void operator+=(const DiagonalMatrix& M) { PlusEqual(M); }
00841    void operator-=(const DiagonalMatrix& M) { MinusEqual(M); }
00842    void operator+=(Real f) { GeneralMatrix::operator+=(f); }
00843    void operator-=(Real f) { GeneralMatrix::operator-=(f); }
00844    void swap(DiagonalMatrix& gm)
00845       { GeneralMatrix::swap((GeneralMatrix&)gm); }
00846    NEW_DELETE(DiagonalMatrix)
00847 };
00848 
00849 class RowVector : public Matrix
00850 {
00851    GeneralMatrix* Image() const;                // copy of matrix
00852 public:
00853    RowVector() { nrows_value = 1; }
00854    ~RowVector() {}
00855    RowVector(ArrayLengthSpecifier n) : Matrix(1,n.Value()) {}
00856    RowVector(const BaseMatrix&);
00857    RowVector(const RowVector& gm) { GetMatrix(&gm); }
00858    void operator=(const BaseMatrix&);
00859    void operator=(Real f) { GeneralMatrix::operator=(f); }
00860    void operator=(const RowVector& m) { Eq(m); }
00861    Real& operator()(int);                       // access element
00862    Real& element(int);                          // access element
00863    Real operator()(int) const;                  // access element
00864    Real element(int) const;                     // access element
00865 #ifdef SETUP_C_SUBSCRIPTS
00866    Real& operator[](int m) { return store[m]; }
00867    const Real& operator[](int m) const { return store[m]; }
00868    // following for Numerical Recipes in C++
00869    RowVector(Real a, int n) : Matrix(a, 1, n) {}
00870    RowVector(const Real* a, int n) : Matrix(a, 1, n) {}
00871 #endif
00872    MatrixType Type() const;
00873    void GetCol(MatrixRowCol&);
00874    void GetCol(MatrixColX&);
00875    void NextCol(MatrixRowCol&);
00876    void NextCol(MatrixColX&);
00877    void RestoreCol(MatrixRowCol&) {}
00878    void RestoreCol(MatrixColX& c);
00879    GeneralMatrix* Transpose(TransposedMatrix*, MatrixType);
00880    void ReSize(int);                       // change dimensions
00881    void ReSize(int,int);                   // in case access is matrix
00882    void ReSize(const GeneralMatrix& A);
00883    Real* nric() const
00884       { CheckStore(); return store-1; }         // for use by NRIC
00885    void CleanUp();                              // to clear store
00886    void MiniCleanUp()
00887       { store = 0; storage = 0; nrows_value = 1; ncols_value = 0; tag = -1; }
00888    // friend ReturnMatrix GetMatrixRow(Matrix& A, int row);
00889    void operator+=(const Matrix& M) { PlusEqual(M); }
00890    void operator-=(const Matrix& M) { MinusEqual(M); }
00891    void operator+=(Real f) { GeneralMatrix::Add(f); }
00892    void operator-=(Real f) { GeneralMatrix::Add(-f); }
00893    void swap(RowVector& gm)
00894       { GeneralMatrix::swap((GeneralMatrix&)gm); }
00895    NEW_DELETE(RowVector)
00896 };
00897 
00898 class ColumnVector : public Matrix
00899 {
00900    GeneralMatrix* Image() const;                // copy of matrix
00901 public:
00902    ColumnVector() { ncols_value = 1; }
00903    ~ColumnVector() {}
00904    ColumnVector(ArrayLengthSpecifier n) : Matrix(n.Value(),1) {}
00905    ColumnVector(const BaseMatrix&);
00906    ColumnVector(const ColumnVector& gm) { GetMatrix(&gm); }
00907    void operator=(const BaseMatrix&);
00908    void operator=(Real f) { GeneralMatrix::operator=(f); }
00909    void operator=(const ColumnVector& m) { Eq(m); }
00910    Real& operator()(int);                       // access element
00911    Real& element(int);                          // access element
00912    Real operator()(int) const;                  // access element
00913    Real element(int) const;                     // access element
00914 #ifdef SETUP_C_SUBSCRIPTS
00915    Real& operator[](int m) { return store[m]; }
00916    const Real& operator[](int m) const { return store[m]; }
00917    // following for Numerical Recipes in C++
00918    ColumnVector(Real a, int m) : Matrix(a, m, 1) {}
00919    ColumnVector(const Real* a, int m) : Matrix(a, m, 1) {}
00920 #endif
00921    MatrixType Type() const;
00922    GeneralMatrix* Transpose(TransposedMatrix*, MatrixType);
00923    void ReSize(int);                       // change dimensions
00924    void ReSize(int,int);                   // in case access is matrix
00925    void ReSize(const GeneralMatrix& A);
00926    Real* nric() const
00927       { CheckStore(); return store-1; }         // for use by NRIC
00928    void CleanUp();                              // to clear store
00929    void MiniCleanUp()
00930       { store = 0; storage = 0; nrows_value = 0; ncols_value = 1; tag = -1; }
00931 //   ReturnMatrix Reverse() const;                // reverse order of elements
00932    void operator+=(const Matrix& M) { PlusEqual(M); }
00933    void operator-=(const Matrix& M) { MinusEqual(M); }
00934    void operator+=(Real f) { GeneralMatrix::Add(f); }
00935    void operator-=(Real f) { GeneralMatrix::Add(-f); }
00936    void swap(ColumnVector& gm)
00937       { GeneralMatrix::swap((GeneralMatrix&)gm); }
00938    NEW_DELETE(ColumnVector)
00939 };
00940 
00941 class CroutMatrix : public GeneralMatrix        // for LU decomposition
00942 {
00943    int* indx;
00944    bool d;
00945    bool sing;
00946    void ludcmp();
00947    void operator=(const CroutMatrix& m) {}     // not allowed
00948 public:
00949    CroutMatrix(const BaseMatrix&);
00950    MatrixType Type() const;
00951    void lubksb(Real*, int=0);
00952    ~CroutMatrix();
00953    GeneralMatrix* MakeSolver() { return this; } // for solving
00954    LogAndSign LogDeterminant() const;
00955    void Solver(MatrixColX&, const MatrixColX&);
00956    void GetRow(MatrixRowCol&);
00957    void GetCol(MatrixRowCol&);
00958    void GetCol(MatrixColX& c) { GetCol((MatrixRowCol&)c); }
00959    void CleanUp();                                // to clear store
00960    void MiniCleanUp();
00961    bool IsEqual(const GeneralMatrix&) const;
00962    bool IsSingular() const { return sing; }
00963    void swap(CroutMatrix& gm);
00964    NEW_DELETE(CroutMatrix)
00965 };
00966 
00967 // ***************************** band matrices ***************************/
00968 
00969 class BandMatrix : public GeneralMatrix         // band matrix
00970 {
00971    GeneralMatrix* Image() const;                // copy of matrix
00972 protected:
00973    void CornerClear() const;                    // set unused elements to zero
00974    short SimpleAddOK(const GeneralMatrix* gm);
00975 public:
00976    int lower, upper;                            // band widths
00977    BandMatrix() { lower=0; upper=0; CornerClear(); }
00978    ~BandMatrix() {}
00979    BandMatrix(int n,int lb,int ub) { ReSize(n,lb,ub); CornerClear(); }
00980                                                 // standard declaration
00981    BandMatrix(const BaseMatrix&);               // evaluate BaseMatrix
00982    void operator=(const BaseMatrix&);
00983    void operator=(Real f) { GeneralMatrix::operator=(f); }
00984    void operator=(const BandMatrix& m) { Eq(m); }
00985    MatrixType Type() const;
00986    Real& operator()(int, int);                  // access element
00987    Real& element(int, int);                     // access element
00988    Real operator()(int, int) const;             // access element
00989    Real element(int, int) const;                // access element
00990 #ifdef SETUP_C_SUBSCRIPTS
00991    Real* operator[](int m) { return store+(upper+lower)*m+lower; }
00992    const Real* operator[](int m) const { return store+(upper+lower)*m+lower; }
00993 #endif
00994    BandMatrix(const BandMatrix& gm) { GetMatrix(&gm); }
00995    LogAndSign LogDeterminant() const;
00996    GeneralMatrix* MakeSolver();
00997    Real Trace() const;
00998    Real SumSquare() const { CornerClear(); return GeneralMatrix::SumSquare(); }
00999    Real SumAbsoluteValue() const
01000       { CornerClear(); return GeneralMatrix::SumAbsoluteValue(); }
01001    Real Sum() const
01002       { CornerClear(); return GeneralMatrix::Sum(); }
01003    Real MaximumAbsoluteValue() const
01004       { CornerClear(); return GeneralMatrix::MaximumAbsoluteValue(); }
01005    Real MinimumAbsoluteValue() const
01006       { int i, j; return GeneralMatrix::MinimumAbsoluteValue2(i, j); }
01007    Real Maximum() const { int i, j; return GeneralMatrix::Maximum2(i, j); }
01008    Real Minimum() const { int i, j; return GeneralMatrix::Minimum2(i, j); }
01009    void GetRow(MatrixRowCol&);
01010    void GetCol(MatrixRowCol&);
01011    void GetCol(MatrixColX&);
01012    void RestoreCol(MatrixRowCol&);
01013    void RestoreCol(MatrixColX& c) { RestoreCol((MatrixRowCol&)c); }
01014    void NextRow(MatrixRowCol&);
01015    virtual void ReSize(int, int, int);             // change dimensions
01016    void ReSize(const GeneralMatrix& A);
01017    //bool SameStorageType(const GeneralMatrix& A) const;
01018    //void ReSizeForAdd(const GeneralMatrix& A, const GeneralMatrix& B);
01019    //void ReSizeForSP(const GeneralMatrix& A, const GeneralMatrix& B);
01020    MatrixBandWidth BandWidth() const;
01021    void SetParameters(const GeneralMatrix*);
01022    MatrixInput operator<<(double);                // will give error
01023    MatrixInput operator<<(float);                // will give error
01024    MatrixInput operator<<(int f);
01025    void operator<<(const double* r);              // will give error
01026    void operator<<(const float* r);              // will give error
01027    void operator<<(const int* r);               // will give error
01028       // the next is included because Zortech and Borland
01029       // cannot find the copy in GeneralMatrix
01030    void operator<<(const BaseMatrix& X) { GeneralMatrix::operator<<(X); }
01031    void swap(BandMatrix& gm);
01032    NEW_DELETE(BandMatrix)
01033 };
01034 
01035 class UpperBandMatrix : public BandMatrix       // upper band matrix
01036 {
01037    GeneralMatrix* Image() const;                // copy of matrix
01038 public:
01039    UpperBandMatrix() {}
01040    ~UpperBandMatrix() {}
01041    UpperBandMatrix(int n, int ubw)              // standard declaration
01042       : BandMatrix(n, 0, ubw) {}
01043    UpperBandMatrix(const BaseMatrix&);          // evaluate BaseMatrix
01044    void operator=(const BaseMatrix&);
01045    void operator=(Real f) { GeneralMatrix::operator=(f); }
01046    void operator=(const UpperBandMatrix& m) { Eq(m); }
01047    MatrixType Type() const;
01048    UpperBandMatrix(const UpperBandMatrix& gm) { GetMatrix(&gm); }
01049    GeneralMatrix* MakeSolver() { return this; }
01050    void Solver(MatrixColX&, const MatrixColX&);
01051    LogAndSign LogDeterminant() const;
01052    void ReSize(int, int, int);             // change dimensions
01053    void ReSize(int n,int ubw)              // change dimensions
01054       { BandMatrix::ReSize(n,0,ubw); }
01055    void ReSize(const GeneralMatrix& A) { BandMatrix::ReSize(A); }
01056    Real& operator()(int, int);
01057    Real operator()(int, int) const;
01058    Real& element(int, int);
01059    Real element(int, int) const;
01060 #ifdef SETUP_C_SUBSCRIPTS
01061    Real* operator[](int m) { return store+upper*m; }
01062    const Real* operator[](int m) const { return store+upper*m; }
01063 #endif
01064    void swap(UpperBandMatrix& gm)
01065       { BandMatrix::swap((BandMatrix&)gm); }
01066    NEW_DELETE(UpperBandMatrix)
01067 };
01068 
01069 class LowerBandMatrix : public BandMatrix       // upper band matrix
01070 {
01071    GeneralMatrix* Image() const;                // copy of matrix
01072 public:
01073    LowerBandMatrix() {}
01074    ~LowerBandMatrix() {}
01075    LowerBandMatrix(int n, int lbw)              // standard declaration
01076       : BandMatrix(n, lbw, 0) {}
01077    LowerBandMatrix(const BaseMatrix&);          // evaluate BaseMatrix
01078    void operator=(const BaseMatrix&);
01079    void operator=(Real f) { GeneralMatrix::operator=(f); }
01080    void operator=(const LowerBandMatrix& m) { Eq(m); }
01081    MatrixType Type() const;
01082    LowerBandMatrix(const LowerBandMatrix& gm) { GetMatrix(&gm); }
01083    GeneralMatrix* MakeSolver() { return this; }
01084    void Solver(MatrixColX&, const MatrixColX&);
01085    LogAndSign LogDeterminant() const;
01086    void ReSize(int, int, int);             // change dimensions
01087    void ReSize(int n,int lbw)             // change dimensions
01088       { BandMatrix::ReSize(n,lbw,0); }
01089    void ReSize(const GeneralMatrix& A) { BandMatrix::ReSize(A); }
01090    Real& operator()(int, int);
01091    Real operator()(int, int) const;
01092    Real& element(int, int);
01093    Real element(int, int) const;
01094 #ifdef SETUP_C_SUBSCRIPTS
01095    Real* operator[](int m) { return store+lower*(m+1); }
01096    const Real* operator[](int m) const { return store+lower*(m+1); }
01097 #endif
01098    void swap(LowerBandMatrix& gm)
01099       { BandMatrix::swap((BandMatrix&)gm); }
01100    NEW_DELETE(LowerBandMatrix)
01101 };
01102 
01103 class SymmetricBandMatrix : public GeneralMatrix
01104 {
01105    GeneralMatrix* Image() const;                // copy of matrix
01106    void CornerClear() const;                    // set unused elements to zero
01107    short SimpleAddOK(const GeneralMatrix* gm);
01108 public:
01109    int lower;                                   // lower band width
01110    SymmetricBandMatrix() { lower=0; CornerClear(); }
01111    ~SymmetricBandMatrix() {}
01112    SymmetricBandMatrix(int n, int lb) { ReSize(n,lb); CornerClear(); }
01113    SymmetricBandMatrix(const BaseMatrix&);
01114    void operator=(const BaseMatrix&);
01115    void operator=(Real f) { GeneralMatrix::operator=(f); }
01116    void operator=(const SymmetricBandMatrix& m) { Eq(m); }
01117    Real& operator()(int, int);                  // access element
01118    Real& element(int, int);                     // access element
01119    Real operator()(int, int) const;             // access element
01120    Real element(int, int) const;                // access element
01121 #ifdef SETUP_C_SUBSCRIPTS
01122    Real* operator[](int m) { return store+lower*(m+1); }
01123    const Real* operator[](int m) const { return store+lower*(m+1); }
01124 #endif
01125    MatrixType Type() const;
01126    SymmetricBandMatrix(const SymmetricBandMatrix& gm) { GetMatrix(&gm); }
01127    GeneralMatrix* MakeSolver();
01128    Real SumSquare() const;
01129    Real SumAbsoluteValue() const;
01130    Real Sum() const;
01131    Real MaximumAbsoluteValue() const
01132       { CornerClear(); return GeneralMatrix::MaximumAbsoluteValue(); }
01133    Real MinimumAbsoluteValue() const
01134       { int i, j; return GeneralMatrix::MinimumAbsoluteValue2(i, j); }
01135    Real Maximum() const { int i, j; return GeneralMatrix::Maximum2(i, j); }
01136    Real Minimum() const { int i, j; return GeneralMatrix::Minimum2(i, j); }
01137    Real Trace() const;
01138    LogAndSign LogDeterminant() const;
01139    void GetRow(MatrixRowCol&);
01140    void GetCol(MatrixRowCol&);
01141    void GetCol(MatrixColX&);
01142    void RestoreCol(MatrixRowCol&) {}
01143    void RestoreCol(MatrixColX&);
01144    GeneralMatrix* Transpose(TransposedMatrix*, MatrixType);
01145    void ReSize(int,int);                       // change dimensions
01146    void ReSize(const GeneralMatrix& A);
01147    //bool SameStorageType(const GeneralMatrix& A) const;
01148    //void ReSizeForAdd(const GeneralMatrix& A, const GeneralMatrix& B);
01149    //void ReSizeForSP(const GeneralMatrix& A, const GeneralMatrix& B);
01150    MatrixBandWidth BandWidth() const;
01151    void SetParameters(const GeneralMatrix*);
01152    void operator<<(const double* r);              // will give error
01153    void operator<<(const float* r);              // will give error
01154    void operator<<(const int* r);               // will give error
01155    void operator<<(const BaseMatrix& X) { GeneralMatrix::operator<<(X); }
01156    void swap(SymmetricBandMatrix& gm);
01157    NEW_DELETE(SymmetricBandMatrix)
01158 };
01159 
01160 class BandLUMatrix : public GeneralMatrix
01161 // for LU decomposition of band matrix
01162 {
01163    int* indx;
01164    bool d;
01165    bool sing;                                   // true if singular
01166    Real* store2;
01167    int storage2;
01168    void ludcmp();
01169    int m1,m2;                                   // lower and upper
01170    void operator=(const BandLUMatrix& m) {}     // no allowed
01171 public:
01172    BandLUMatrix(const BaseMatrix&);
01173    MatrixType Type() const;
01174    void lubksb(Real*, int=0);
01175    ~BandLUMatrix();
01176    GeneralMatrix* MakeSolver() { return this; } // for solving
01177    LogAndSign LogDeterminant() const;
01178    void Solver(MatrixColX&, const MatrixColX&);
01179    void GetRow(MatrixRowCol&);
01180    void GetCol(MatrixRowCol&);
01181    void GetCol(MatrixColX& c) { GetCol((MatrixRowCol&)c); }
01182    void CleanUp();                                // to clear store
01183    void MiniCleanUp();
01184    bool IsEqual(const GeneralMatrix&) const;
01185    bool IsSingular() const { return sing; }
01186    void swap(BandLUMatrix& gm);
01187    NEW_DELETE(BandLUMatrix)
01188 };
01189 
01190 // ************************** special matrices ****************************
01191 
01192 class IdentityMatrix : public GeneralMatrix
01193 {
01194    GeneralMatrix* Image() const;          // copy of matrix
01195 public:
01196    IdentityMatrix() {}
01197    ~IdentityMatrix() {}
01198    IdentityMatrix(ArrayLengthSpecifier n) : GeneralMatrix(1)
01199       { nrows_value = ncols_value = n.Value(); *store = 1; }
01200    IdentityMatrix(const IdentityMatrix& gm) { GetMatrix(&gm); }
01201    IdentityMatrix(const BaseMatrix&);
01202    void operator=(const BaseMatrix&);
01203    void operator=(const IdentityMatrix& m) { Eq(m); }
01204    void operator=(Real f) { GeneralMatrix::operator=(f); }
01205    MatrixType Type() const;
01206 
01207    LogAndSign LogDeterminant() const;
01208    Real Trace() const;
01209    Real SumSquare() const;
01210    Real SumAbsoluteValue() const;
01211    Real Sum() const { return Trace(); }
01212    void GetRow(MatrixRowCol&);
01213    void GetCol(MatrixRowCol&);
01214    void GetCol(MatrixColX&);
01215    void NextRow(MatrixRowCol&);
01216    void NextCol(MatrixRowCol&);
01217    void NextCol(MatrixColX&);
01218    GeneralMatrix* MakeSolver() { return this; } // for solving
01219    void Solver(MatrixColX&, const MatrixColX&);
01220    GeneralMatrix* Transpose(TransposedMatrix*, MatrixType);
01221    void ReSize(int n);
01222    void ReSize(const GeneralMatrix& A);
01223    MatrixBandWidth BandWidth() const;
01224 //   ReturnMatrix Reverse() const;                // reverse order of elements
01225    void swap(IdentityMatrix& gm)
01226       { GeneralMatrix::swap((GeneralMatrix&)gm); }
01227    NEW_DELETE(IdentityMatrix)
01228 };
01229 
01230 
01231 
01232 
01233 // ************************** GenericMatrix class ************************/
01234 
01235 class GenericMatrix : public BaseMatrix
01236 {
01237    GeneralMatrix* gm;
01238    int search(const BaseMatrix* bm) const;
01239    friend class BaseMatrix;
01240 public:
01241    GenericMatrix() : gm(0) {}
01242    GenericMatrix(const BaseMatrix& bm)
01243       { gm = ((BaseMatrix&)bm).Evaluate(); gm = gm->Image(); }
01244    GenericMatrix(const GenericMatrix& bm)
01245       { gm = bm.gm->Image(); }
01246    void operator=(const GenericMatrix&);
01247    void operator=(const BaseMatrix&);
01248    void operator+=(const BaseMatrix&);
01249    void operator-=(const BaseMatrix&);
01250    void operator*=(const BaseMatrix&);
01251    void operator|=(const BaseMatrix&);
01252    void operator&=(const BaseMatrix&);
01253    void operator+=(Real);
01254    void operator-=(Real r) { operator+=(-r); }
01255    void operator*=(Real);
01256    void operator/=(Real r) { operator*=(1.0/r); }
01257    ~GenericMatrix() { delete gm; }
01258    void CleanUp() { delete gm; gm = 0; }
01259    void Release() { gm->Release(); }
01260    GeneralMatrix* Evaluate(MatrixType = MatrixTypeUnSp);
01261    MatrixBandWidth BandWidth() const;
01262    void swap(GenericMatrix& x);
01263    NEW_DELETE(GenericMatrix)
01264 };
01265 
01266 // *************************** temporary classes *************************/
01267 
01268 class MultipliedMatrix : public BaseMatrix
01269 {
01270 protected:
01271    // if these union statements cause problems, simply remove them
01272    // and declare the items individually
01273    union { const BaseMatrix* bm1; GeneralMatrix* gm1; };
01274                                                   // pointers to summands
01275    union { const BaseMatrix* bm2; GeneralMatrix* gm2; };
01276    MultipliedMatrix(const BaseMatrix* bm1x, const BaseMatrix* bm2x)
01277       : bm1(bm1x),bm2(bm2x) {}
01278    int search(const BaseMatrix*) const;
01279    friend class BaseMatrix;
01280    friend class GeneralMatrix;
01281    friend class GenericMatrix;
01282 public:
01283    ~MultipliedMatrix() {}
01284    GeneralMatrix* Evaluate(MatrixType mt=MatrixTypeUnSp);
01285    MatrixBandWidth BandWidth() const;
01286    NEW_DELETE(MultipliedMatrix)
01287 };
01288 
01289 class AddedMatrix : public MultipliedMatrix
01290 {
01291 protected:
01292    AddedMatrix(const BaseMatrix* bm1x, const BaseMatrix* bm2x)
01293       : MultipliedMatrix(bm1x,bm2x) {}
01294 
01295    friend class BaseMatrix;
01296    friend class GeneralMatrix;
01297    friend class GenericMatrix;
01298 public:
01299    ~AddedMatrix() {}
01300    GeneralMatrix* Evaluate(MatrixType mt=MatrixTypeUnSp);
01301    MatrixBandWidth BandWidth() const;
01302    NEW_DELETE(AddedMatrix)
01303 };
01304 
01305 class SPMatrix : public AddedMatrix
01306 {
01307 protected:
01308    SPMatrix(const BaseMatrix* bm1x, const BaseMatrix* bm2x)
01309       : AddedMatrix(bm1x,bm2x) {}
01310 
01311    friend class BaseMatrix;
01312    friend class GeneralMatrix;
01313    friend class GenericMatrix;
01314 public:
01315    ~SPMatrix() {}
01316    GeneralMatrix* Evaluate(MatrixType mt=MatrixTypeUnSp);
01317    MatrixBandWidth BandWidth() const;
01318 
01319    friend SPMatrix SP(const BaseMatrix&, const BaseMatrix&);
01320 
01321    NEW_DELETE(SPMatrix)
01322 };
01323 
01324 class KPMatrix : public MultipliedMatrix
01325 {
01326 protected:
01327    KPMatrix(const BaseMatrix* bm1x, const BaseMatrix* bm2x)
01328       : MultipliedMatrix(bm1x,bm2x) {}
01329 
01330    friend class BaseMatrix;
01331    friend class GeneralMatrix;
01332    friend class GenericMatrix;
01333 public:
01334    ~KPMatrix() {}
01335    MatrixBandWidth BandWidth() const;
01336    GeneralMatrix* Evaluate(MatrixType mt=MatrixTypeUnSp);
01337    friend KPMatrix KP(const BaseMatrix&, const BaseMatrix&);
01338    NEW_DELETE(KPMatrix)
01339 };
01340 
01341 class ConcatenatedMatrix : public MultipliedMatrix
01342 {
01343 protected:
01344    ConcatenatedMatrix(const BaseMatrix* bm1x, const BaseMatrix* bm2x)
01345       : MultipliedMatrix(bm1x,bm2x) {}
01346 
01347    friend class BaseMatrix;
01348    friend class GeneralMatrix;
01349    friend class GenericMatrix;
01350 public:
01351    ~ConcatenatedMatrix() {}
01352    MatrixBandWidth BandWidth() const;
01353    GeneralMatrix* Evaluate(MatrixType mt=MatrixTypeUnSp);
01354    NEW_DELETE(ConcatenatedMatrix)
01355 };
01356 
01357 class StackedMatrix : public ConcatenatedMatrix
01358 {
01359 protected:
01360    StackedMatrix(const BaseMatrix* bm1x, const BaseMatrix* bm2x)
01361       : ConcatenatedMatrix(bm1x,bm2x) {}
01362 
01363    friend class BaseMatrix;
01364    friend class GeneralMatrix;
01365    friend class GenericMatrix;
01366 public:
01367    ~StackedMatrix() {}
01368    GeneralMatrix* Evaluate(MatrixType mt=MatrixTypeUnSp);
01369    NEW_DELETE(StackedMatrix)
01370 };
01371 
01372 class SolvedMatrix : public MultipliedMatrix
01373 {
01374    SolvedMatrix(const BaseMatrix* bm1x, const BaseMatrix* bm2x)
01375       : MultipliedMatrix(bm1x,bm2x) {}
01376    friend class BaseMatrix;
01377    friend class InvertedMatrix;                        // for operator*
01378 public:
01379    ~SolvedMatrix() {}
01380    GeneralMatrix* Evaluate(MatrixType mt=MatrixTypeUnSp);
01381    MatrixBandWidth BandWidth() const;
01382    NEW_DELETE(SolvedMatrix)
01383 };
01384 
01385 class SubtractedMatrix : public AddedMatrix
01386 {
01387    SubtractedMatrix(const BaseMatrix* bm1x, const BaseMatrix* bm2x)
01388       : AddedMatrix(bm1x,bm2x) {}
01389    friend class BaseMatrix;
01390    friend class GeneralMatrix;
01391    friend class GenericMatrix;
01392 public:
01393    ~SubtractedMatrix() {}
01394    GeneralMatrix* Evaluate(MatrixType mt=MatrixTypeUnSp);
01395    NEW_DELETE(SubtractedMatrix)
01396 };
01397 
01398 class ShiftedMatrix : public BaseMatrix
01399 {
01400 protected:
01401    union { const BaseMatrix* bm; GeneralMatrix* gm; };
01402    Real f;
01403    ShiftedMatrix(const BaseMatrix* bmx, Real fx) : bm(bmx),f(fx) {}
01404    int search(const BaseMatrix*) const;
01405    friend class BaseMatrix;
01406    friend class GeneralMatrix;
01407    friend class GenericMatrix;
01408 public:
01409    ~ShiftedMatrix() {}
01410    GeneralMatrix* Evaluate(MatrixType mt=MatrixTypeUnSp);
01411    friend ShiftedMatrix operator+(Real f, const BaseMatrix& BM);
01412    NEW_DELETE(ShiftedMatrix)
01413 };
01414 
01415 class NegShiftedMatrix : public ShiftedMatrix
01416 {
01417 protected:
01418    NegShiftedMatrix(Real fx, const BaseMatrix* bmx) : ShiftedMatrix(bmx,fx) {}
01419    friend class BaseMatrix;
01420    friend class GeneralMatrix;
01421    friend class GenericMatrix;
01422 public:
01423    ~NegShiftedMatrix() {}
01424    GeneralMatrix* Evaluate(MatrixType mt=MatrixTypeUnSp);
01425    friend NegShiftedMatrix operator-(Real, const BaseMatrix&);
01426    NEW_DELETE(NegShiftedMatrix)
01427 };
01428 
01429 class ScaledMatrix : public ShiftedMatrix
01430 {
01431    ScaledMatrix(const BaseMatrix* bmx, Real fx) : ShiftedMatrix(bmx,fx) {}
01432    friend class BaseMatrix;
01433    friend class GeneralMatrix;
01434    friend class GenericMatrix;
01435 public:
01436    ~ScaledMatrix() {}
01437    GeneralMatrix* Evaluate(MatrixType mt=MatrixTypeUnSp);
01438    MatrixBandWidth BandWidth() const;
01439 //#ifndef TEMPS_DESTROYED_QUICKLY
01440    friend ScaledMatrix operator*(Real f, const BaseMatrix& BM);
01441 // { return ScaledMatrix(&BM, f); }
01442 //#endif
01443    NEW_DELETE(ScaledMatrix)
01444 };
01445 
01446 class NegatedMatrix : public BaseMatrix
01447 {
01448 protected:
01449    union { const BaseMatrix* bm; GeneralMatrix* gm; };
01450    NegatedMatrix(const BaseMatrix* bmx) : bm(bmx) {}
01451    int search(const BaseMatrix*) const;
01452 private:
01453    friend class BaseMatrix;
01454 public:
01455    ~NegatedMatrix() {}
01456    GeneralMatrix* Evaluate(MatrixType mt=MatrixTypeUnSp);
01457    MatrixBandWidth BandWidth() const;
01458    NEW_DELETE(NegatedMatrix)
01459 };
01460 
01461 class TransposedMatrix : public NegatedMatrix
01462 {
01463    TransposedMatrix(const BaseMatrix* bmx) : NegatedMatrix(bmx) {}
01464    friend class BaseMatrix;
01465 public:
01466    ~TransposedMatrix() {}
01467    GeneralMatrix* Evaluate(MatrixType mt=MatrixTypeUnSp);
01468    MatrixBandWidth BandWidth() const;
01469    NEW_DELETE(TransposedMatrix)
01470 };
01471 
01472 class ReversedMatrix : public NegatedMatrix
01473 {
01474    ReversedMatrix(const BaseMatrix* bmx) : NegatedMatrix(bmx) {}
01475    friend class BaseMatrix;
01476 public:
01477    ~ReversedMatrix() {}
01478    GeneralMatrix* Evaluate(MatrixType mt=MatrixTypeUnSp);
01479    NEW_DELETE(ReversedMatrix)
01480 };
01481 
01482 class InvertedMatrix : public NegatedMatrix
01483 {
01484    InvertedMatrix(const BaseMatrix* bmx) : NegatedMatrix(bmx) {}
01485 public:
01486    ~InvertedMatrix() {}
01487    SolvedMatrix operator*(const BaseMatrix&) const;       // inverse(A) * B
01488    ScaledMatrix operator*(Real t) const { return BaseMatrix::operator*(t); }
01489    friend class BaseMatrix;
01490    GeneralMatrix* Evaluate(MatrixType mt=MatrixTypeUnSp);
01491    MatrixBandWidth BandWidth() const;
01492    NEW_DELETE(InvertedMatrix)
01493 };
01494 
01495 class RowedMatrix : public NegatedMatrix
01496 {
01497    RowedMatrix(const BaseMatrix* bmx) : NegatedMatrix(bmx) {}
01498    friend class BaseMatrix;
01499 public:
01500    ~RowedMatrix() {}
01501    GeneralMatrix* Evaluate(MatrixType mt=MatrixTypeUnSp);
01502    MatrixBandWidth BandWidth() const;
01503    NEW_DELETE(RowedMatrix)
01504 };
01505 
01506 class ColedMatrix : public NegatedMatrix
01507 {
01508    ColedMatrix(const BaseMatrix* bmx) : NegatedMatrix(bmx) {}
01509    friend class BaseMatrix;
01510 public:
01511    ~ColedMatrix() {}
01512    GeneralMatrix* Evaluate(MatrixType mt=MatrixTypeUnSp);
01513    MatrixBandWidth BandWidth() const;
01514    NEW_DELETE(ColedMatrix)
01515 };
01516 
01517 class DiagedMatrix : public NegatedMatrix
01518 {
01519    DiagedMatrix(const BaseMatrix* bmx) : NegatedMatrix(bmx) {}
01520    friend class BaseMatrix;
01521 public:
01522    ~DiagedMatrix() {}
01523    GeneralMatrix* Evaluate(MatrixType mt=MatrixTypeUnSp);
01524    MatrixBandWidth BandWidth() const;
01525    NEW_DELETE(DiagedMatrix)
01526 };
01527 
01528 class MatedMatrix : public NegatedMatrix
01529 {
01530    int nr, nc;
01531    MatedMatrix(const BaseMatrix* bmx, int nrx, int ncx)
01532       : NegatedMatrix(bmx), nr(nrx), nc(ncx) {}
01533    friend class BaseMatrix;
01534 public:
01535    ~MatedMatrix() {}
01536    GeneralMatrix* Evaluate(MatrixType mt=MatrixTypeUnSp);
01537    MatrixBandWidth BandWidth() const;
01538    NEW_DELETE(MatedMatrix)
01539 };
01540 
01541 class ReturnMatrix : public BaseMatrix    // for matrix return
01542 {
01543    GeneralMatrix* gm;
01544    int search(const BaseMatrix*) const;
01545 public:
01546    ~ReturnMatrix() {}
01547    GeneralMatrix* Evaluate(MatrixType mt=MatrixTypeUnSp);
01548    friend class BaseMatrix;
01549    ReturnMatrix(const ReturnMatrix& tm) : gm(tm.gm) {}
01550    ReturnMatrix(const GeneralMatrix* gmx) : gm((GeneralMatrix*&)gmx) {}
01551 //   ReturnMatrix(GeneralMatrix&);
01552    MatrixBandWidth BandWidth() const;
01553    NEW_DELETE(ReturnMatrix)
01554 };
01555 
01556 
01557 // ************************** submatrices ******************************/
01558 
01559 class GetSubMatrix : public NegatedMatrix
01560 {
01561    int row_skip;
01562    int row_number;
01563    int col_skip;
01564    int col_number;
01565    bool IsSym;
01566 
01567    GetSubMatrix
01568       (const BaseMatrix* bmx, int rs, int rn, int cs, int cn, bool is)
01569       : NegatedMatrix(bmx),
01570       row_skip(rs), row_number(rn), col_skip(cs), col_number(cn), IsSym(is) {}
01571    void SetUpLHS();
01572    friend class BaseMatrix;
01573 public:
01574    GetSubMatrix(const GetSubMatrix& g)
01575       : NegatedMatrix(g.bm), row_skip(g.row_skip), row_number(g.row_number),
01576       col_skip(g.col_skip), col_number(g.col_number), IsSym(g.IsSym) {}
01577    ~GetSubMatrix() {}
01578    GeneralMatrix* Evaluate(MatrixType mt=MatrixTypeUnSp);
01579    void operator=(const BaseMatrix&);
01580    void operator+=(const BaseMatrix&);
01581    void operator-=(const BaseMatrix&);
01582    void operator=(const GetSubMatrix& m) { operator=((const BaseMatrix&)m); }
01583    void operator<<(const BaseMatrix&);
01584    void operator<<(const double*);                // copy from array
01585    void operator<<(const float*);                // copy from array
01586    void operator<<(const int*);                 // copy from array
01587    MatrixInput operator<<(double);                // for loading a list
01588    MatrixInput operator<<(float);                // for loading a list
01589    MatrixInput operator<<(int f);
01590    void operator=(Real);                        // copy from constant
01591    void operator+=(Real);                       // add constant
01592    void operator-=(Real r) { operator+=(-r); }  // subtract constant
01593    void operator*=(Real);                       // multiply by constant
01594    void operator/=(Real r) { operator*=(1.0/r); } // divide by constant
01595    void Inject(const GeneralMatrix&);           // copy stored els only
01596    MatrixBandWidth BandWidth() const;
01597    NEW_DELETE(GetSubMatrix)
01598 };
01599 
01600 // ******************** linear equation solving ****************************/
01601 
01602 class LinearEquationSolver : public BaseMatrix
01603 {
01604    GeneralMatrix* gm;
01605    int search(const BaseMatrix*) const { return 0; }
01606    friend class BaseMatrix;
01607 public:
01608    LinearEquationSolver(const BaseMatrix& bm);
01609    ~LinearEquationSolver() { delete gm; }
01610    void CleanUp() { delete gm; } 
01611    GeneralMatrix* Evaluate(MatrixType) { return gm; }
01612    // probably should have an error message if MatrixType != UnSp
01613    NEW_DELETE(LinearEquationSolver)
01614 };
01615 
01616 // ************************** matrix input *******************************/
01617 
01618 class MatrixInput          // for reading a list of values into a matrix
01619                            // the difficult part is detecting a mismatch
01620                            // in the number of elements
01621 {
01622    int n;                  // number values still to be read
01623    Real* r;                // pointer to next location to be read to
01624 public:
01625    MatrixInput(const MatrixInput& mi) : n(mi.n), r(mi.r) {}
01626    MatrixInput(int nx, Real* rx) : n(nx), r(rx) {}
01627    ~MatrixInput();
01628    MatrixInput operator<<(double);
01629    MatrixInput operator<<(float);
01630    MatrixInput operator<<(int f);
01631    friend class GeneralMatrix;
01632 };
01633 
01634 
01635 
01636 // **************** a very simple integer array class ********************/
01637 
01638 // A minimal array class to imitate a C style array but giving dynamic storage
01639 // mostly intended for internal use by newmat
01640 
01641 class SimpleIntArray : public Janitor
01642 {
01643 protected:
01644    int* a;                    // pointer to the array
01645    int n;                     // length of the array
01646 public:
01647    SimpleIntArray(int xn);    // build an array length xn
01648    SimpleIntArray() : a(0), n(0) {}  // build an array length 0
01649    ~SimpleIntArray();         // return the space to memory
01650    int& operator[](int i);    // access element of the array - start at 0
01651    int operator[](int i) const;
01652                               // access element of constant array
01653    void operator=(int ai);    // set the array equal to a constant
01654    void operator=(const SimpleIntArray& b);
01655                               // copy the elements of an array
01656    SimpleIntArray(const SimpleIntArray& b);
01657                               // make a new array equal to an existing one
01658    int Size() const { return n; }
01659                               // return the size of the array
01660    int size() const { return n; }
01661                               // return the size of the array
01662    int* Data() { return a; }  // pointer to the data
01663    const int* Data() const { return a; }  // pointer to the data
01664    int* data() { return a; }  // pointer to the data
01665    const int* data() const { return a; }  // pointer to the data
01666    const int* const_data() const { return a; }  // pointer to the data
01667    void ReSize(int i, bool keep = false);
01668                               // change length, keep data if keep = true
01669    void resize(int i, bool keep = false) { ReSize(i, keep); }
01670                               // change length, keep data if keep = true
01671    void CleanUp() { ReSize(0); }
01672    NEW_DELETE(SimpleIntArray)
01673 };
01674 
01675 // ********************** C subscript classes ****************************
01676 
01677 class RealStarStar
01678 {
01679    Real** a;
01680 public:
01681    RealStarStar(Matrix& A);
01682    ~RealStarStar() { delete [] a; }
01683    operator Real**() { return a; }
01684 };
01685 
01686 class ConstRealStarStar
01687 {
01688    const Real** a;
01689 public:
01690    ConstRealStarStar(const Matrix& A);
01691    ~ConstRealStarStar() { delete [] a; }
01692    operator const Real**() { return a; }
01693 };
01694 
01695 // *************************** exceptions ********************************/
01696 
01697 class NPDException : public Runtime_error     // Not positive definite
01698 {
01699 public:
01700    static unsigned long Select;          // for identifying exception
01701    NPDException(const GeneralMatrix&);
01702 };
01703 
01704 class ConvergenceException : public Runtime_error
01705 {
01706 public:
01707    static unsigned long Select;          // for identifying exception
01708    ConvergenceException(const GeneralMatrix& A);
01709    ConvergenceException(const char* c);
01710 };
01711 
01712 class SingularException : public Runtime_error
01713 {
01714 public:
01715    static unsigned long Select;          // for identifying exception
01716    SingularException(const GeneralMatrix& A);
01717 };
01718 
01719 class OverflowException : public Runtime_error
01720 {
01721 public:
01722    static unsigned long Select;          // for identifying exception
01723    OverflowException(const char* c);
01724 };
01725 
01726 class ProgramException : public Logic_error
01727 {
01728 protected:
01729    ProgramException();
01730 public:
01731    static unsigned long Select;          // for identifying exception
01732    ProgramException(const char* c);
01733    ProgramException(const char* c, const GeneralMatrix&);
01734    ProgramException(const char* c, const GeneralMatrix&, const GeneralMatrix&);
01735    ProgramException(const char* c, MatrixType, MatrixType);
01736 };
01737 
01738 class IndexException : public Logic_error
01739 {
01740 public:
01741    static unsigned long Select;          // for identifying exception
01742    IndexException(int i, const GeneralMatrix& A);
01743    IndexException(int i, int j, const GeneralMatrix& A);
01744    // next two are for access via element function
01745    IndexException(int i, const GeneralMatrix& A, bool);
01746    IndexException(int i, int j, const GeneralMatrix& A, bool);
01747 };
01748 
01749 class VectorException : public Logic_error    // cannot convert to vector
01750 {
01751 public:
01752    static unsigned long Select;          // for identifying exception
01753    VectorException();
01754    VectorException(const GeneralMatrix& A);
01755 };
01756 
01757 class NotSquareException : public Logic_error
01758 {
01759 public:
01760    static unsigned long Select;          // for identifying exception
01761    NotSquareException(const GeneralMatrix& A);
01762    NotSquareException();
01763 };
01764 
01765 class SubMatrixDimensionException : public Logic_error
01766 {
01767 public:
01768    static unsigned long Select;          // for identifying exception
01769    SubMatrixDimensionException();
01770 };
01771 
01772 class IncompatibleDimensionsException : public Logic_error
01773 {
01774 public:
01775    static unsigned long Select;          // for identifying exception
01776    IncompatibleDimensionsException();
01777    IncompatibleDimensionsException(const GeneralMatrix&);
01778    IncompatibleDimensionsException(const GeneralMatrix&, const GeneralMatrix&);
01779 };
01780 
01781 class NotDefinedException : public Logic_error
01782 {
01783 public:
01784    static unsigned long Select;          // for identifying exception
01785    NotDefinedException(const char* op, const char* matrix);
01786 };
01787 
01788 class CannotBuildException : public Logic_error
01789 {
01790 public:
01791    static unsigned long Select;          // for identifying exception
01792    CannotBuildException(const char* matrix);
01793 };
01794 
01795 
01796 class InternalException : public Logic_error
01797 {
01798 public:
01799    static unsigned long Select;          // for identifying exception
01800    InternalException(const char* c);
01801 };
01802 
01803 // ************************ functions ************************************ //
01804 
01805 bool operator==(const GeneralMatrix& A, const GeneralMatrix& B);
01806 bool operator==(const BaseMatrix& A, const BaseMatrix& B);
01807 inline bool operator!=(const GeneralMatrix& A, const GeneralMatrix& B)
01808    { return ! (A==B); }
01809 inline bool operator!=(const BaseMatrix& A, const BaseMatrix& B)
01810    { return ! (A==B); }
01811 
01812    // inequality operators are dummies included for compatibility
01813    // with STL. They throw an exception if actually called.
01814 inline bool operator<=(const BaseMatrix& A, const BaseMatrix&)
01815    { A.IEQND(); return true; }
01816 inline bool operator>=(const BaseMatrix& A, const BaseMatrix&)
01817    { A.IEQND(); return true; }
01818 inline bool operator<(const BaseMatrix& A, const BaseMatrix&)
01819    { A.IEQND(); return true; }
01820 inline bool operator>(const BaseMatrix& A, const BaseMatrix&)
01821    { A.IEQND(); return true; }
01822 
01823 bool IsZero(const BaseMatrix& A);
01824 
01825 Matrix CrossProduct(const Matrix& A, const Matrix& B);
01826 ReturnMatrix CrossProductRows(const Matrix& A, const Matrix& B);
01827 ReturnMatrix CrossProductColumns(const Matrix& A, const Matrix& B);
01828 
01829 
01830 // ********************* inline functions ******************************** //
01831 
01832 
01833 inline LogAndSign LogDeterminant(const BaseMatrix& B)
01834    { return B.LogDeterminant(); }
01835 inline Real Determinant(const BaseMatrix& B)
01836    { return B.Determinant(); }
01837 inline Real SumSquare(const BaseMatrix& B) { return B.SumSquare(); }
01838 inline Real NormFrobenius(const BaseMatrix& B) { return B.NormFrobenius(); }
01839 inline Real Trace(const BaseMatrix& B) { return B.Trace(); }
01840 inline Real SumAbsoluteValue(const BaseMatrix& B)
01841    { return B.SumAbsoluteValue(); }
01842 inline Real Sum(const BaseMatrix& B)
01843    { return B.Sum(); }
01844 inline Real MaximumAbsoluteValue(const BaseMatrix& B)
01845    { return B.MaximumAbsoluteValue(); }
01846 inline Real MinimumAbsoluteValue(const BaseMatrix& B)
01847    { return B.MinimumAbsoluteValue(); }
01848 inline Real Maximum(const BaseMatrix& B) { return B.Maximum(); }
01849 inline Real Minimum(const BaseMatrix& B) { return B.Minimum(); }
01850 inline Real Norm1(const BaseMatrix& B) { return B.Norm1(); }
01851 inline Real Norm1(RowVector& RV) { return RV.MaximumAbsoluteValue(); }
01852 inline Real NormInfinity(const BaseMatrix& B) { return B.NormInfinity(); }
01853 inline Real NormInfinity(ColumnVector& CV)
01854    { return CV.MaximumAbsoluteValue(); }
01855 inline bool IsZero(const GeneralMatrix& A) { return A.IsZero(); }
01856 
01857 
01858 inline MatrixInput MatrixInput::operator<<(int f) { return *this << (Real)f; }
01859 inline MatrixInput GeneralMatrix::operator<<(int f) { return *this << (Real)f; }
01860 inline MatrixInput BandMatrix::operator<<(int f) { return *this << (Real)f; }
01861 inline MatrixInput GetSubMatrix::operator<<(int f) { return *this << (Real)f; }
01862 
01863 inline void swap(Matrix& A, Matrix& B) { A.swap(B); }
01864 inline void swap(SquareMatrix& A, SquareMatrix& B) { A.swap(B); }
01865 inline void swap(nricMatrix& A, nricMatrix& B) { A.swap(B); }
01866 inline void swap(UpperTriangularMatrix& A, UpperTriangularMatrix& B)
01867    { A.swap(B); }
01868 inline void swap(LowerTriangularMatrix& A, LowerTriangularMatrix& B)
01869    { A.swap(B); }
01870 inline void swap(SymmetricMatrix& A, SymmetricMatrix& B) { A.swap(B); }
01871 inline void swap(DiagonalMatrix& A, DiagonalMatrix& B) { A.swap(B); }
01872 inline void swap(RowVector& A, RowVector& B) { A.swap(B); }
01873 inline void swap(ColumnVector& A, ColumnVector& B) { A.swap(B); }
01874 inline void swap(CroutMatrix& A, CroutMatrix& B) { A.swap(B); }
01875 inline void swap(BandMatrix& A, BandMatrix& B) { A.swap(B); }
01876 inline void swap(UpperBandMatrix& A, UpperBandMatrix& B) { A.swap(B); }
01877 inline void swap(LowerBandMatrix& A, LowerBandMatrix& B) { A.swap(B); }
01878 inline void swap(SymmetricBandMatrix& A, SymmetricBandMatrix& B) { A.swap(B); }
01879 inline void swap(BandLUMatrix& A, BandLUMatrix& B) { A.swap(B); }
01880 inline void swap(IdentityMatrix& A, IdentityMatrix& B) { A.swap(B); }
01881 inline void swap(GenericMatrix& A, GenericMatrix& B) { A.swap(B); }
01882 
01883 
01884 #ifdef OPT_COMPATIBLE                    // for compatibility with opt++
01885 
01886 inline ScaledMatrix operator*(Real f, const BaseMatrix& BM)
01887    { return ScaledMatrix(&BM, f); }
01888 inline Real Norm2(const ColumnVector& CV) { return CV.NormFrobenius(); }
01889 inline Real Dot(ColumnVector& CV1, ColumnVector& CV2)
01890    { return DotProduct(CV1, CV2); }
01891 
01892 #endif
01893 
01894 
01895 #ifdef use_namespace
01896 }
01897 #endif
01898 
01899 
01900 #endif
01901 
01902 // body file: newmat1.cpp
01903 // body file: newmat2.cpp
01904 // body file: newmat3.cpp
01905 // body file: newmat4.cpp
01906 // body file: newmat5.cpp
01907 // body file: newmat6.cpp
01908 // body file: newmat7.cpp
01909 // body file: newmat8.cpp
01910 // body file: newmatex.cpp
01911 // body file: bandmat.cpp
01912 // body file: submat.cpp
01913 
01914 
01915 
01916 
01917 
01918 
01919 
Generated on Mon Jan 24 12:04:37 2011 for FASTlib by  doxygen 1.6.3