| 1 | //$$ newmatrc.h              definition file for row/column classes | 
|---|
| 2 |  | 
|---|
| 3 | // Copyright (C) 1991,2,3,4,7: R B Davies | 
|---|
| 4 |  | 
|---|
| 5 | #ifndef NEWMATRC_LIB | 
|---|
| 6 | #define NEWMATRC_LIB 0 | 
|---|
| 7 |  | 
|---|
| 8 | #ifdef use_namespace | 
|---|
| 9 | namespace NEWMAT { | 
|---|
| 10 | #endif | 
|---|
| 11 |  | 
|---|
| 12 | #include "controlw.h" | 
|---|
| 13 |  | 
|---|
| 14 |  | 
|---|
| 15 | /************** classes MatrixRowCol, MatrixRow, MatrixCol *****************/ | 
|---|
| 16 |  | 
|---|
| 17 | // Used for accessing the rows and columns of matrices | 
|---|
| 18 | // All matrix classes must provide routines for calculating matrix rows and | 
|---|
| 19 | // columns. Assume rows can be found very efficiently. | 
|---|
| 20 |  | 
|---|
| 21 | enum LSF { LoadOnEntry=1,StoreOnExit=2,DirectPart=4,StoreHere=8,HaveStore=16 }; | 
|---|
| 22 |  | 
|---|
| 23 |  | 
|---|
| 24 | class LoadAndStoreFlag : public ControlWord | 
|---|
| 25 | { | 
|---|
| 26 | public: | 
|---|
| 27 |    LoadAndStoreFlag() {} | 
|---|
| 28 |    LoadAndStoreFlag(int i) : ControlWord(i) {} | 
|---|
| 29 |    LoadAndStoreFlag(LSF lsf) : ControlWord(lsf) {} | 
|---|
| 30 |    LoadAndStoreFlag(const ControlWord& cwx) : ControlWord(cwx) {} | 
|---|
| 31 | }; | 
|---|
| 32 |  | 
|---|
| 33 | class MatrixRowCol | 
|---|
| 34 | // the row or column of a matrix | 
|---|
| 35 | { | 
|---|
| 36 | public:                                        // these are public to avoid | 
|---|
| 37 |                                                // numerous friend statements | 
|---|
| 38 |    int length;                                 // row or column length | 
|---|
| 39 |    int skip;                                   // initial number of zeros | 
|---|
| 40 |    int storage;                                // number of stored elements | 
|---|
| 41 |    int rowcol;                                 // row or column number | 
|---|
| 42 |    GeneralMatrix* gm;                          // pointer to parent matrix | 
|---|
| 43 |    Real* data;                                 // pointer to local storage | 
|---|
| 44 |    LoadAndStoreFlag cw;                        // Load? Store? Is a Copy? | 
|---|
| 45 |    void IncrMat() { rowcol++; data += storage; }   // used by NextRow | 
|---|
| 46 |    void IncrDiag() { rowcol++; skip++; data++; } | 
|---|
| 47 |    void IncrId() { rowcol++; skip++; } | 
|---|
| 48 |    void IncrUT() { rowcol++; data += storage; storage--; skip++; } | 
|---|
| 49 |    void IncrLT() { rowcol++; data += storage; storage++; } | 
|---|
| 50 |  | 
|---|
| 51 | public: | 
|---|
| 52 |    void Zero();                                // set elements to zero | 
|---|
| 53 |    void Add(const MatrixRowCol&);              // add a row/col | 
|---|
| 54 |    void AddScaled(const MatrixRowCol&, Real);  // add a multiple of a row/col | 
|---|
| 55 |    void Add(const MatrixRowCol&, const MatrixRowCol&); | 
|---|
| 56 |                                                // add two rows/cols | 
|---|
| 57 |    void Add(const MatrixRowCol&, Real);        // add a row/col | 
|---|
| 58 |    void NegAdd(const MatrixRowCol&, Real);     // Real - a row/col | 
|---|
| 59 |    void Sub(const MatrixRowCol&);              // subtract a row/col | 
|---|
| 60 |    void Sub(const MatrixRowCol&, const MatrixRowCol&); | 
|---|
| 61 |                                                // sub a row/col from another | 
|---|
| 62 |    void RevSub(const MatrixRowCol&);           // subtract from a row/col | 
|---|
| 63 |    void ConCat(const MatrixRowCol&, const MatrixRowCol&); | 
|---|
| 64 |                                                // concatenate two row/cols | 
|---|
| 65 |    void Multiply(const MatrixRowCol&);         // multiply a row/col | 
|---|
| 66 |    void Multiply(const MatrixRowCol&, const MatrixRowCol&); | 
|---|
| 67 |                                                // multiply two row/cols | 
|---|
| 68 |    void KP(const MatrixRowCol&, const MatrixRowCol&); | 
|---|
| 69 |                                                // Kronecker Product two row/cols | 
|---|
| 70 |    void Copy(const MatrixRowCol&);             // copy a row/col | 
|---|
| 71 |    void CopyCheck(const MatrixRowCol&);        // ... check for data loss | 
|---|
| 72 |    void Check(const MatrixRowCol&);            // just check for data loss | 
|---|
| 73 |    void Check();                               // check full row/col present | 
|---|
| 74 |    void Copy(const Real*&);                    // copy from an array | 
|---|
| 75 |    void Copy(Real);                            // copy from constant | 
|---|
| 76 |    void Add(Real);                             // add a constant | 
|---|
| 77 |    void Multiply(Real);                        // multiply by constant | 
|---|
| 78 |    Real SumAbsoluteValue();                    // sum of absolute values | 
|---|
| 79 |    Real MaximumAbsoluteValue1(Real r, int& i); // maximum of absolute values | 
|---|
| 80 |    Real MinimumAbsoluteValue1(Real r, int& i); // minimum of absolute values | 
|---|
| 81 |    Real Maximum1(Real r, int& i);              // maximum | 
|---|
| 82 |    Real Minimum1(Real r, int& i);              // minimum | 
|---|
| 83 |    Real Sum();                                 // sum of values | 
|---|
| 84 |    void Inject(const MatrixRowCol&);           // copy stored els of a row/col | 
|---|
| 85 |    void Negate(const MatrixRowCol&);           // change sign of a row/col | 
|---|
| 86 |    void Multiply(const MatrixRowCol&, Real);   // scale a row/col | 
|---|
| 87 |    friend Real DotProd(const MatrixRowCol&, const MatrixRowCol&); | 
|---|
| 88 |                                                // sum of pairwise product | 
|---|
| 89 |    Real* Data() { return data; } | 
|---|
| 90 |    int Skip() { return skip; }                 // number of elements skipped | 
|---|
| 91 |    int Storage() { return storage; }           // number of elements stored | 
|---|
| 92 |    int Length() { return length; }             // length of row or column | 
|---|
| 93 |    void Skip(int i) { skip=i; } | 
|---|
| 94 |    void Storage(int i) { storage=i; } | 
|---|
| 95 |    void Length(int i) { length=i; } | 
|---|
| 96 |    void SubRowCol(MatrixRowCol&, int, int) const; | 
|---|
| 97 |                                                // get part of a row or column | 
|---|
| 98 |    MatrixRowCol() {}                           // to stop warning messages | 
|---|
| 99 |    ~MatrixRowCol(); | 
|---|
| 100 |    FREE_CHECK(MatrixRowCol) | 
|---|
| 101 | }; | 
|---|
| 102 |  | 
|---|
| 103 | class MatrixRow : public MatrixRowCol | 
|---|
| 104 | { | 
|---|
| 105 | public: | 
|---|
| 106 |    // bodies for these are inline at the end of this .h file | 
|---|
| 107 |    MatrixRow(GeneralMatrix*, LoadAndStoreFlag, int=0); | 
|---|
| 108 |                                                // extract a row | 
|---|
| 109 |    ~MatrixRow(); | 
|---|
| 110 |    void Next();                                // get next row | 
|---|
| 111 |    FREE_CHECK(MatrixRow) | 
|---|
| 112 | }; | 
|---|
| 113 |  | 
|---|
| 114 | class MatrixCol : public MatrixRowCol | 
|---|
| 115 | { | 
|---|
| 116 | public: | 
|---|
| 117 |    // bodies for these are inline at the end of this .h file | 
|---|
| 118 |    MatrixCol(GeneralMatrix*, LoadAndStoreFlag, int=0); | 
|---|
| 119 |                                                // extract a col | 
|---|
| 120 |    MatrixCol(GeneralMatrix*, Real*, LoadAndStoreFlag, int=0); | 
|---|
| 121 |                                                // store/retrieve a col | 
|---|
| 122 |    ~MatrixCol(); | 
|---|
| 123 |    void Next();                                // get next row | 
|---|
| 124 |    FREE_CHECK(MatrixCol) | 
|---|
| 125 | }; | 
|---|
| 126 |  | 
|---|
| 127 | // MatrixColX is an alternative to MatrixCol where the complete | 
|---|
| 128 | // column is stored externally | 
|---|
| 129 |  | 
|---|
| 130 | class MatrixColX : public MatrixRowCol | 
|---|
| 131 | { | 
|---|
| 132 | public: | 
|---|
| 133 |    // bodies for these are inline at the end of this .h file | 
|---|
| 134 |    MatrixColX(GeneralMatrix*, Real*, LoadAndStoreFlag, int=0); | 
|---|
| 135 |                                                // store/retrieve a col | 
|---|
| 136 |    ~MatrixColX(); | 
|---|
| 137 |    void Next();                                // get next row | 
|---|
| 138 |    Real* store;                                // pointer to local storage | 
|---|
| 139 |                                                //    less skip | 
|---|
| 140 |    FREE_CHECK(MatrixColX) | 
|---|
| 141 | }; | 
|---|
| 142 |  | 
|---|
| 143 | /**************************** inline bodies ****************************/ | 
|---|
| 144 |  | 
|---|
| 145 | inline MatrixRow::MatrixRow(GeneralMatrix* gmx, LoadAndStoreFlag cwx, int row) | 
|---|
| 146 | { gm=gmx; cw=cwx; rowcol=row; gm->GetRow(*this); } | 
|---|
| 147 |  | 
|---|
| 148 | inline void MatrixRow::Next() { gm->NextRow(*this); } | 
|---|
| 149 |  | 
|---|
| 150 | inline MatrixCol::MatrixCol(GeneralMatrix* gmx, LoadAndStoreFlag cwx, int col) | 
|---|
| 151 | { gm=gmx; cw=cwx; rowcol=col; gm->GetCol(*this); } | 
|---|
| 152 |  | 
|---|
| 153 | inline MatrixCol::MatrixCol(GeneralMatrix* gmx, Real* r, | 
|---|
| 154 |    LoadAndStoreFlag cwx, int col) | 
|---|
| 155 | { gm=gmx; data=r; cw=cwx+StoreHere; rowcol=col; gm->GetCol(*this); } | 
|---|
| 156 |  | 
|---|
| 157 | inline MatrixColX::MatrixColX(GeneralMatrix* gmx, Real* r, | 
|---|
| 158 |    LoadAndStoreFlag cwx, int col) | 
|---|
| 159 | { gm=gmx; store=data=r; cw=cwx+StoreHere; rowcol=col; gm->GetCol(*this); } | 
|---|
| 160 |  | 
|---|
| 161 |  | 
|---|
| 162 | inline void MatrixCol::Next() { gm->NextCol(*this); } | 
|---|
| 163 |  | 
|---|
| 164 | inline void MatrixColX::Next() { gm->NextCol(*this); } | 
|---|
| 165 |  | 
|---|
| 166 | #ifdef use_namespace | 
|---|
| 167 | } | 
|---|
| 168 | #endif | 
|---|
| 169 |  | 
|---|
| 170 | #endif | 
|---|