CoinPackedMatrix.hpp
Go to the documentation of this file.
1 /* $Id: CoinPackedMatrix.hpp 1215 2009-11-05 11:03:04Z forrest $ */
2 // Copyright (C) 2000, International Business Machines
3 // Corporation and others. All Rights Reserved.
4 #ifndef CoinPackedMatrix_H
5 #define CoinPackedMatrix_H
6 
7 #include "CoinError.hpp"
8 #ifndef CLP_NO_VECTOR
11 #else
12 #include "CoinFinite.hpp"
13 #include "CoinFloatEqual.hpp"
14 #endif
15 
63  friend void CoinPackedMatrixUnitTest();
64 
65 public:
66 
67 
68  //---------------------------------------------------------------------------
72  inline double getExtraGap() const { return extraGap_; }
74  inline double getExtraMajor() const { return extraMajor_; }
75 
78  void reserve(const int newMaxMajorDim, const CoinBigIndex newMaxSize,
79  bool create=false);
81  void clear();
82 
84  inline bool isColOrdered() const { return colOrdered_; }
86  inline bool hasGaps() const
87  { return size_<start_[majorDim_];}
88 
90  inline CoinBigIndex getNumElements() const { return size_; }
92  inline int getNumCols() const { return colOrdered_ ? majorDim_ : minorDim_; }
94  inline int getNumRows() const { return colOrdered_ ? minorDim_ : majorDim_; }
95 
100  inline const double * getElements() const { return element_; }
106  inline const int * getIndices() const { return index_; }
107 
109  inline int getSizeVectorStarts()const { return majorDim_ > 0 ? majorDim_+1 : 0;}
111  inline int getSizeVectorLengths() const { return majorDim_; }
114  inline const CoinBigIndex * getVectorStarts() const { return start_; }
116  inline const int * getVectorLengths() const { return length_; }
117 
118 
121  CoinBigIndex getVectorFirst(const int i) const {
122 #ifndef COIN_FAST_CODE
123  if (i < 0 || i >= majorDim_)
124  throw CoinError("bad index", "vectorFirst", "CoinPackedMatrix");
125 #endif
126  return start_[i];
127  }
130  CoinBigIndex getVectorLast(const int i) const {
131 #ifndef COIN_FAST_CODE
132  if (i < 0 || i >= majorDim_)
133  throw CoinError("bad index", "vectorLast", "CoinPackedMatrix");
134 #endif
135  return start_[i] + length_[i];
136  }
138  inline int getVectorSize(const int i) const {
139 #ifndef COIN_FAST_CODE
140  if (i < 0 || i >= majorDim_)
141  throw CoinError("bad index", "vectorSize", "CoinPackedMatrix");
142 #endif
143  return length_[i];
144  }
145 #ifndef CLP_NO_VECTOR
146 
147  const CoinShallowPackedVector getVector(int i) const {
148 #ifndef COIN_FAST_CODE
149  if (i < 0 || i >= majorDim_)
150  throw CoinError("bad index", "vector", "CoinPackedMatrix");
151 #endif
153  index_ + start_[i],
154  element_ + start_[i],
155  false);
156  }
157 #endif
158 
168  int * getMajorIndices() const;
170 
171  //---------------------------------------------------------------------------
179  void setDimensions(int numrows, int numcols);
180 
182  void setExtraGap(const double newGap);
184  void setExtraMajor(const double newMajor);
185 #ifndef CLP_NO_VECTOR
186 
190  void appendCol(const CoinPackedVectorBase& vec);
191 #endif
192 
196  void appendCol(const int vecsize,
197  const int *vecind, const double *vecelem);
198 #ifndef CLP_NO_VECTOR
199 
204  void appendCols(const int numcols,
205  const CoinPackedVectorBase * const * cols);
206 #endif
207 
210  int appendCols(const int numcols,
211  const CoinBigIndex * columnStarts, const int * row,
212  const double * element, int numberRows=-1);
213 #ifndef CLP_NO_VECTOR
214 
218  void appendRow(const CoinPackedVectorBase& vec);
219 #endif
220 
224  void appendRow(const int vecsize,
225  const int *vecind, const double *vecelem);
226 #ifndef CLP_NO_VECTOR
227 
232  void appendRows(const int numrows,
233  const CoinPackedVectorBase * const * rows);
234 #endif
235 
238  int appendRows(const int numrows,
239  const CoinBigIndex * rowStarts, const int * column,
240  const double * element, int numberColumns=-1);
241 
246  void rightAppendPackedMatrix(const CoinPackedMatrix& matrix);
251  void bottomAppendPackedMatrix(const CoinPackedMatrix& matrix);
252 
254  void deleteCols(const int numDel, const int * indDel);
256  void deleteRows(const int numDel, const int * indDel);
257 
261  void replaceVector(const int index,
262  const int numReplace, const double * newElements);
267  void modifyCoefficient(int row, int column, double newElement,
268  bool keepZero=false);
272  double getCoefficient(int row, int column) const;
273 
279  int compress(double threshold);
284  int eliminateDuplicates(double threshold);
286  void orderMatrix();
294  int cleanMatrix(double threshold=1.0e-20);
296 
297  //---------------------------------------------------------------------------
302  void removeGaps(double removeValue=-1.0);
303 
307  void submatrixOf(const CoinPackedMatrix& matrix,
308  const int numMajor, const int * indMajor);
312  void submatrixOfWithDuplicates(const CoinPackedMatrix& matrix,
313  const int numMajor, const int * indMajor);
314 #if 0
315 
318  void submatrixOf(const CoinPackedMatrix& matrix,
319  const int numMajor, const int * indMajor,
320  const int numMinor, const int * indMinor);
321 #endif
322 
325  void copyOf(const CoinPackedMatrix& rhs);
329  void copyOf(const bool colordered,
330  const int minor, const int major, const CoinBigIndex numels,
331  const double * elem, const int * ind,
332  const CoinBigIndex * start, const int * len,
333  const double extraMajor=0.0, const double extraGap=0.0);
337  void copyReuseArrays(const CoinPackedMatrix& rhs);
341  void reverseOrderedCopyOf(const CoinPackedMatrix& rhs);
350  void assignMatrix(const bool colordered,
351  const int minor, const int major,
352  const CoinBigIndex numels,
353  double *& elem, int *& ind,
354  CoinBigIndex *& start, int *& len,
355  const int maxmajor = -1, const CoinBigIndex maxsize = -1);
356 
357 
358 
362 
364  void reverseOrdering();
371  void transpose();
372 
374  void swap(CoinPackedMatrix& matrix);
375 
377 
378  //---------------------------------------------------------------------------
384  void times(const double * x, double * y) const;
385 #ifndef CLP_NO_VECTOR
386 
388  void times(const CoinPackedVectorBase& x, double * y) const;
389 #endif
390 
393  void transposeTimes(const double * x, double * y) const;
394 #ifndef CLP_NO_VECTOR
395 
397  void transposeTimes(const CoinPackedVectorBase& x, double * y) const;
398 #endif
399 
400 
401  //---------------------------------------------------------------------------
409 
410  //-------------------------------------------------------------------------
417  int * countOrthoLength() const;
420  void countOrthoLength(int * counts) const;
423  int getMajorDim() const { return majorDim_; }
426  int getMinorDim() const { return minorDim_; }
430  int getMaxMajorDim() const { return maxMajorDim_; }
431 
434  void dumpMatrix(const char* fname = NULL) const;
435 
437  void printMatrixElement(const int row_val, const int col_val) const;
439 
440  //-------------------------------------------------------------------------
447 #ifndef CLP_NO_VECTOR
448 
449  void appendMajorVector(const CoinPackedVectorBase& vec);
450 #endif
451 
452  void appendMajorVector(const int vecsize, const int *vecind,
453  const double *vecelem);
454 #ifndef CLP_NO_VECTOR
455 
456  void appendMajorVectors(const int numvecs,
457  const CoinPackedVectorBase * const * vecs);
458 
460  void appendMinorVector(const CoinPackedVectorBase& vec);
461 #endif
462 
463  void appendMinorVector(const int vecsize, const int *vecind,
464  const double *vecelem);
465 #ifndef CLP_NO_VECTOR
466 
467  void appendMinorVectors(const int numvecs,
468  const CoinPackedVectorBase * const * vecs);
469 #endif
470 
471 
472  //-------------------------------------------------------------------------
485  void majorAppendSameOrdered(const CoinPackedMatrix& matrix);
490  void minorAppendSameOrdered(const CoinPackedMatrix& matrix);
496  void majorAppendOrthoOrdered(const CoinPackedMatrix& matrix);
502  void minorAppendOrthoOrdered(const CoinPackedMatrix& matrix);
504 
505  //-----------------------------------------------------------------------
510  void deleteMajorVectors(const int numDel, const int * indDel);
513  void deleteMinorVectors(const int numDel, const int * indDel);
515 
516  //-----------------------------------------------------------------------
523  void timesMajor(const double * x, double * y) const;
524 #ifndef CLP_NO_VECTOR
525 
528  void timesMajor(const CoinPackedVectorBase& x, double * y) const;
529 #endif
530 
534  void timesMinor(const double * x, double * y) const;
535 #ifndef CLP_NO_VECTOR
536 
539  void timesMinor(const CoinPackedVectorBase& x, double * y) const;
540 #endif
541 
542 
543 
544  //--------------------------------------------------------------------------
547 #ifndef CLP_NO_VECTOR
548 
553  template <class FloatEqual> bool
554  isEquivalent(const CoinPackedMatrix& rhs, const FloatEqual& eq) const
555  {
556  // Both must be column order or both row ordered and must be of same size
557  if ((isColOrdered() ^ rhs.isColOrdered()) ||
558  (getNumCols() != rhs.getNumCols()) ||
559  (getNumRows() != rhs.getNumRows()) ||
560  (getNumElements() != rhs.getNumElements()))
561  return false;
562 
563  for (int i=getMajorDim()-1; i >= 0; --i) {
565  CoinShallowPackedVector rhsPv = rhs.getVector(i);
566  if ( !pv.isEquivalent(rhsPv,eq) )
567  return false;
568  }
569  return true;
570  }
571 
572  bool isEquivalent2(const CoinPackedMatrix& rhs) const;
573 #else
574 
579  bool isEquivalent(const CoinPackedMatrix& rhs, const CoinRelFltEq & eq) const;
580 #endif
581  bool isEquivalent(const CoinPackedMatrix& rhs) const
583  {
584  return isEquivalent(rhs, CoinRelFltEq());
585  }
587 
588  //--------------------------------------------------------------------------
596  inline double * getMutableElements() const { return element_; }
602  inline int * getMutableIndices() const { return index_; }
603 
606  inline CoinBigIndex * getMutableVectorStarts() const { return start_; }
608  inline int * getMutableVectorLengths() const { return length_; }
610  inline void setNumElements(CoinBigIndex value)
611  { size_ = value;}
613  inline void nullElementArray() {element_=NULL;}
615  inline void nullStartArray() {start_=NULL;}
617  inline void nullLengthArray() {length_=NULL;}
619  inline void nullIndexArray() {index_=NULL;}
621 
622  //--------------------------------------------------------------------------
627 
629  CoinPackedMatrix(const bool colordered,
630  const double extraMajor, const double extraGap);
631 
632  CoinPackedMatrix(const bool colordered,
633  const int minor, const int major, const CoinBigIndex numels,
634  const double * elem, const int * ind,
635  const CoinBigIndex * start, const int * len,
636  const double extraMajor, const double extraGap);
637 
638  CoinPackedMatrix(const bool colordered,
639  const int minor, const int major, const CoinBigIndex numels,
640  const double * elem, const int * ind,
641  const CoinBigIndex * start, const int * len);
642 
653  CoinPackedMatrix(const bool colordered,
654  const int * rowIndices,
655  const int * colIndices,
656  const double * elements,
657  CoinBigIndex numels );
658 
661 
666  CoinPackedMatrix(const CoinPackedMatrix& m, int extraForMajor, int extraElements, bool reverseOrdering=false);
667 
670  CoinPackedMatrix (const CoinPackedMatrix & wholeModel,
671  int numberRows, const int * whichRows,
672  int numberColumns, const int * whichColumns);
673 
675  virtual ~CoinPackedMatrix();
677 
678  //--------------------------------------------------------------------------
679 protected:
680  void gutsOfDestructor();
681  void gutsOfCopyOf(const bool colordered,
682  const int minor, const int major, const CoinBigIndex numels,
683  const double * elem, const int * ind,
684  const CoinBigIndex * start, const int * len,
685  const double extraMajor=0.0, const double extraGap=0.0);
687  void gutsOfCopyOfNoGaps(const bool colordered,
688  const int minor, const int major,
689  const double * elem, const int * ind,
690  const CoinBigIndex * start);
691  void gutsOfOpEqual(const bool colordered,
692  const int minor, const int major, const CoinBigIndex numels,
693  const double * elem, const int * ind,
694  const CoinBigIndex * start, const int * len);
695  void resizeForAddingMajorVectors(const int numVec, const int * lengthVec);
696  void resizeForAddingMinorVectors(const int * addedEntries);
701  int appendMajor(const int number,
702  const CoinBigIndex * starts, const int * index,
703  const double * element, int numberOther=-1);
708  int appendMinor(const int number,
709  const CoinBigIndex * starts, const int * index,
710  const double * element, int numberOther=-1);
711 public:
715  void appendMinorFast(const int number,
716  const CoinBigIndex * starts, const int * index,
717  const double * element);
718 private:
719  inline CoinBigIndex getLastStart() const {
720  return majorDim_ == 0 ? 0 : start_[majorDim_];
721  }
722 
723  //--------------------------------------------------------------------------
724 protected:
734  double extraGap_;
738  double extraMajor_;
739 
742  double *element_;
745  int *index_;
749  int *length_;
750 
757 
763 };
764 
765 //#############################################################################
771 void
773 
774 #endif