Bonmin  1.7
BonTMatrix.hpp
Go to the documentation of this file.
00001 // (C) Copyright International Business Machines Corporation 2007
00002 // All Rights Reserved.
00003 // This code is published under the Common Public License.
00004 //
00005 // Authors :
00006 // Pierre Bonami, International Business Machines Corporation
00007 //
00008 // Date : 10/06/2007
00009 
00010 #ifndef BonTMatrix_H
00011 #define BonTMatrix_H
00012 
00013 #include "CoinPackedMatrix.hpp"
00014 #include "BonArraysHelpers.hpp"
00015 #include <vector>
00016 #include <list>
00017 #include <algorithm> 
00018 #include "BonQuadCut.hpp"
00019 
00020 namespace Bonmin {
00021 
00022 struct TMat{
00023   int * iRow_;
00024   int * jCol_;
00025   double * value_;
00026   int nnz_;
00027   int capacity_;
00028 
00029 
00032  typedef vector< std::pair< int, int> > RowS;
00033 
00035   TMat(): iRow_(NULL), jCol_(NULL), value_(NULL), nnz_(0),
00036                capacity_(0)
00037   {}
00038 
00039 
00040   void freeSpace(){
00041      delete [] iRow_;
00042      delete [] jCol_;
00043      delete [] value_;
00044   } 
00045 
00047   TMat(const TMat &other);
00048 
00050   TMat(const CoinPackedMatrix &M,  MatrixStorageType T);
00051 
00053   TMat& operator=(const TMat &rhs);
00054 
00056   TMat & operator=(const CoinPackedMatrix &M);
00057 
00058   void resize(int nnz){
00059     Bonmin::resizeAndCopyArray(iRow_, nnz_, nnz);
00060     Bonmin::resizeAndCopyArray(jCol_, nnz_, nnz);
00061     Bonmin::resizeAndCopyArray(value_, nnz_, nnz);
00062     nnz_ = nnz;
00063   }
00064 
00065   ~TMat();
00066 
00068  int numNonEmptyRows();
00069 
00071  const RowS & nonEmptyRows() const {
00072     return nonEmptyRows_;}
00073 
00075  int numNonEmptyCols();
00076 
00078  const RowS & nonEmptyCols() const {
00079     return nonEmptyCols_;}
00080 
00081  private:
00083  struct TMatOrdering{
00084    TMat * M_;
00085    TMatOrdering(TMat *M):
00086      M_(M){}
00087  };
00088 
00090  struct ColumnOrder : public TMatOrdering {
00091    ColumnOrder(TMat *M):
00092      TMatOrdering(M){}
00093 
00094    bool operator()(const int& i, const int& j){
00095       if (M_->jCol_[i] < M_->jCol_[j])
00096         return true;
00097       if (M_->jCol_[i] == M_->jCol_[j] && M_->iRow_[i] < M_->iRow_[j])
00098         return true;
00099      return false;
00100    }
00101  };
00102 
00103 
00105  struct RowOrder : public TMatOrdering {
00106    RowOrder(TMat *M):
00107      TMatOrdering(M){}
00108    bool operator()(const int& i, const int& j){
00109       if (M_->iRow_[i]< M_->iRow_[j])
00110         return true;
00111       if (M_->iRow_[i] == M_->iRow_[j] && M_->jCol_[i] < M_->jCol_[j])
00112         return true;
00113      return false;
00114    }
00115  };
00116  public:
00118  const vector<int>& orderByColumns(){
00119     resizeOrdering(columnOrdering_, nnz_);
00120     std::sort(columnOrdering_.begin(), columnOrdering_.end(),ColumnOrder(this));
00121     return columnOrdering_;
00122  }
00124  const vector<int>& orderByRows(){
00125     resizeOrdering(rowOrdering_, nnz_);
00126     std::sort(rowOrdering_.begin(), rowOrdering_.end(), RowOrder(this));
00127     return rowOrdering_;
00128  }
00129 
00131  void removeDuplicates();
00132 
00135  void makeQuadUpperDiag();
00136 
00137  void resizeOrdering(vector<int> &ordering, unsigned int newSize){
00138         size_t oldSize = ordering.size();
00139         ordering.resize(newSize);
00140         for(size_t i = oldSize ; i < newSize ; i++)
00141            ordering[i] = static_cast<int>(i);
00142    }
00143 
00145    void create(const CoinPackedMatrix &M);
00146  
00147    vector<int> columnOrdering_;
00148  
00149    vector<int> rowOrdering_;
00150 
00151    void make_upper_triangular(const MatrixStorageType &T);
00152 
00153    void make_lower_to_be_upper();
00154 
00155    void make_full_upper_triangular();
00156 
00157    // Stores non empty rows for computing jacobian structure
00158    RowS nonEmptyRows_;
00159 
00160    // Stores non empty cols for computing jacobian structure
00161    RowS nonEmptyCols_;
00162  };
00163 
00164 }//Ends Bonmin namespace
00165 
00166 #endif
00167