CoinUtils  trunk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
CoinPackedMatrix.hpp
Go to the documentation of this file.
00001 /* $Id$ */
00002 // Copyright (C) 2000, International Business Machines
00003 // Corporation and others.  All Rights Reserved.
00004 // This code is licensed under the terms of the Eclipse Public License (EPL).
00005 
00006 #ifndef CoinPackedMatrix_H
00007 #define CoinPackedMatrix_H
00008 
00009 #include "CoinError.hpp"
00010 #include "CoinTypes.hpp"
00011 #ifndef CLP_NO_VECTOR
00012 #include "CoinPackedVectorBase.hpp"
00013 #include "CoinShallowPackedVector.hpp"
00014 #else
00015 class CoinRelFltEq;
00016 #endif
00017 
00079 class CoinPackedMatrix  {
00080    friend void CoinPackedMatrixUnitTest();
00081 
00082 public:
00083 
00084 
00085   //---------------------------------------------------------------------------
00089     inline double getExtraGap() const { return extraGap_; }
00091     inline double getExtraMajor() const { return extraMajor_; }
00092 
00095     void reserve(const int newMaxMajorDim, const CoinBigIndex newMaxSize,
00096                  bool create=false);
00098     void clear();
00099 
00101     inline bool isColOrdered() const { return colOrdered_; }
00102 
00104     inline bool hasGaps() const { return (size_<start_[majorDim_]) ; } 
00105 
00107     inline CoinBigIndex getNumElements() const { return size_; }
00108 
00110     inline int getNumCols() const
00111     { return colOrdered_ ? majorDim_ : minorDim_; }
00112 
00114     inline int getNumRows() const
00115     { return colOrdered_ ? minorDim_ : majorDim_; }
00116 
00124     inline const double * getElements() const { return element_; }
00125 
00134     inline const int * getIndices() const { return index_; }
00135 
00140     inline int getSizeVectorStarts() const
00141     { return ((majorDim_ > 0)?(majorDim_+1):(0)) ; }
00142 
00147     inline int getSizeVectorLengths() const { return majorDim_; }
00148 
00154     inline const CoinBigIndex * getVectorStarts() const { return start_; }
00155 
00160     inline const int * getVectorLengths() const { return length_; }
00161 
00164     CoinBigIndex getVectorFirst(const int i) const {
00165 #ifndef COIN_FAST_CODE
00166       if (i < 0 || i >= majorDim_)
00167         throw CoinError("bad index", "vectorFirst", "CoinPackedMatrix");
00168 #endif
00169       return start_[i];
00170     }
00173     CoinBigIndex getVectorLast(const int i) const {
00174 #ifndef COIN_FAST_CODE
00175       if (i < 0 || i >= majorDim_)
00176         throw CoinError("bad index", "vectorLast", "CoinPackedMatrix");
00177 #endif
00178       return start_[i] + length_[i];
00179     }
00181     inline int getVectorSize(const int i) const {
00182 #ifndef COIN_FAST_CODE
00183       if (i < 0 || i >= majorDim_)
00184         throw CoinError("bad index", "vectorSize", "CoinPackedMatrix");
00185 #endif
00186       return length_[i];
00187     }
00188 #ifndef CLP_NO_VECTOR  
00189 
00190     const CoinShallowPackedVector getVector(int i) const {
00191 #ifndef COIN_FAST_CODE
00192       if (i < 0 || i >= majorDim_)
00193         throw CoinError("bad index", "vector", "CoinPackedMatrix");
00194 #endif
00195       return CoinShallowPackedVector(length_[i],
00196                                     index_ + start_[i],
00197                                     element_ + start_[i],
00198                                     false);
00199     }
00200 #endif
00201 
00211     int * getMajorIndices() const;
00213 
00214   //---------------------------------------------------------------------------
00225     void setDimensions(int numrows, int numcols);
00226    
00228     void setExtraGap(const double newGap);
00230     void setExtraMajor(const double newMajor);
00231 #ifndef CLP_NO_VECTOR
00232 
00238     void appendCol(const CoinPackedVectorBase& vec);
00239 #endif
00240 
00246     void appendCol(const int vecsize,
00247                    const int *vecind, const double *vecelem);
00248 #ifndef CLP_NO_VECTOR
00249 
00255     void appendCols(const int numcols,
00256                     const CoinPackedVectorBase * const * cols);
00257 #endif
00258 
00263     int appendCols(const int numcols,
00264                     const CoinBigIndex * columnStarts, const int * row,
00265                    const double * element, int numberRows=-1);
00266 #ifndef CLP_NO_VECTOR
00267 
00273     void appendRow(const CoinPackedVectorBase& vec);
00274 #endif
00275 
00281     void appendRow(const int vecsize,
00282                   const int *vecind, const double *vecelem);
00283 #ifndef CLP_NO_VECTOR
00284 
00290     void appendRows(const int numrows,
00291                     const CoinPackedVectorBase * const * rows);
00292 #endif
00293 
00298     int appendRows(const int numrows,
00299                     const CoinBigIndex * rowStarts, const int * column,
00300                    const double * element, int numberColumns=-1);
00301   
00306     void rightAppendPackedMatrix(const CoinPackedMatrix& matrix);
00311     void bottomAppendPackedMatrix(const CoinPackedMatrix& matrix);
00312   
00314     void deleteCols(const int numDel, const int * indDel);
00316     void deleteRows(const int numDel, const int * indDel);
00317 
00321     void replaceVector(const int index,
00322                        const int numReplace, const double * newElements);
00327     void modifyCoefficient(int row, int column, double newElement,
00328                            bool keepZero=false);
00332     double getCoefficient(int row, int column) const;
00333 
00339     int compress(double threshold);
00344     int eliminateDuplicates(double threshold);
00346     void orderMatrix();
00354     int cleanMatrix(double threshold=1.0e-20);
00356 
00357   //---------------------------------------------------------------------------
00362     void removeGaps(double removeValue=-1.0);
00363  
00367     void submatrixOf(const CoinPackedMatrix& matrix,
00368                      const int numMajor, const int * indMajor);
00372     void submatrixOfWithDuplicates(const CoinPackedMatrix& matrix,
00373                      const int numMajor, const int * indMajor);
00374 #if 0
00375 
00378     void submatrixOf(const CoinPackedMatrix& matrix,
00379                      const int numMajor, const int * indMajor,
00380                      const int numMinor, const int * indMinor);
00381 #endif
00382 
00385     void copyOf(const CoinPackedMatrix& rhs);
00389     void copyOf(const bool colordered,
00390                const int minor, const int major, const CoinBigIndex numels,
00391                const double * elem, const int * ind,
00392                const CoinBigIndex * start, const int * len,
00393                const double extraMajor=0.0, const double extraGap=0.0);
00397     void copyReuseArrays(const CoinPackedMatrix& rhs);
00398 
00406     void reverseOrderedCopyOf(const CoinPackedMatrix& rhs);
00407 
00416     void assignMatrix(const bool colordered,
00417                      const int minor, const int major, 
00418                       const CoinBigIndex numels,
00419                      double *& elem, int *& ind,
00420                      CoinBigIndex *& start, int *& len,
00421                      const int maxmajor = -1, const CoinBigIndex maxsize = -1);
00422  
00423  
00424  
00427     CoinPackedMatrix & operator=(const CoinPackedMatrix& rhs);
00428  
00435     void reverseOrdering();
00436 
00446     void transpose();
00447  
00449     void swap(CoinPackedMatrix& matrix);
00450    
00452 
00453   //---------------------------------------------------------------------------
00459     void times(const double * x, double * y) const;
00460 #ifndef CLP_NO_VECTOR
00461 
00463     void times(const CoinPackedVectorBase& x, double * y) const;
00464 #endif
00465 
00468     void transposeTimes(const double * x, double * y) const;
00469 #ifndef CLP_NO_VECTOR
00470 
00472     void transposeTimes(const CoinPackedVectorBase& x, double * y) const;
00473 #endif
00474 
00475 
00476   //---------------------------------------------------------------------------
00484 
00485     //-------------------------------------------------------------------------
00492       int * countOrthoLength() const;
00495       void countOrthoLength(int * counts) const;
00498       inline int getMajorDim() const { return majorDim_; }
00501       inline void setMajorDim(int value) { majorDim_ = value; }
00504       inline int getMinorDim() const { return minorDim_; }
00507       inline void setMinorDim(int value) { minorDim_ = value; }
00511       inline int getMaxMajorDim() const { return maxMajorDim_; }
00512 
00515       void dumpMatrix(const char* fname = NULL) const;
00516 
00518       void printMatrixElement(const int row_val, const int col_val) const;
00520 
00521     //-------------------------------------------------------------------------
00531 #ifndef CLP_NO_VECTOR
00532 
00533       void appendMajorVector(const CoinPackedVectorBase& vec);
00534 #endif
00535 
00536       void appendMajorVector(const int vecsize, const int *vecind,
00537                              const double *vecelem);
00538 #ifndef CLP_NO_VECTOR
00539 
00540       void appendMajorVectors(const int numvecs,
00541                               const CoinPackedVectorBase * const * vecs);
00542 
00544       void appendMinorVector(const CoinPackedVectorBase& vec);
00545 #endif
00546 
00547       void appendMinorVector(const int vecsize, const int *vecind,
00548                              const double *vecelem);
00549 #ifndef CLP_NO_VECTOR
00550 
00551       void appendMinorVectors(const int numvecs,
00552                               const CoinPackedVectorBase * const * vecs);
00553 #endif
00554 
00566     void appendMinorFast(const int number,
00567                     const CoinBigIndex * starts, const int * index,
00568                     const double * element);
00570 
00571     //-------------------------------------------------------------------------
00585       void majorAppendSameOrdered(const CoinPackedMatrix& matrix);
00590       void minorAppendSameOrdered(const CoinPackedMatrix& matrix);
00596       void majorAppendOrthoOrdered(const CoinPackedMatrix& matrix);
00602       void minorAppendOrthoOrdered(const CoinPackedMatrix& matrix);
00604 
00605       //-----------------------------------------------------------------------
00610       void deleteMajorVectors(const int numDel, const int * indDel);
00613       void deleteMinorVectors(const int numDel, const int * indDel);
00615 
00616       //-----------------------------------------------------------------------
00623       void timesMajor(const double * x, double * y) const;
00624 #ifndef CLP_NO_VECTOR
00625 
00628       void timesMajor(const CoinPackedVectorBase& x, double * y) const;
00629 #endif
00630 
00634       void timesMinor(const double * x, double * y) const;
00635 #ifndef CLP_NO_VECTOR
00636 
00639       void timesMinor(const CoinPackedVectorBase& x, double * y) const;
00640 #endif
00641 
00642 
00643 
00644    //--------------------------------------------------------------------------
00647 #ifndef CLP_NO_VECTOR
00648 
00655    template <class FloatEqual> bool 
00656    isEquivalent(const CoinPackedMatrix& rhs, const FloatEqual& eq) const
00657    {
00658       // Both must be column order or both row ordered and must be of same size
00659       if ((isColOrdered() ^ rhs.isColOrdered()) ||
00660           (getNumCols() != rhs.getNumCols()) ||
00661           (getNumRows() != rhs.getNumRows()) ||
00662           (getNumElements() != rhs.getNumElements()))
00663          return false;
00664      
00665       for (int i=getMajorDim()-1; i >= 0; --i) {
00666         CoinShallowPackedVector pv = getVector(i);
00667         CoinShallowPackedVector rhsPv = rhs.getVector(i);
00668         if ( !pv.isEquivalent(rhsPv,eq) )
00669           return false;
00670       }
00671       return true;
00672    }
00673 
00680   bool isEquivalent2(const CoinPackedMatrix& rhs) const;
00681 #else
00682 
00690   bool isEquivalent(const CoinPackedMatrix& rhs, const CoinRelFltEq & eq) const;
00691 #endif
00692 
00696    bool isEquivalent(const CoinPackedMatrix& rhs) const;
00698 
00699    //--------------------------------------------------------------------------
00709     inline double * getMutableElements() const { return element_; }
00715     inline int * getMutableIndices() const { return index_; }
00716 
00719     inline CoinBigIndex * getMutableVectorStarts() const { return start_; }
00721     inline int * getMutableVectorLengths() const { return length_; }
00723     inline void setNumElements(CoinBigIndex value)
00724     { size_ = value;}
00729     inline void nullElementArray() {element_=NULL;}
00730 
00735     inline void nullStartArray() {start_=NULL;}
00736 
00741     inline void nullLengthArray() {length_=NULL;}
00742 
00747     inline void nullIndexArray() {index_=NULL;}
00749 
00750    //--------------------------------------------------------------------------
00753 
00754    CoinPackedMatrix();
00755 
00757    CoinPackedMatrix(const bool colordered,
00758                    const double extraMajor, const double extraGap);
00759 
00760    CoinPackedMatrix(const bool colordered,
00761                    const int minor, const int major, const CoinBigIndex numels,
00762                    const double * elem, const int * ind,
00763                    const CoinBigIndex * start, const int * len,
00764                    const double extraMajor, const double extraGap);
00765 
00766    CoinPackedMatrix(const bool colordered,
00767                    const int minor, const int major, const CoinBigIndex numels,
00768                    const double * elem, const int * ind,
00769                    const CoinBigIndex * start, const int * len);
00770 
00781    CoinPackedMatrix(const bool colordered,
00782      const int * rowIndices, 
00783      const int * colIndices, 
00784      const double * elements, 
00785      CoinBigIndex numels ); 
00786 
00788    CoinPackedMatrix(const CoinPackedMatrix& m);
00789 
00804   CoinPackedMatrix(const CoinPackedMatrix &m,
00805                    int extraForMajor, int extraElements,
00806                    bool reverseOrdering = false) ;
00807 
00810   CoinPackedMatrix (const CoinPackedMatrix & wholeModel,
00811                     int numberRows, const int * whichRows,
00812                     int numberColumns, const int * whichColumns);
00813 
00815    virtual ~CoinPackedMatrix();    
00817 
00841     int verifyMtx(int verbosity = 1, bool zeroesAreError = false) const ;
00843 
00844   //--------------------------------------------------------------------------
00845 protected:
00846    void gutsOfDestructor();
00847    void gutsOfCopyOf(const bool colordered,
00848                      const int minor, const int major, const CoinBigIndex numels,
00849                      const double * elem, const int * ind,
00850                      const CoinBigIndex * start, const int * len,
00851                      const double extraMajor=0.0, const double extraGap=0.0);
00853    void gutsOfCopyOfNoGaps(const bool colordered,
00854                      const int minor, const int major,
00855                      const double * elem, const int * ind,
00856                            const CoinBigIndex * start);
00857    void gutsOfOpEqual(const bool colordered,
00858                       const int minor, const int major, const CoinBigIndex numels,
00859                       const double * elem, const int * ind,
00860                       const CoinBigIndex * start, const int * len);
00861    void resizeForAddingMajorVectors(const int numVec, const int * lengthVec);
00862    void resizeForAddingMinorVectors(const int * addedEntries);
00863 
00873     int appendMajor(const int number,
00874                     const CoinBigIndex * starts, const int * index,
00875                     const double * element, int numberOther=-1);
00885     int appendMinor(const int number,
00886                     const CoinBigIndex * starts, const int * index,
00887                     const double * element, int numberOther=-1);
00888 
00889 private:
00890    inline CoinBigIndex getLastStart() const {
00891       return majorDim_ == 0 ? 0 : start_[majorDim_];
00892    }
00893 
00894    //--------------------------------------------------------------------------
00895 protected:
00900    bool     colOrdered_;
00905    double   extraGap_;
00909    double   extraMajor_;
00910 
00913    double  *element_;
00916    int     *index_;
00918    CoinBigIndex     *start_;
00920    int     *length_;
00921 
00923    int majorDim_;
00925    int minorDim_;
00927    CoinBigIndex size_;
00928 
00930    int maxMajorDim_;
00932    CoinBigIndex maxSize_;
00934 };
00935 
00936 //#############################################################################
00944 void
00945 CoinPackedMatrixUnitTest();
00946 
00947 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines