Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

CoinPackedMatrix.hpp

00001 // Copyright (C) 2000, International Business Machines
00002 // Corporation and others.  All Rights Reserved.
00003 #ifndef CoinPackedMatrix_H
00004 #define CoinPackedMatrix_H
00005 
00006 #include "CoinError.hpp"
00007 #include "CoinPackedVectorBase.hpp"
00008 #include "CoinShallowPackedVector.hpp"
00009 
00056 #ifndef COIN_BIG_INDEX
00057 typedef int CoinBigIndex;
00058 #else
00059 typedef long CoinBigIndex;
00060 #endif
00061 class CoinPackedMatrix  {
00062    friend void CoinPackedMatrixUnitTest();
00063 
00064 public:
00065 
00066 
00067   //---------------------------------------------------------------------------
00071     double getExtraGap() const { return extraGap_; }
00073     double getExtraMajor() const { return extraMajor_; }
00074 
00076     void reserve(const int newMaxMajorDim, const CoinBigIndex newMaxSize);
00078     void clear();
00079 
00081     bool isColOrdered() const { return colOrdered_; }
00083     CoinBigIndex getNumElements() const { return size_; }
00085     int getNumCols() const { return colOrdered_ ? majorDim_ : minorDim_; }
00087     int getNumRows() const { return colOrdered_ ? minorDim_ : majorDim_; }
00088 
00093     inline const double * getElements() const { return element_; }
00099     inline const int * getIndices() const { return index_; }
00100 
00102     int getSizeVectorStarts()const { return majorDim_ > 0 ? majorDim_+1 : 0;}
00104     int getSizeVectorLengths() const { return majorDim_; }
00107     inline const CoinBigIndex * getVectorStarts() const { return start_; }
00109     inline const int * getVectorLengths() const { return length_; }
00110 
00111 
00114     CoinBigIndex getVectorFirst(const int i) const throw(CoinError) {
00115       if (i < 0 || i >= majorDim_)
00116         throw CoinError("bad index", "vectorFirst", "CoinPackedMatrix");
00117       return start_[i];
00118     }
00121     CoinBigIndex getVectorLast(const int i) const throw(CoinError) {
00122       if (i < 0 || i >= majorDim_)
00123         throw CoinError("bad index", "vectorLast", "CoinPackedMatrix");
00124       return start_[i] + length_[i];
00125     }
00127     int getVectorSize(const int i) const throw(CoinError) {
00128       if (i < 0 || i >= majorDim_)
00129         throw CoinError("bad index", "vectorSize", "CoinPackedMatrix");
00130       return length_[i];
00131     }
00132   
00134     const CoinShallowPackedVector getVector(int i) const throw(CoinError) {
00135       if (i < 0 || i >= majorDim_)
00136         throw CoinError("bad index", "vector", "CoinPackedMatrix");
00137       return CoinShallowPackedVector(length_[i],
00138                                     index_ + start_[i],
00139                                     element_ + start_[i],
00140                                     false);
00141     }
00143 
00144   //---------------------------------------------------------------------------
00152     void setDimensions(int numrows, int numcols) throw(CoinError);
00153    
00155     void setExtraGap(const double newGap) throw(CoinError);
00157     void setExtraMajor(const double newMajor) throw(CoinError);
00158 
00163     void appendCol(const CoinPackedVectorBase& vec) throw(CoinError);
00168     void appendCol(const int vecsize,
00169                   const int *vecind, const double *vecelem) throw(CoinError);
00175     void appendCols(const int numcols,
00176                     const CoinPackedVectorBase * const * cols) throw(CoinError);
00177 
00182     void appendRow(const CoinPackedVectorBase& vec) throw(CoinError);
00187     void appendRow(const int vecsize,
00188                   const int *vecind, const double *vecelem) throw(CoinError);
00194     void appendRows(const int numrows,
00195                     const CoinPackedVectorBase * const * rows) throw(CoinError);
00196   
00201     void rightAppendPackedMatrix(const CoinPackedMatrix& matrix)
00202       throw(CoinError);
00207     void bottomAppendPackedMatrix(const CoinPackedMatrix& matrix)
00208       throw(CoinError);
00209   
00211     void deleteCols(const int numDel, const int * indDel);
00213     void deleteRows(const int numDel, const int * indDel);
00214 
00218     void replaceVector(const int index,
00219                        const int numReplace, const double * newElements);
00224     void modifyCoefficient(int row, int column, double newElement,
00225                            bool keepZero=false);
00226 
00232     int compress(double threshold);
00237     int eliminateDuplicates(double threshold);
00239 
00240   //---------------------------------------------------------------------------
00244     void removeGaps();
00245  
00249     void submatrixOf(const CoinPackedMatrix& matrix,
00250                      const int numMajor, const int * indMajor)
00251        throw(CoinError);
00252 #if 0
00253 
00256     void submatrixOf(const CoinPackedMatrix& matrix,
00257                      const int numMajor, const int * indMajor,
00258                      const int numMinor, const int * indMinor) throw(CoinError);
00259 #endif
00260 
00263     void copyOf(const CoinPackedMatrix& rhs);
00267     void copyOf(const bool colordered,
00268                const int minor, const int major, const CoinBigIndex numels,
00269                const double * elem, const int * ind,
00270                const CoinBigIndex * start, const int * len,
00271                const double extraMajor=0.0, const double extraGap=0.0);
00275     void reverseOrderedCopyOf(const CoinPackedMatrix& rhs);
00284     void assignMatrix(const bool colordered,
00285                      const int minor, const int major, 
00286                       const CoinBigIndex numels,
00287                      double *& elem, int *& ind,
00288                      CoinBigIndex *& start, int *& len,
00289                      const int maxmajor = -1, const CoinBigIndex maxsize = -1);
00290  
00291  
00292  
00295     CoinPackedMatrix & operator=(const CoinPackedMatrix& rhs);
00296  
00298     void reverseOrdering();
00305     void transpose();
00306  
00308     void swap(CoinPackedMatrix& matrix);
00309    
00311 
00312   //---------------------------------------------------------------------------
00318     void times(const double * x, double * y) const;
00321     void times(const CoinPackedVectorBase& x, double * y) const;
00325     void transposeTimes(const double * x, double * y) const;
00328     void transposeTimes(const CoinPackedVectorBase& x, double * y) const;
00330 
00331   //---------------------------------------------------------------------------
00339 
00340     //-------------------------------------------------------------------------
00347       int * countOrthoLength() const;
00350       int getMajorDim() const { return majorDim_; }
00353       int getMinorDim() const { return minorDim_; }
00357       int getMaxMajorDim() const { return maxMajorDim_; }
00358 
00361       void dumpMatrix(const char* fname = NULL) const;
00363 
00364     //-------------------------------------------------------------------------
00372       void appendMajorVector(const CoinPackedVectorBase& vec) throw(CoinError);
00374       void appendMajorVector(const int vecsize, const int *vecind,
00375                              const double *vecelem) throw(CoinError);
00377       void appendMajorVectors(const int numvecs,
00378                               const CoinPackedVectorBase * const * vecs)
00379         throw(CoinError);
00380 
00382       void appendMinorVector(const CoinPackedVectorBase& vec) throw(CoinError);
00384       void appendMinorVector(const int vecsize, const int *vecind,
00385                              const double *vecelem) throw(CoinError);
00387       void appendMinorVectors(const int numvecs,
00388                               const CoinPackedVectorBase * const * vecs)
00389         throw(CoinError);
00391 
00392     //-------------------------------------------------------------------------
00405       void majorAppendSameOrdered(const CoinPackedMatrix& matrix)
00406          throw(CoinError);
00411       void minorAppendSameOrdered(const CoinPackedMatrix& matrix)
00412          throw(CoinError);
00418       void majorAppendOrthoOrdered(const CoinPackedMatrix& matrix)
00419          throw(CoinError);
00425       void minorAppendOrthoOrdered(const CoinPackedMatrix& matrix)
00426          throw(CoinError);
00428 
00429       //-----------------------------------------------------------------------
00434       void deleteMajorVectors(const int numDel,
00435                               const int * indDel) throw(CoinError);
00438       void deleteMinorVectors(const int numDel,
00439                               const int * indDel) throw(CoinError);
00441 
00442       //-----------------------------------------------------------------------
00449       void timesMajor(const double * x, double * y) const;
00453       void timesMajor(const CoinPackedVectorBase& x, double * y) const;
00458       void timesMinor(const double * x, double * y) const;
00462       void timesMinor(const CoinPackedVectorBase& x, double * y) const;
00464 
00465 
00466    //--------------------------------------------------------------------------
00474    template <class FloatEqual> bool 
00475    isEquivalent(const CoinPackedMatrix& rhs, const FloatEqual& eq) const
00476    {
00477       // Both must be column order or both row ordered and must be of same size
00478       if ((isColOrdered() ^ rhs.isColOrdered()) ||
00479           (getNumCols() != rhs.getNumCols()) ||
00480           (getNumRows() != rhs.getNumRows()) ||
00481           (getNumElements() != rhs.getNumElements()))
00482          return false;
00483      
00484       for (int i=getMajorDim()-1; i >= 0; --i) {
00485         CoinShallowPackedVector pv = getVector(i);
00486         CoinShallowPackedVector rhsPv = rhs.getVector(i);
00487         if ( !pv.isEquivalent(rhsPv,eq) )
00488           return false;
00489       }
00490       return true;
00491    }
00492    
00494    bool isEquivalent(const CoinPackedMatrix& rhs) const
00495    {
00496       return isEquivalent(rhs,  CoinRelFltEq());
00497    }
00498   bool isEquivalent2(const CoinPackedMatrix& rhs) const;
00500 
00501    //--------------------------------------------------------------------------
00504 
00505    CoinPackedMatrix();
00506 
00508    CoinPackedMatrix(const bool colordered,
00509                    const double extraMajor, const double extraGap);
00510 
00511    CoinPackedMatrix(const bool colordered,
00512                    const int minor, const int major, const CoinBigIndex numels,
00513                    const double * elem, const int * ind,
00514                    const CoinBigIndex * start, const int * len,
00515                    const double extraMajor, const double extraGap);
00516 
00517    CoinPackedMatrix(const bool colordered,
00518                    const int minor, const int major, const CoinBigIndex numels,
00519                    const double * elem, const int * ind,
00520                    const CoinBigIndex * start, const int * len);
00521 
00532    CoinPackedMatrix(const bool colordered,
00533      const int * rowIndices, 
00534      const int * colIndices, 
00535      const double * elements, 
00536      CoinBigIndex numels ); 
00537 
00539    CoinPackedMatrix(const CoinPackedMatrix& m);
00540 
00543   CoinPackedMatrix (const CoinPackedMatrix & wholeModel,
00544                     int numberRows, const int * whichRows,
00545                     int numberColumns, const int * whichColumns);
00546 
00548    virtual ~CoinPackedMatrix();    
00550 
00551    //--------------------------------------------------------------------------
00552 protected:
00553    void gutsOfDestructor();
00554    void gutsOfCopyOf(const bool colordered,
00555                      const int minor, const int major, const CoinBigIndex numels,
00556                      const double * elem, const int * ind,
00557                      const CoinBigIndex * start, const int * len,
00558                      const double extraMajor=0.0, const double extraGap=0.0);
00559    void gutsOfOpEqual(const bool colordered,
00560                       const int minor, const int major, const CoinBigIndex numels,
00561                       const double * elem, const int * ind,
00562                       const CoinBigIndex * start, const int * len);
00563    void resizeForAddingMajorVectors(const int numVec, const int * lengthVec);
00564    void resizeForAddingMinorVectors(const int * addedEntries);
00565 private:
00566    inline CoinBigIndex getLastStart() const {
00567       return majorDim_ == 0 ? 0 : start_[majorDim_];
00568    }
00569 
00570    //--------------------------------------------------------------------------
00571 protected:
00576    bool     colOrdered_;
00581    double   extraGap_;
00585    double   extraMajor_;
00586 
00589    double  *element_;
00592    int     *index_;
00594    CoinBigIndex     *start_;
00596    int     *length_;
00597 
00599    int majorDim_;
00601    int minorDim_;
00603    CoinBigIndex size_;
00604 
00606    int maxMajorDim_;
00608    CoinBigIndex maxSize_;
00610 };
00611 
00612 //#############################################################################
00618 void
00619 CoinPackedMatrixUnitTest();
00620 
00621 #endif

Generated on Wed Dec 3 14:34:21 2003 for Coin by doxygen 1.3.5