00001
00002
00003 #ifndef CoinPackedMatrix_H
00004 #define CoinPackedMatrix_H
00005
00006 #include "CoinError.hpp"
00007 #ifndef CLP_NO_VECTOR
00008 #include "CoinPackedVectorBase.hpp"
00009 #include "CoinShallowPackedVector.hpp"
00010 #else
00011 #include "CoinFinite.hpp"
00012 #include "CoinFloatEqual.hpp"
00013 #endif
00014
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
00077 void reserve(const int newMaxMajorDim, const CoinBigIndex newMaxSize,
00078 bool create=false);
00080 void clear();
00081
00083 bool isColOrdered() const { return colOrdered_; }
00085 CoinBigIndex getNumElements() const { return size_; }
00087 int getNumCols() const { return colOrdered_ ? majorDim_ : minorDim_; }
00089 int getNumRows() const { return colOrdered_ ? minorDim_ : majorDim_; }
00090
00095 inline const double * getElements() const { return element_; }
00101 inline const int * getIndices() const { return index_; }
00102
00104 int getSizeVectorStarts()const { return majorDim_ > 0 ? majorDim_+1 : 0;}
00106 int getSizeVectorLengths() const { return majorDim_; }
00109 inline const CoinBigIndex * getVectorStarts() const { return start_; }
00111 inline const int * getVectorLengths() const { return length_; }
00112
00113
00116 CoinBigIndex getVectorFirst(const int i) const {
00117 if (i < 0 || i >= majorDim_)
00118 throw CoinError("bad index", "vectorFirst", "CoinPackedMatrix");
00119 return start_[i];
00120 }
00123 CoinBigIndex getVectorLast(const int i) const {
00124 if (i < 0 || i >= majorDim_)
00125 throw CoinError("bad index", "vectorLast", "CoinPackedMatrix");
00126 return start_[i] + length_[i];
00127 }
00129 inline int getVectorSize(const int i) const {
00130 if (i < 0 || i >= majorDim_)
00131 throw CoinError("bad index", "vectorSize", "CoinPackedMatrix");
00132 return length_[i];
00133 }
00134 #ifndef CLP_NO_VECTOR
00135
00136 const CoinShallowPackedVector getVector(int i) const {
00137 if (i < 0 || i >= majorDim_)
00138 throw CoinError("bad index", "vector", "CoinPackedMatrix");
00139 return CoinShallowPackedVector(length_[i],
00140 index_ + start_[i],
00141 element_ + start_[i],
00142 false);
00143 }
00144 #endif
00145
00155 int * getMajorIndices() const;
00157
00158
00166 void setDimensions(int numrows, int numcols);
00167
00169 void setExtraGap(const double newGap);
00171 void setExtraMajor(const double newMajor);
00172 #ifndef CLP_NO_VECTOR
00173
00177 void appendCol(const CoinPackedVectorBase& vec);
00178 #endif
00179
00183 void appendCol(const int vecsize,
00184 const int *vecind, const double *vecelem);
00185 #ifndef CLP_NO_VECTOR
00186
00191 void appendCols(const int numcols,
00192 const CoinPackedVectorBase * const * cols);
00193 #endif
00194
00197 int appendCols(const int numcols,
00198 const CoinBigIndex * columnStarts, const int * row,
00199 const double * element, int numberRows=-1);
00200 #ifndef CLP_NO_VECTOR
00201
00205 void appendRow(const CoinPackedVectorBase& vec);
00206 #endif
00207
00211 void appendRow(const int vecsize,
00212 const int *vecind, const double *vecelem);
00213 #ifndef CLP_NO_VECTOR
00214
00219 void appendRows(const int numrows,
00220 const CoinPackedVectorBase * const * rows);
00221 #endif
00222
00225 int appendRows(const int numrows,
00226 const CoinBigIndex * rowStarts, const int * column,
00227 const double * element, int numberColumns=-1);
00228
00233 void rightAppendPackedMatrix(const CoinPackedMatrix& matrix);
00238 void bottomAppendPackedMatrix(const CoinPackedMatrix& matrix);
00239
00241 void deleteCols(const int numDel, const int * indDel);
00243 void deleteRows(const int numDel, const int * indDel);
00244
00248 void replaceVector(const int index,
00249 const int numReplace, const double * newElements);
00254 void modifyCoefficient(int row, int column, double newElement,
00255 bool keepZero=false);
00259 double getCoefficient(int row, int column) const;
00260
00266 int compress(double threshold);
00271 int eliminateDuplicates(double threshold);
00273 void orderMatrix();
00275
00276
00280 void removeGaps();
00281
00285 void submatrixOf(const CoinPackedMatrix& matrix,
00286 const int numMajor, const int * indMajor);
00290 void submatrixOfWithDuplicates(const CoinPackedMatrix& matrix,
00291 const int numMajor, const int * indMajor);
00292 #if 0
00293
00296 void submatrixOf(const CoinPackedMatrix& matrix,
00297 const int numMajor, const int * indMajor,
00298 const int numMinor, const int * indMinor);
00299 #endif
00300
00303 void copyOf(const CoinPackedMatrix& rhs);
00307 void copyOf(const bool colordered,
00308 const int minor, const int major, const CoinBigIndex numels,
00309 const double * elem, const int * ind,
00310 const CoinBigIndex * start, const int * len,
00311 const double extraMajor=0.0, const double extraGap=0.0);
00315 void reverseOrderedCopyOf(const CoinPackedMatrix& rhs);
00324 void assignMatrix(const bool colordered,
00325 const int minor, const int major,
00326 const CoinBigIndex numels,
00327 double *& elem, int *& ind,
00328 CoinBigIndex *& start, int *& len,
00329 const int maxmajor = -1, const CoinBigIndex maxsize = -1);
00330
00331
00332
00335 CoinPackedMatrix & operator=(const CoinPackedMatrix& rhs);
00336
00338 void reverseOrdering();
00345 void transpose();
00346
00348 void swap(CoinPackedMatrix& matrix);
00349
00351
00352
00358 void times(const double * x, double * y) const;
00359 #ifndef CLP_NO_VECTOR
00360
00362 void times(const CoinPackedVectorBase& x, double * y) const;
00363 #endif
00364
00367 void transposeTimes(const double * x, double * y) const;
00368 #ifndef CLP_NO_VECTOR
00369
00371 void transposeTimes(const CoinPackedVectorBase& x, double * y) const;
00372 #endif
00373
00374
00375
00383
00384
00391 int * countOrthoLength() const;
00394 int getMajorDim() const { return majorDim_; }
00397 int getMinorDim() const { return minorDim_; }
00401 int getMaxMajorDim() const { return maxMajorDim_; }
00402
00405 void dumpMatrix(const char* fname = NULL) const;
00406
00408 void printMatrixElement(const int row_val, const int col_val) const;
00410
00411
00418 #ifndef CLP_NO_VECTOR
00419
00420 void appendMajorVector(const CoinPackedVectorBase& vec);
00421 #endif
00422
00423 void appendMajorVector(const int vecsize, const int *vecind,
00424 const double *vecelem);
00425 #ifndef CLP_NO_VECTOR
00426
00427 void appendMajorVectors(const int numvecs,
00428 const CoinPackedVectorBase * const * vecs);
00429
00431 void appendMinorVector(const CoinPackedVectorBase& vec);
00432 #endif
00433
00434 void appendMinorVector(const int vecsize, const int *vecind,
00435 const double *vecelem);
00436 #ifndef CLP_NO_VECTOR
00437
00438 void appendMinorVectors(const int numvecs,
00439 const CoinPackedVectorBase * const * vecs);
00440 #endif
00441
00442
00443
00456 void majorAppendSameOrdered(const CoinPackedMatrix& matrix);
00461 void minorAppendSameOrdered(const CoinPackedMatrix& matrix);
00467 void majorAppendOrthoOrdered(const CoinPackedMatrix& matrix);
00473 void minorAppendOrthoOrdered(const CoinPackedMatrix& matrix);
00475
00476
00481 void deleteMajorVectors(const int numDel, const int * indDel);
00484 void deleteMinorVectors(const int numDel, const int * indDel);
00486
00487
00494 void timesMajor(const double * x, double * y) const;
00495 #ifndef CLP_NO_VECTOR
00496
00499 void timesMajor(const CoinPackedVectorBase& x, double * y) const;
00500 #endif
00501
00505 void timesMinor(const double * x, double * y) const;
00506 #ifndef CLP_NO_VECTOR
00507
00510 void timesMinor(const CoinPackedVectorBase& x, double * y) const;
00511 #endif
00512
00513
00514
00515
00518 #ifndef CLP_NO_VECTOR
00519
00524 template <class FloatEqual> bool
00525 isEquivalent(const CoinPackedMatrix& rhs, const FloatEqual& eq) const
00526 {
00527
00528 if ((isColOrdered() ^ rhs.isColOrdered()) ||
00529 (getNumCols() != rhs.getNumCols()) ||
00530 (getNumRows() != rhs.getNumRows()) ||
00531 (getNumElements() != rhs.getNumElements()))
00532 return false;
00533
00534 for (int i=getMajorDim()-1; i >= 0; --i) {
00535 CoinShallowPackedVector pv = getVector(i);
00536 CoinShallowPackedVector rhsPv = rhs.getVector(i);
00537 if ( !pv.isEquivalent(rhsPv,eq) )
00538 return false;
00539 }
00540 return true;
00541 }
00542
00543 bool isEquivalent2(const CoinPackedMatrix& rhs) const;
00544 #else
00545
00550 bool isEquivalent(const CoinPackedMatrix& rhs, const CoinRelFltEq & eq) const;
00551 #endif
00553 bool isEquivalent(const CoinPackedMatrix& rhs) const
00554 {
00555 return isEquivalent(rhs, CoinRelFltEq());
00556 }
00558
00559
00567 inline double * getMutableElements() const { return element_; }
00573 inline int * getMutableIndices() const { return index_; }
00574
00577 inline CoinBigIndex * getMutableVectorStarts() const { return start_; }
00579 inline int * getMutableVectorLengths() const { return length_; }
00581 inline void nullElementArray() {element_=NULL;};
00583 inline void nullStartArray() {start_=NULL;};
00585 inline void nullLengthArray() {length_=NULL;};
00587 inline void nullIndexArray() {index_=NULL;};
00589
00590
00593
00594 CoinPackedMatrix();
00595
00597 CoinPackedMatrix(const bool colordered,
00598 const double extraMajor, const double extraGap);
00599
00600 CoinPackedMatrix(const bool colordered,
00601 const int minor, const int major, const CoinBigIndex numels,
00602 const double * elem, const int * ind,
00603 const CoinBigIndex * start, const int * len,
00604 const double extraMajor, const double extraGap);
00605
00606 CoinPackedMatrix(const bool colordered,
00607 const int minor, const int major, const CoinBigIndex numels,
00608 const double * elem, const int * ind,
00609 const CoinBigIndex * start, const int * len);
00610
00621 CoinPackedMatrix(const bool colordered,
00622 const int * rowIndices,
00623 const int * colIndices,
00624 const double * elements,
00625 CoinBigIndex numels );
00626
00628 CoinPackedMatrix(const CoinPackedMatrix& m);
00629
00632 CoinPackedMatrix (const CoinPackedMatrix & wholeModel,
00633 int numberRows, const int * whichRows,
00634 int numberColumns, const int * whichColumns);
00635
00637 virtual ~CoinPackedMatrix();
00639
00640
00641 protected:
00642 void gutsOfDestructor();
00643 void gutsOfCopyOf(const bool colordered,
00644 const int minor, const int major, const CoinBigIndex numels,
00645 const double * elem, const int * ind,
00646 const CoinBigIndex * start, const int * len,
00647 const double extraMajor=0.0, const double extraGap=0.0);
00649 void gutsOfCopyOfNoGaps(const bool colordered,
00650 const int minor, const int major,
00651 const double * elem, const int * ind,
00652 const CoinBigIndex * start);
00653 void gutsOfOpEqual(const bool colordered,
00654 const int minor, const int major, const CoinBigIndex numels,
00655 const double * elem, const int * ind,
00656 const CoinBigIndex * start, const int * len);
00657 void resizeForAddingMajorVectors(const int numVec, const int * lengthVec);
00658 void resizeForAddingMinorVectors(const int * addedEntries);
00663 int appendMajor(const int number,
00664 const CoinBigIndex * starts, const int * index,
00665 const double * element, int numberOther=-1);
00670 int appendMinor(const int number,
00671 const CoinBigIndex * starts, const int * index,
00672 const double * element, int numberOther=-1);
00673 private:
00674 inline CoinBigIndex getLastStart() const {
00675 return majorDim_ == 0 ? 0 : start_[majorDim_];
00676 }
00677
00678
00679 protected:
00684 bool colOrdered_;
00689 double extraGap_;
00693 double extraMajor_;
00694
00697 double *element_;
00700 int *index_;
00702 CoinBigIndex *start_;
00704 int *length_;
00705
00707 int majorDim_;
00709 int minorDim_;
00711 CoinBigIndex size_;
00712
00714 int maxMajorDim_;
00716 CoinBigIndex maxSize_;
00718 };
00719
00720
00726 void
00727 CoinPackedMatrixUnitTest();
00728
00729 #endif