home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / n / newmat06.zip / NEWMATRC.H < prev    next >
C/C++ Source or Header  |  1992-07-15  |  5KB  |  124 lines

  1. //$$ newmatrc.h              definition file for row/column classes
  2.  
  3. // Copyright (C) 1991,2: R B Davies
  4.  
  5. #ifndef MATRIXRC_LIB
  6. #define MATRIXRC_LIB 0
  7.  
  8. #include "controlw.h"
  9.  
  10.  
  11.  
  12. /************** classes MatrixRowCol, MatrixRow, MatrixCol *****************/
  13.  
  14. // Used for accessing the rows and columns of matrices
  15. // All matrix classes must provide routines for calculating matrix rows and
  16. // columns. Assume rows can be found very efficiently.
  17.  
  18. enum LSF { LoadOnEntry=1,StoreOnExit=2,IsACopy=4,DirectPart=8,StoreHere=16 };
  19.  
  20.  
  21. class LoadAndStoreFlag : public ControlWord
  22. {
  23. public:
  24.    LoadAndStoreFlag() {}
  25.    LoadAndStoreFlag(int i) : ControlWord(i) {}
  26.    LoadAndStoreFlag(LSF lsf) : ControlWord(lsf) {}
  27.    LoadAndStoreFlag(const ControlWord& cwx) : ControlWord(cwx) {}
  28.    FREE_CHECK(LoadAndStoreFlag)
  29. };
  30.  
  31. class MatrixRowCol
  32. // the row or column of a matrix
  33. {
  34. public:                                        // these are public to avoid
  35.                            // numerous friend statements
  36.    int length;                                 // row or column length
  37.    int skip;                                   // initial number of zeros
  38.    int storage;                                // number of stored elements
  39.    int rowcol;                                 // row or column number
  40.    GeneralMatrix* gm;                          // pointer to parent matrix
  41.    Real* store;                                // pointer to local storage
  42.                            //    less skip
  43.    LoadAndStoreFlag cw;                        // Load? Store? Is a Copy?
  44.    void IncrMat() { rowcol++; store += storage; }
  45.                            // used by NextRow
  46.    void IncrDiag() { rowcol++; skip++; }
  47.    void IncrUT() { rowcol++; storage--; store += storage; skip++; }
  48.    void IncrLT() { rowcol++; store += storage; storage++; }
  49.  
  50. public:
  51.    void Add(const MatrixRowCol&);              // add a row/col
  52.    void AddScaled(const MatrixRowCol&, Real);  // add a multiple of a row/col
  53.    void Add(const MatrixRowCol&, const MatrixRowCol&);
  54.                            // add two rows/cols
  55.    void Add(const MatrixRowCol&, Real);        // add a row/col
  56.    void Sub(const MatrixRowCol&);              // subtract a row/col
  57.    void Sub(const MatrixRowCol&, const MatrixRowCol&);
  58.                            // sub a row/col from another
  59.    void RevSub(const MatrixRowCol&);           // subtract from a row/col
  60.    void Copy(const MatrixRowCol&);             // copy a row/col
  61.    void Copy(const Real*&);                    // copy from an array
  62.    void Copy(Real);                            // copy from constant
  63.    Real SumAbsoluteValue();                    // sum of absolute values
  64.    void Inject(const MatrixRowCol&);           // copy stored els of a row/col
  65.    void Negate(const MatrixRowCol&);           // change sign of a row/col
  66.    void Multiply(const MatrixRowCol&, Real);   // scale a row/col
  67.    friend Real DotProd(const MatrixRowCol&, const MatrixRowCol&);
  68.                            // sum of pairwise product
  69.    Real* operator()() { return store+skip; }   // pointer to first element
  70.    Real* Store() { return store; }
  71.    int Skip() { return skip; }                 // number of elements skipped
  72.    int Storage() { return storage; }           // number of elements stored
  73.    void Skip(int i) { skip=i; }
  74.    void Storage(int i) { storage=i; }
  75.    void SubRowCol(MatrixRowCol&, int, int) const;
  76.                            // get part of a row or column
  77.    MatrixRowCol() {}                           // to stop warning messages
  78.    virtual ~MatrixRowCol();
  79.    FREE_CHECK(MatrixRowCol)
  80. };
  81.  
  82. class MatrixRow : public MatrixRowCol
  83. {
  84. public:
  85.    // bodies for these are inline at the end of this .h file
  86.    MatrixRow(GeneralMatrix*, LoadAndStoreFlag, int=0);
  87.                                                // extract a row
  88.    ~MatrixRow();
  89.    void Next();                                // get next row
  90.    FREE_CHECK(MatrixRow)
  91. };
  92.  
  93. class MatrixCol : public MatrixRowCol
  94. {
  95. public:
  96.    // bodies for these are inline at the end of this .h file
  97.    MatrixCol(GeneralMatrix*, LoadAndStoreFlag, int=0);
  98.                                                // extract a col
  99.    MatrixCol(GeneralMatrix*, Real*, LoadAndStoreFlag, int=0);
  100.                                                // store/retrieve a col
  101.    ~MatrixCol();
  102.    void Next();                                // get next row
  103.    FREE_CHECK(MatrixCol)
  104. };
  105.  
  106.  
  107. /**************************** inline bodies ****************************/
  108.  
  109. inline MatrixRow::MatrixRow(GeneralMatrix* gmx, LoadAndStoreFlag cwx, int row)
  110. { gm=gmx; cw=cwx; rowcol=row; gm->GetRow(*this); } 
  111.  
  112. inline void MatrixRow::Next() { gm->NextRow(*this); }
  113.  
  114. inline MatrixCol::MatrixCol(GeneralMatrix* gmx, LoadAndStoreFlag cwx, int col)
  115. { gm=gmx; cw=cwx; rowcol=col; gm->GetCol(*this); } 
  116.  
  117. inline MatrixCol::MatrixCol(GeneralMatrix* gmx, Real* r,
  118.    LoadAndStoreFlag cwx, int col)
  119. { gm=gmx; store=r; cw=cwx+StoreHere; rowcol=col; gm->GetCol(*this); } 
  120.  
  121. inline void MatrixCol::Next() { gm->NextCol(*this); }
  122.  
  123. #endif
  124.