00001
00002
00003
00004 #ifndef CoinPackedMatrix_H
00005 #define CoinPackedMatrix_H
00006
00007 #include "CoinError.hpp"
00008 #ifndef CLP_NO_VECTOR
00009 #include "CoinPackedVectorBase.hpp"
00010 #include "CoinShallowPackedVector.hpp"
00011 #else
00012 #include "CoinFinite.hpp"
00013 #include "CoinFloatEqual.hpp"
00014 #endif
00015
00062 class CoinPackedMatrix {
00063 friend void CoinPackedMatrixUnitTest();
00064
00065 public:
00066
00067
00068
00072 inline double getExtraGap() const { return extraGap_; }
00074 inline double getExtraMajor() const { return extraMajor_; }
00075
00078 void reserve(const int newMaxMajorDim, const CoinBigIndex newMaxSize,
00079 bool create=false);
00081 void clear();
00082
00084 inline bool isColOrdered() const { return colOrdered_; }
00086 inline bool hasGaps() const
00087 { return size_<start_[majorDim_];}
00088
00090 inline CoinBigIndex getNumElements() const { return size_; }
00092 inline int getNumCols() const { return colOrdered_ ? majorDim_ : minorDim_; }
00094 inline int getNumRows() const { return colOrdered_ ? minorDim_ : majorDim_; }
00095
00100 inline const double * getElements() const { return element_; }
00106 inline const int * getIndices() const { return index_; }
00107
00109 inline int getSizeVectorStarts()const { return majorDim_ > 0 ? majorDim_+1 : 0;}
00111 inline int getSizeVectorLengths() const { return majorDim_; }
00114 inline const CoinBigIndex * getVectorStarts() const { return start_; }
00116 inline const int * getVectorLengths() const { return length_; }
00117
00118
00121 CoinBigIndex getVectorFirst(const int i) const {
00122 #ifndef COIN_FAST_CODE
00123 if (i < 0 || i >= majorDim_)
00124 throw CoinError("bad index", "vectorFirst", "CoinPackedMatrix");
00125 #endif
00126 return start_[i];
00127 }
00130 CoinBigIndex getVectorLast(const int i) const {
00131 #ifndef COIN_FAST_CODE
00132 if (i < 0 || i >= majorDim_)
00133 throw CoinError("bad index", "vectorLast", "CoinPackedMatrix");
00134 #endif
00135 return start_[i] + length_[i];
00136 }
00138 inline int getVectorSize(const int i) const {
00139 #ifndef COIN_FAST_CODE
00140 if (i < 0 || i >= majorDim_)
00141 throw CoinError("bad index", "vectorSize", "CoinPackedMatrix");
00142 #endif
00143 return length_[i];
00144 }
00145 #ifndef CLP_NO_VECTOR
00146
00147 const CoinShallowPackedVector getVector(int i) const {
00148 #ifndef COIN_FAST_CODE
00149 if (i < 0 || i >= majorDim_)
00150 throw CoinError("bad index", "vector", "CoinPackedMatrix");
00151 #endif
00152 return CoinShallowPackedVector(length_[i],
00153 index_ + start_[i],
00154 element_ + start_[i],
00155 false);
00156 }
00157 #endif
00158
00168 int * getMajorIndices() const;
00170
00171
00179 void setDimensions(int numrows, int numcols);
00180
00182 void setExtraGap(const double newGap);
00184 void setExtraMajor(const double newMajor);
00185 #ifndef CLP_NO_VECTOR
00186
00190 void appendCol(const CoinPackedVectorBase& vec);
00191 #endif
00192
00196 void appendCol(const int vecsize,
00197 const int *vecind, const double *vecelem);
00198 #ifndef CLP_NO_VECTOR
00199
00204 void appendCols(const int numcols,
00205 const CoinPackedVectorBase * const * cols);
00206 #endif
00207
00210 int appendCols(const int numcols,
00211 const CoinBigIndex * columnStarts, const int * row,
00212 const double * element, int numberRows=-1);
00213 #ifndef CLP_NO_VECTOR
00214
00218 void appendRow(const CoinPackedVectorBase& vec);
00219 #endif
00220
00224 void appendRow(const int vecsize,
00225 const int *vecind, const double *vecelem);
00226 #ifndef CLP_NO_VECTOR
00227
00232 void appendRows(const int numrows,
00233 const CoinPackedVectorBase * const * rows);
00234 #endif
00235
00238 int appendRows(const int numrows,
00239 const CoinBigIndex * rowStarts, const int * column,
00240 const double * element, int numberColumns=-1);
00241
00246 void rightAppendPackedMatrix(const CoinPackedMatrix& matrix);
00251 void bottomAppendPackedMatrix(const CoinPackedMatrix& matrix);
00252
00254 void deleteCols(const int numDel, const int * indDel);
00256 void deleteRows(const int numDel, const int * indDel);
00257
00261 void replaceVector(const int index,
00262 const int numReplace, const double * newElements);
00267 void modifyCoefficient(int row, int column, double newElement,
00268 bool keepZero=false);
00272 double getCoefficient(int row, int column) const;
00273
00279 int compress(double threshold);
00284 int eliminateDuplicates(double threshold);
00286 void orderMatrix();
00294 int cleanMatrix(double threshold=1.0e-20);
00296
00297
00302 void removeGaps(double removeValue=-1.0);
00303
00307 void submatrixOf(const CoinPackedMatrix& matrix,
00308 const int numMajor, const int * indMajor);
00312 void submatrixOfWithDuplicates(const CoinPackedMatrix& matrix,
00313 const int numMajor, const int * indMajor);
00314 #if 0
00315
00318 void submatrixOf(const CoinPackedMatrix& matrix,
00319 const int numMajor, const int * indMajor,
00320 const int numMinor, const int * indMinor);
00321 #endif
00322
00325 void copyOf(const CoinPackedMatrix& rhs);
00329 void copyOf(const bool colordered,
00330 const int minor, const int major, const CoinBigIndex numels,
00331 const double * elem, const int * ind,
00332 const CoinBigIndex * start, const int * len,
00333 const double extraMajor=0.0, const double extraGap=0.0);
00337 void copyReuseArrays(const CoinPackedMatrix& rhs);
00341 void reverseOrderedCopyOf(const CoinPackedMatrix& rhs);
00350 void assignMatrix(const bool colordered,
00351 const int minor, const int major,
00352 const CoinBigIndex numels,
00353 double *& elem, int *& ind,
00354 CoinBigIndex *& start, int *& len,
00355 const int maxmajor = -1, const CoinBigIndex maxsize = -1);
00356
00357
00358
00361 CoinPackedMatrix & operator=(const CoinPackedMatrix& rhs);
00362
00364 void reverseOrdering();
00371 void transpose();
00372
00374 void swap(CoinPackedMatrix& matrix);
00375
00377
00378
00384 void times(const double * x, double * y) const;
00385 #ifndef CLP_NO_VECTOR
00386
00388 void times(const CoinPackedVectorBase& x, double * y) const;
00389 #endif
00390
00393 void transposeTimes(const double * x, double * y) const;
00394 #ifndef CLP_NO_VECTOR
00395
00397 void transposeTimes(const CoinPackedVectorBase& x, double * y) const;
00398 #endif
00399
00400
00401
00409
00410
00417 int * countOrthoLength() const;
00420 void countOrthoLength(int * counts) const;
00423 int getMajorDim() const { return majorDim_; }
00426 int getMinorDim() const { return minorDim_; }
00430 int getMaxMajorDim() const { return maxMajorDim_; }
00431
00434 void dumpMatrix(const char* fname = NULL) const;
00435
00437 void printMatrixElement(const int row_val, const int col_val) const;
00439
00440
00447 #ifndef CLP_NO_VECTOR
00448
00449 void appendMajorVector(const CoinPackedVectorBase& vec);
00450 #endif
00451
00452 void appendMajorVector(const int vecsize, const int *vecind,
00453 const double *vecelem);
00454 #ifndef CLP_NO_VECTOR
00455
00456 void appendMajorVectors(const int numvecs,
00457 const CoinPackedVectorBase * const * vecs);
00458
00460 void appendMinorVector(const CoinPackedVectorBase& vec);
00461 #endif
00462
00463 void appendMinorVector(const int vecsize, const int *vecind,
00464 const double *vecelem);
00465 #ifndef CLP_NO_VECTOR
00466
00467 void appendMinorVectors(const int numvecs,
00468 const CoinPackedVectorBase * const * vecs);
00469 #endif
00470
00471
00472
00485 void majorAppendSameOrdered(const CoinPackedMatrix& matrix);
00490 void minorAppendSameOrdered(const CoinPackedMatrix& matrix);
00496 void majorAppendOrthoOrdered(const CoinPackedMatrix& matrix);
00502 void minorAppendOrthoOrdered(const CoinPackedMatrix& matrix);
00504
00505
00510 void deleteMajorVectors(const int numDel, const int * indDel);
00513 void deleteMinorVectors(const int numDel, const int * indDel);
00515
00516
00523 void timesMajor(const double * x, double * y) const;
00524 #ifndef CLP_NO_VECTOR
00525
00528 void timesMajor(const CoinPackedVectorBase& x, double * y) const;
00529 #endif
00530
00534 void timesMinor(const double * x, double * y) const;
00535 #ifndef CLP_NO_VECTOR
00536
00539 void timesMinor(const CoinPackedVectorBase& x, double * y) const;
00540 #endif
00541
00542
00543
00544
00547 #ifndef CLP_NO_VECTOR
00548
00553 template <class FloatEqual> bool
00554 isEquivalent(const CoinPackedMatrix& rhs, const FloatEqual& eq) const
00555 {
00556
00557 if ((isColOrdered() ^ rhs.isColOrdered()) ||
00558 (getNumCols() != rhs.getNumCols()) ||
00559 (getNumRows() != rhs.getNumRows()) ||
00560 (getNumElements() != rhs.getNumElements()))
00561 return false;
00562
00563 for (int i=getMajorDim()-1; i >= 0; --i) {
00564 CoinShallowPackedVector pv = getVector(i);
00565 CoinShallowPackedVector rhsPv = rhs.getVector(i);
00566 if ( !pv.isEquivalent(rhsPv,eq) )
00567 return false;
00568 }
00569 return true;
00570 }
00571
00572 bool isEquivalent2(const CoinPackedMatrix& rhs) const;
00573 #else
00574
00579 bool isEquivalent(const CoinPackedMatrix& rhs, const CoinRelFltEq & eq) const;
00580 #endif
00581
00582 bool isEquivalent(const CoinPackedMatrix& rhs) const
00583 {
00584 return isEquivalent(rhs, CoinRelFltEq());
00585 }
00587
00588
00596 inline double * getMutableElements() const { return element_; }
00602 inline int * getMutableIndices() const { return index_; }
00603
00606 inline CoinBigIndex * getMutableVectorStarts() const { return start_; }
00608 inline int * getMutableVectorLengths() const { return length_; }
00610 inline void setNumElements(CoinBigIndex value)
00611 { size_ = value;}
00613 inline void nullElementArray() {element_=NULL;}
00615 inline void nullStartArray() {start_=NULL;}
00617 inline void nullLengthArray() {length_=NULL;}
00619 inline void nullIndexArray() {index_=NULL;}
00621
00622
00625
00626 CoinPackedMatrix();
00627
00629 CoinPackedMatrix(const bool colordered,
00630 const double extraMajor, const double extraGap);
00631
00632 CoinPackedMatrix(const bool colordered,
00633 const int minor, const int major, const CoinBigIndex numels,
00634 const double * elem, const int * ind,
00635 const CoinBigIndex * start, const int * len,
00636 const double extraMajor, const double extraGap);
00637
00638 CoinPackedMatrix(const bool colordered,
00639 const int minor, const int major, const CoinBigIndex numels,
00640 const double * elem, const int * ind,
00641 const CoinBigIndex * start, const int * len);
00642
00653 CoinPackedMatrix(const bool colordered,
00654 const int * rowIndices,
00655 const int * colIndices,
00656 const double * elements,
00657 CoinBigIndex numels );
00658
00660 CoinPackedMatrix(const CoinPackedMatrix& m);
00661
00666 CoinPackedMatrix(const CoinPackedMatrix& m, int extraForMajor, int extraElements, bool reverseOrdering=false);
00667
00670 CoinPackedMatrix (const CoinPackedMatrix & wholeModel,
00671 int numberRows, const int * whichRows,
00672 int numberColumns, const int * whichColumns);
00673
00675 virtual ~CoinPackedMatrix();
00677
00678
00679 protected:
00680 void gutsOfDestructor();
00681 void gutsOfCopyOf(const bool colordered,
00682 const int minor, const int major, const CoinBigIndex numels,
00683 const double * elem, const int * ind,
00684 const CoinBigIndex * start, const int * len,
00685 const double extraMajor=0.0, const double extraGap=0.0);
00687 void gutsOfCopyOfNoGaps(const bool colordered,
00688 const int minor, const int major,
00689 const double * elem, const int * ind,
00690 const CoinBigIndex * start);
00691 void gutsOfOpEqual(const bool colordered,
00692 const int minor, const int major, const CoinBigIndex numels,
00693 const double * elem, const int * ind,
00694 const CoinBigIndex * start, const int * len);
00695 void resizeForAddingMajorVectors(const int numVec, const int * lengthVec);
00696 void resizeForAddingMinorVectors(const int * addedEntries);
00701 int appendMajor(const int number,
00702 const CoinBigIndex * starts, const int * index,
00703 const double * element, int numberOther=-1);
00708 int appendMinor(const int number,
00709 const CoinBigIndex * starts, const int * index,
00710 const double * element, int numberOther=-1);
00711 public:
00715 void appendMinorFast(const int number,
00716 const CoinBigIndex * starts, const int * index,
00717 const double * element);
00718 private:
00719 inline CoinBigIndex getLastStart() const {
00720 return majorDim_ == 0 ? 0 : start_[majorDim_];
00721 }
00722
00723
00724 protected:
00729 bool colOrdered_;
00734 double extraGap_;
00738 double extraMajor_;
00739
00742 double *element_;
00745 int *index_;
00747 CoinBigIndex *start_;
00749 int *length_;
00750
00752 int majorDim_;
00754 int minorDim_;
00756 CoinBigIndex size_;
00757
00759 int maxMajorDim_;
00761 CoinBigIndex maxSize_;
00763 };
00764
00765
00771 void
00772 CoinPackedMatrixUnitTest();
00773
00774 #endif