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
int CoinBigIndex
void appendMinorVectors(const int numvecs, const CoinPackedVectorBase *const *vecs)
Append several minor-dimensonvectors to the end of the matrix.
void clear()
Clear the data, but do not free any arrays.
bool isEquivalent(const CoinPackedVectorBase &rhs, const FloatEqual &eq) const
equivalent - If shallow packed vector A & B are equivalent, then they are still equivalent no matter ...
void reserve(const int newMaxMajorDim, const CoinBigIndex newMaxSize, bool create=false)
Reserve sufficient space for appending major-ordered vectors.
void deleteMajorVectors(const int numDel, const int *indDel)
Delete the major-dimension vectors whose indices are listed in indDel.
CoinBigIndex size_
the number of nonzero entries
CoinBigIndex getVectorFirst(const int i) const
The position of the first element in the i'th major-dimension vector.
void resizeForAddingMajorVectors(const int numVec, const int *lengthVec)
void resizeForAddingMinorVectors(const int *addedEntries)
int appendMajor(const int number, const CoinBigIndex *starts, const int *index, const double *element, int numberOther=-1)
Append a set of rows/columns to the end of the matrix.
void setExtraMajor(const double newMajor)
Set the extra major to be allocated to the specified value.
Equality to a scaled tolerance.
void swap(CoinPackedMatrix &matrix)
Swap the content of the two packed matrix.
void appendMinorVector(const CoinPackedVectorBase &vec)
Append a minor-dimension vector to the end of the matrix.
void rightAppendPackedMatrix(const CoinPackedMatrix &matrix)
Append the argument to the "right" of the current matrix.
void nullIndexArray()
NULLify index array - used when space is very tight.
void timesMajor(const double *x, double *y) const
Return A * x (multiplied from the "right" direction) in y.
void appendMinorFast(const int number, const CoinBigIndex *starts, const int *index, const double *element)
Append a set of rows/columns to the end of the matrix.
void minorAppendOrthoOrdered(const CoinPackedMatrix &matrix)
Append the rows of the argument to the bottom end of this matrix.
double extraGap_
This much times more space should be allocated for each major-dimension vector (with respect to the n...
CoinBigIndex getLastStart() const
void gutsOfOpEqual(const bool colordered, const int minor, const int major, const CoinBigIndex numels, const double *elem, const int *ind, const CoinBigIndex *start, const int *len)
void times(const double *x, double *y) const
Return A * x in y.
int compress(double threshold)
Eliminate all elements in matrix whose absolute value is less than threshold.
void submatrixOf(const CoinPackedMatrix &matrix, const int numMajor, const int *indMajor)
Extract a submatrix from matrix.
const CoinShallowPackedVector getVector(int i) const
Return the i'th vector in matrix.
friend void CoinPackedMatrixUnitTest()
A function that tests the methods in the CoinPackedMatrix class.
void orderMatrix()
Sort all columns so indices are increasing.in each column.
void gutsOfDestructor()
void transposeTimes(const double *x, double *y) const
Return x * A in y.
bool hasGaps() const
Whether the packed matrix has gaps or not.
double getExtraGap() const
Return the current setting of the extra gap.
double extraMajor_
his much times more space should be allocated for major-dimension vectors when the matrix is resized...
int * index_
List of nonzero element minor-dimension indices.
int getNumRows() const
Number of rows.
void deleteCols(const int numDel, const int *indDel)
Delete the columns whose indices are listed in indDel.
Sparse Matrix Base Class.
int * countOrthoLength() const
Count the number of entries in every minor-dimension vector and return an array containing these leng...
void deleteMinorVectors(const int numDel, const int *indDel)
Delete the minor-dimension vectors whose indices are listed in indDel.
CoinBigIndex maxSize_
max space allocated for entries
virtual ~CoinPackedMatrix()
Destructor.
int getVectorSize(const int i) const
The length of i'th vector.
int appendMinor(const int number, const CoinBigIndex *starts, const int *index, const double *element, int numberOther=-1)
Append a set of rows/columns to the end of the matrix.
Abstract base class for various sparse vectors.
void appendRow(const CoinPackedVectorBase &vec)
Append a row to the end of the matrix.
int maxMajorDim_
max space allocated for major-dimension
void assignMatrix(const bool colordered, const int minor, const int major, const CoinBigIndex numels, double *&elem, int *&ind, CoinBigIndex *&start, int *&len, const int maxmajor=-1, const CoinBigIndex maxsize=-1)
Assign the arguments to the matrix.
bool colOrdered_
A flag indicating whether the matrix is column or row major ordered.
int minorDim_
size of other dimension
int getMinorDim() const
Minor dimension.
int getMajorDim() const
Major dimension.
void reverseOrderedCopyOf(const CoinPackedMatrix &rhs)
Reverse copy method.
void appendMajorVector(const CoinPackedVectorBase &vec)
Append a major-dimension vector to the end of the matrix.
void majorAppendSameOrdered(const CoinPackedMatrix &matrix)
Append the columns of the argument to the right end of this matrix.
void copyReuseArrays(const CoinPackedMatrix &rhs)
Copy method.
void majorAppendOrthoOrdered(const CoinPackedMatrix &matrix)
Append the rows of the argument to the right end of this matrix.
int getSizeVectorStarts() const
The size of the vectorStarts array.
double getCoefficient(int row, int column) const
Return one element of packed matrix.
const int * getIndices() const
A vector containing the minor indices of the elements in the packed matrix.
void dumpMatrix(const char *fname=NULL) const
Dump the matrix on stdout.
void replaceVector(const int index, const int numReplace, const double *newElements)
Replace the elements of a vector.
void CoinPackedMatrixUnitTest()
A function that tests the methods in the CoinPackedMatrix class.
const CoinBigIndex * getVectorStarts() const
The positions where the major-dimension vectors start in elements and indices.
void printMatrixElement(const int row_val, const int col_val) const
Print a single matrix element.
bool isEquivalent2(const CoinPackedMatrix &rhs) const
Equivalence.
int getMaxMajorDim() const
Current maximum for major dimension.
void reverseOrdering()
Reverse the ordering of the packed matrix.
int * length_
Lengths of major-dimension vectors.
CoinPackedMatrix()
Default Constructor creates an empty column ordered packed matrix.
void setNumElements(CoinBigIndex value)
Change size after modifying - be careful.
int cleanMatrix(double threshold=1.0e-20)
Really clean up matrix.
void nullLengthArray()
NULLify length array - used when space is very tight.
void setExtraGap(const double newGap)
Set the extra gap to be allocated to the specified value.
int getNumCols() const
Number of columns.
bool isEquivalent(const CoinPackedMatrix &rhs, const FloatEqual &eq) const
Equivalence.
void gutsOfCopyOfNoGaps(const bool colordered, const int minor, const int major, const double *elem, const int *ind, const CoinBigIndex *start)
When no gaps we can do faster.
void submatrixOfWithDuplicates(const CoinPackedMatrix &matrix, const int numMajor, const int *indMajor)
Extract a submatrix from matrix.
void appendMajorVectors(const int numvecs, const CoinPackedVectorBase *const *vecs)
Append several major-dimensonvectors to the end of the matrix.
void modifyCoefficient(int row, int column, double newElement, bool keepZero=false)
Modify one element of packed matrix.
int * getMajorIndices() const
Returns an array containing major indices.
int * getMutableIndices() const
A vector containing the minor indices of the elements in the packed matrix.
void nullElementArray()
NULLify element array - used when space is very tight.
void bottomAppendPackedMatrix(const CoinPackedMatrix &matrix)
Append the argument to the "bottom" of the current matrix.
void transpose()
Transpose the matrix.
void timesMinor(const double *x, double *y) const
Return A * x (multiplied from the "right" direction) in y.
int majorDim_
number of vectors in matrix
void appendRows(const int numrows, const CoinPackedVectorBase *const *rows)
Append a set of rows to the end of the matrix.
Error Class thrown by an exception.
Definition: CoinError.hpp:40
double * element_
List of nonzero element values.
CoinBigIndex * getMutableVectorStarts() const
The positions where the major-dimension vectors start in elements and indices.
double * getMutableElements() const
A vector containing the elements in the packed matrix.
void minorAppendSameOrdered(const CoinPackedMatrix &matrix)
Append the columns of the argument to the bottom end of this matrix.
CoinBigIndex getVectorLast(const int i) const
The position of the last element (well, one entry past the last) in the i'th major-dimension vector...
CoinBigIndex * start_
Starting positions of major-dimension vectors.
void setDimensions(int numrows, int numcols)
Set the dimansions of the matrix.
int * getMutableVectorLengths() const
The lengths of the major-dimension vectors.
bool isColOrdered() const
Whether the packed matrix is column major ordered or not.
int eliminateDuplicates(double threshold)
Eliminate all duplicate AND small elements in matrix The column starts are not affected.
int getSizeVectorLengths() const
The size of the vectorLengths array.
CoinBigIndex getNumElements() const
Number of entries in the packed matrix.
void nullStartArray()
NULLify start array - used when space is very tight.
Function objects for testing equality of real numbers.
void deleteRows(const int numDel, const int *indDel)
Delete the rows whose indices are listed in indDel.
void copyOf(const CoinPackedMatrix &rhs)
Copy method.
void appendCol(const CoinPackedVectorBase &vec)
Append a column to the end of the matrix.
void gutsOfCopyOf(const bool colordered, const int minor, const int major, const CoinBigIndex numels, const double *elem, const int *ind, const CoinBigIndex *start, const int *len, const double extraMajor=0.0, const double extraGap=0.0)
void appendCols(const int numcols, const CoinPackedVectorBase *const *cols)
Append a set of columns to the end of the matrix.
void removeGaps(double removeValue=-1.0)
Remove the gaps from the matrix if there were any Can also remove small elements fabs() <= removeValu...
CoinPackedMatrix & operator=(const CoinPackedMatrix &rhs)
Assignment operator.
double getExtraMajor() const
Return the current setting of the extra major.
const double * getElements() const
A vector containing the elements in the packed matrix.
const int * getVectorLengths() const
The lengths of the major-dimension vectors.