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 inline double getExtraGap() const { return extraGap_; }
00073 inline double getExtraMajor() const { return extraMajor_; }
00074
00077 void reserve(const int newMaxMajorDim, const CoinBigIndex newMaxSize,
00078 bool create=false);
00080 void clear();
00081
00083 inline bool isColOrdered() const { return colOrdered_; }
00085 inline bool hasGaps() const
00086 { return size_<start_[majorDim_];}
00087
00089 inline CoinBigIndex getNumElements() const { return size_; }
00091 inline int getNumCols() const { return colOrdered_ ? majorDim_ : minorDim_; }
00093 inline int getNumRows() const { return colOrdered_ ? minorDim_ : majorDim_; }
00094
00099 inline const double * getElements() const { return element_; }
00105 inline const int * getIndices() const { return index_; }
00106
00108 inline int getSizeVectorStarts()const { return majorDim_ > 0 ? majorDim_+1 : 0;}
00110 inline int getSizeVectorLengths() const { return majorDim_; }
00113 inline const CoinBigIndex * getVectorStarts() const { return start_; }
00115 inline const int * getVectorLengths() const { return length_; }
00116
00117
00120 CoinBigIndex getVectorFirst(const int i) const {
00121 #ifndef COIN_FAST_CODE
00122 if (i < 0 || i >= majorDim_)
00123 throw CoinError("bad index", "vectorFirst", "CoinPackedMatrix");
00124 #endif
00125 return start_[i];
00126 }
00129 CoinBigIndex getVectorLast(const int i) const {
00130 #ifndef COIN_FAST_CODE
00131 if (i < 0 || i >= majorDim_)
00132 throw CoinError("bad index", "vectorLast", "CoinPackedMatrix");
00133 #endif
00134 return start_[i] + length_[i];
00135 }
00137 inline int getVectorSize(const int i) const {
00138 #ifndef COIN_FAST_CODE
00139 if (i < 0 || i >= majorDim_)
00140 throw CoinError("bad index", "vectorSize", "CoinPackedMatrix");
00141 #endif
00142 return length_[i];
00143 }
00144 #ifndef CLP_NO_VECTOR
00145
00146 const CoinShallowPackedVector getVector(int i) const {
00147 #ifndef COIN_FAST_CODE
00148 if (i < 0 || i >= majorDim_)
00149 throw CoinError("bad index", "vector", "CoinPackedMatrix");
00150 #endif
00151 return CoinShallowPackedVector(length_[i],
00152 index_ + start_[i],
00153 element_ + start_[i],
00154 false);
00155 }
00156 #endif
00157
00167 int * getMajorIndices() const;
00169
00170
00178 void setDimensions(int numrows, int numcols);
00179
00181 void setExtraGap(const double newGap);
00183 void setExtraMajor(const double newMajor);
00184 #ifndef CLP_NO_VECTOR
00185
00189 void appendCol(const CoinPackedVectorBase& vec);
00190 #endif
00191
00195 void appendCol(const int vecsize,
00196 const int *vecind, const double *vecelem);
00197 #ifndef CLP_NO_VECTOR
00198
00203 void appendCols(const int numcols,
00204 const CoinPackedVectorBase * const * cols);
00205 #endif
00206
00209 int appendCols(const int numcols,
00210 const CoinBigIndex * columnStarts, const int * row,
00211 const double * element, int numberRows=-1);
00212 #ifndef CLP_NO_VECTOR
00213
00217 void appendRow(const CoinPackedVectorBase& vec);
00218 #endif
00219
00223 void appendRow(const int vecsize,
00224 const int *vecind, const double *vecelem);
00225 #ifndef CLP_NO_VECTOR
00226
00231 void appendRows(const int numrows,
00232 const CoinPackedVectorBase * const * rows);
00233 #endif
00234
00237 int appendRows(const int numrows,
00238 const CoinBigIndex * rowStarts, const int * column,
00239 const double * element, int numberColumns=-1);
00240
00245 void rightAppendPackedMatrix(const CoinPackedMatrix& matrix);
00250 void bottomAppendPackedMatrix(const CoinPackedMatrix& matrix);
00251
00253 void deleteCols(const int numDel, const int * indDel);
00255 void deleteRows(const int numDel, const int * indDel);
00256
00260 void replaceVector(const int index,
00261 const int numReplace, const double * newElements);
00266 void modifyCoefficient(int row, int column, double newElement,
00267 bool keepZero=false);
00271 double getCoefficient(int row, int column) const;
00272
00278 int compress(double threshold);
00283 int eliminateDuplicates(double threshold);
00285 void orderMatrix();
00293 int cleanMatrix(double threshold=1.0e-20);
00295
00296
00301 void removeGaps(double removeValue=-1.0);
00302
00306 void submatrixOf(const CoinPackedMatrix& matrix,
00307 const int numMajor, const int * indMajor);
00311 void submatrixOfWithDuplicates(const CoinPackedMatrix& matrix,
00312 const int numMajor, const int * indMajor);
00313 #if 0
00314
00317 void submatrixOf(const CoinPackedMatrix& matrix,
00318 const int numMajor, const int * indMajor,
00319 const int numMinor, const int * indMinor);
00320 #endif
00321
00324 void copyOf(const CoinPackedMatrix& rhs);
00328 void copyOf(const bool colordered,
00329 const int minor, const int major, const CoinBigIndex numels,
00330 const double * elem, const int * ind,
00331 const CoinBigIndex * start, const int * len,
00332 const double extraMajor=0.0, const double extraGap=0.0);
00336 void copyReuseArrays(const CoinPackedMatrix& rhs);
00340 void reverseOrderedCopyOf(const CoinPackedMatrix& rhs);
00349 void assignMatrix(const bool colordered,
00350 const int minor, const int major,
00351 const CoinBigIndex numels,
00352 double *& elem, int *& ind,
00353 CoinBigIndex *& start, int *& len,
00354 const int maxmajor = -1, const CoinBigIndex maxsize = -1);
00355
00356
00357
00360 CoinPackedMatrix & operator=(const CoinPackedMatrix& rhs);
00361
00363 void reverseOrdering();
00370 void transpose();
00371
00373 void swap(CoinPackedMatrix& matrix);
00374
00376
00377
00383 void times(const double * x, double * y) const;
00384 #ifndef CLP_NO_VECTOR
00385
00387 void times(const CoinPackedVectorBase& x, double * y) const;
00388 #endif
00389
00392 void transposeTimes(const double * x, double * y) const;
00393 #ifndef CLP_NO_VECTOR
00394
00396 void transposeTimes(const CoinPackedVectorBase& x, double * y) const;
00397 #endif
00398
00399
00400
00408
00409
00416 int * countOrthoLength() const;
00419 void countOrthoLength(int * counts) const;
00422 int getMajorDim() const { return majorDim_; }
00425 int getMinorDim() const { return minorDim_; }
00429 int getMaxMajorDim() const { return maxMajorDim_; }
00430
00433 void dumpMatrix(const char* fname = NULL) const;
00434
00436 void printMatrixElement(const int row_val, const int col_val) const;
00438
00439
00446 #ifndef CLP_NO_VECTOR
00447
00448 void appendMajorVector(const CoinPackedVectorBase& vec);
00449 #endif
00450
00451 void appendMajorVector(const int vecsize, const int *vecind,
00452 const double *vecelem);
00453 #ifndef CLP_NO_VECTOR
00454
00455 void appendMajorVectors(const int numvecs,
00456 const CoinPackedVectorBase * const * vecs);
00457
00459 void appendMinorVector(const CoinPackedVectorBase& vec);
00460 #endif
00461
00462 void appendMinorVector(const int vecsize, const int *vecind,
00463 const double *vecelem);
00464 #ifndef CLP_NO_VECTOR
00465
00466 void appendMinorVectors(const int numvecs,
00467 const CoinPackedVectorBase * const * vecs);
00468 #endif
00469
00470
00471
00484 void majorAppendSameOrdered(const CoinPackedMatrix& matrix);
00489 void minorAppendSameOrdered(const CoinPackedMatrix& matrix);
00495 void majorAppendOrthoOrdered(const CoinPackedMatrix& matrix);
00501 void minorAppendOrthoOrdered(const CoinPackedMatrix& matrix);
00503
00504
00509 void deleteMajorVectors(const int numDel, const int * indDel);
00512 void deleteMinorVectors(const int numDel, const int * indDel);
00514
00515
00522 void timesMajor(const double * x, double * y) const;
00523 #ifndef CLP_NO_VECTOR
00524
00527 void timesMajor(const CoinPackedVectorBase& x, double * y) const;
00528 #endif
00529
00533 void timesMinor(const double * x, double * y) const;
00534 #ifndef CLP_NO_VECTOR
00535
00538 void timesMinor(const CoinPackedVectorBase& x, double * y) const;
00539 #endif
00540
00541
00542
00543
00546 #ifndef CLP_NO_VECTOR
00547
00552 template <class FloatEqual> bool
00553 isEquivalent(const CoinPackedMatrix& rhs, const FloatEqual& eq) const
00554 {
00555
00556 if ((isColOrdered() ^ rhs.isColOrdered()) ||
00557 (getNumCols() != rhs.getNumCols()) ||
00558 (getNumRows() != rhs.getNumRows()) ||
00559 (getNumElements() != rhs.getNumElements()))
00560 return false;
00561
00562 for (int i=getMajorDim()-1; i >= 0; --i) {
00563 CoinShallowPackedVector pv = getVector(i);
00564 CoinShallowPackedVector rhsPv = rhs.getVector(i);
00565 if ( !pv.isEquivalent(rhsPv,eq) )
00566 return false;
00567 }
00568 return true;
00569 }
00570
00571 bool isEquivalent2(const CoinPackedMatrix& rhs) const;
00572 #else
00573
00578 bool isEquivalent(const CoinPackedMatrix& rhs, const CoinRelFltEq & eq) const;
00579 #endif
00581 bool isEquivalent(const CoinPackedMatrix& rhs) const
00582 {
00583 return isEquivalent(rhs, CoinRelFltEq());
00584 }
00586
00587
00595 inline double * getMutableElements() const { return element_; }
00601 inline int * getMutableIndices() const { return index_; }
00602
00605 inline CoinBigIndex * getMutableVectorStarts() const { return start_; }
00607 inline int * getMutableVectorLengths() const { return length_; }
00609 inline void setNumElements(CoinBigIndex value)
00610 { size_ = value;}
00612 inline void nullElementArray() {element_=NULL;}
00614 inline void nullStartArray() {start_=NULL;}
00616 inline void nullLengthArray() {length_=NULL;}
00618 inline void nullIndexArray() {index_=NULL;}
00620
00621
00624
00625 CoinPackedMatrix();
00626
00628 CoinPackedMatrix(const bool colordered,
00629 const double extraMajor, const double extraGap);
00630
00631 CoinPackedMatrix(const bool colordered,
00632 const int minor, const int major, const CoinBigIndex numels,
00633 const double * elem, const int * ind,
00634 const CoinBigIndex * start, const int * len,
00635 const double extraMajor, const double extraGap);
00636
00637 CoinPackedMatrix(const bool colordered,
00638 const int minor, const int major, const CoinBigIndex numels,
00639 const double * elem, const int * ind,
00640 const CoinBigIndex * start, const int * len);
00641
00652 CoinPackedMatrix(const bool colordered,
00653 const int * rowIndices,
00654 const int * colIndices,
00655 const double * elements,
00656 CoinBigIndex numels );
00657
00659 CoinPackedMatrix(const CoinPackedMatrix& m);
00660
00665 CoinPackedMatrix(const CoinPackedMatrix& m, int extraForMajor, int extraElements, bool reverseOrdering=false);
00666
00669 CoinPackedMatrix (const CoinPackedMatrix & wholeModel,
00670 int numberRows, const int * whichRows,
00671 int numberColumns, const int * whichColumns);
00672
00674 virtual ~CoinPackedMatrix();
00676
00677
00678 protected:
00679 void gutsOfDestructor();
00680 void gutsOfCopyOf(const bool colordered,
00681 const int minor, const int major, const CoinBigIndex numels,
00682 const double * elem, const int * ind,
00683 const CoinBigIndex * start, const int * len,
00684 const double extraMajor=0.0, const double extraGap=0.0);
00686 void gutsOfCopyOfNoGaps(const bool colordered,
00687 const int minor, const int major,
00688 const double * elem, const int * ind,
00689 const CoinBigIndex * start);
00690 void gutsOfOpEqual(const bool colordered,
00691 const int minor, const int major, const CoinBigIndex numels,
00692 const double * elem, const int * ind,
00693 const CoinBigIndex * start, const int * len);
00694 void resizeForAddingMajorVectors(const int numVec, const int * lengthVec);
00695 void resizeForAddingMinorVectors(const int * addedEntries);
00700 int appendMajor(const int number,
00701 const CoinBigIndex * starts, const int * index,
00702 const double * element, int numberOther=-1);
00707 int appendMinor(const int number,
00708 const CoinBigIndex * starts, const int * index,
00709 const double * element, int numberOther=-1);
00710 private:
00711 inline CoinBigIndex getLastStart() const {
00712 return majorDim_ == 0 ? 0 : start_[majorDim_];
00713 }
00714
00715
00716 protected:
00721 bool colOrdered_;
00726 double extraGap_;
00730 double extraMajor_;
00731
00734 double *element_;
00737 int *index_;
00739 CoinBigIndex *start_;
00741 int *length_;
00742
00744 int majorDim_;
00746 int minorDim_;
00748 CoinBigIndex size_;
00749
00751 int maxMajorDim_;
00753 CoinBigIndex maxSize_;
00755 };
00756
00757
00763 void
00764 CoinPackedMatrixUnitTest();
00765
00766 #endif