| 1 |
// (C) Copyright International Business Machines Corporation 2007 |
| 2 |
// All Rights Reserved. |
| 3 |
// This code is published under the Eclipse Public License. |
| 4 |
// |
| 5 |
// Authors : |
| 6 |
// Pierre Bonami, International Business Machines Corporation |
| 7 |
// |
| 8 |
// Date : 10/06/2007 |
| 9 |
|
| 10 |
#ifndef BonTMatrix_H |
| 11 |
#define BonTMatrix_H |
| 12 |
|
| 13 |
#include "CoinPackedMatrix.hpp" |
| 14 |
#include "BonArraysHelpers.hpp" |
| 15 |
#include <vector> |
| 16 |
#include <list> |
| 17 |
#include <algorithm> |
| 18 |
#include "BonQuadCut.hpp" |
| 19 |
|
| 20 |
namespace Bonmin { |
| 21 |
|
| 22 |
struct TMat{ |
| 23 |
int * iRow_; |
| 24 |
int * jCol_; |
| 25 |
double * value_; |
| 26 |
int nnz_; |
| 27 |
int capacity_; |
| 28 |
|
| 29 |
|
| 30 |
/** Storage for non empty rows. |
| 31 |
first is row number and second is first element in row.*/ |
| 32 |
typedef vector< std::pair< int, int> > RowS; |
| 33 |
|
| 34 |
/** Default constructor.*/ |
| 35 |
TMat(): iRow_(NULL), jCol_(NULL), value_(NULL), nnz_(0), |
| 36 |
capacity_(0) |
| 37 |
{} |
| 38 |
|
| 39 |
|
| 40 |
void freeSpace(){ |
| 41 |
delete [] iRow_; |
| 42 |
delete [] jCol_; |
| 43 |
delete [] value_; |
| 44 |
} |
| 45 |
|
| 46 |
/** Copy constructor.*/ |
| 47 |
TMat(const TMat &other); |
| 48 |
|
| 49 |
/** Construct from a CoinPackedMatrix*/ |
| 50 |
TMat(const CoinPackedMatrix &M, MatrixStorageType T); |
| 51 |
|
| 52 |
/** Assignment operator.*/ |
| 53 |
TMat& operator=(const TMat &rhs); |
| 54 |
|
| 55 |
/** Assignment from a CoinPackedMatrix.*/ |
| 56 |
TMat & operator=(const CoinPackedMatrix &M); |
| 57 |
|
| 58 |
void resize(int nnz){ |
| 59 |
Bonmin::resizeAndCopyArray(iRow_, nnz_, nnz); |
| 60 |
Bonmin::resizeAndCopyArray(jCol_, nnz_, nnz); |
| 61 |
Bonmin::resizeAndCopyArray(value_, nnz_, nnz); |
| 62 |
nnz_ = nnz; |
| 63 |
} |
| 64 |
|
| 65 |
~TMat(); |
| 66 |
|
| 67 |
/** Get number of non empty rows.*/ |
| 68 |
int numNonEmptyRows(); |
| 69 |
|
| 70 |
/** Get the list of non empty row.*/ |
| 71 |
const RowS & nonEmptyRows() const { |
| 72 |
return nonEmptyRows_;} |
| 73 |
|
| 74 |
/** Get number of non empty cols.*/ |
| 75 |
int numNonEmptyCols(); |
| 76 |
|
| 77 |
/** Get the list of non empty row.*/ |
| 78 |
const RowS & nonEmptyCols() const { |
| 79 |
return nonEmptyCols_;} |
| 80 |
|
| 81 |
private: |
| 82 |
/** Structure for ordering matrix.*/ |
| 83 |
struct TMatOrdering{ |
| 84 |
TMat * M_; |
| 85 |
TMatOrdering(TMat *M): |
| 86 |
M_(M){} |
| 87 |
}; |
| 88 |
|
| 89 |
/** Structure for ordering matrix by columns.*/ |
| 90 |
struct ColumnOrder : public TMatOrdering { |
| 91 |
ColumnOrder(TMat *M): |
| 92 |
TMatOrdering(M){} |
| 93 |
|
| 94 |
bool operator()(const int& i, const int& j){ |
| 95 |
if (M_->jCol_[i] < M_->jCol_[j]) |
| 96 |
return true; |
| 97 |
if (M_->jCol_[i] == M_->jCol_[j] && M_->iRow_[i] < M_->iRow_[j]) |
| 98 |
return true; |
| 99 |
return false; |
| 100 |
} |
| 101 |
}; |
| 102 |
|
| 103 |
|
| 104 |
/** Structure for ordering matrix by columns.*/ |
| 105 |
struct RowOrder : public TMatOrdering { |
| 106 |
RowOrder(TMat *M): |
| 107 |
TMatOrdering(M){} |
| 108 |
bool operator()(const int& i, const int& j){ |
| 109 |
if (M_->iRow_[i]< M_->iRow_[j]) |
| 110 |
return true; |
| 111 |
if (M_->iRow_[i] == M_->iRow_[j] && M_->jCol_[i] < M_->jCol_[j]) |
| 112 |
return true; |
| 113 |
return false; |
| 114 |
} |
| 115 |
}; |
| 116 |
public: |
| 117 |
/** Orders current matrix by columns. */ |
| 118 |
const vector<int>& orderByColumns(){ |
| 119 |
resizeOrdering(columnOrdering_, nnz_); |
| 120 |
std::sort(columnOrdering_.begin(), columnOrdering_.end(),ColumnOrder(this)); |
| 121 |
return columnOrdering_; |
| 122 |
} |
| 123 |
/** Orders current matrix by rows.*/ |
| 124 |
const vector<int>& orderByRows(){ |
| 125 |
resizeOrdering(rowOrdering_, nnz_); |
| 126 |
std::sort(rowOrdering_.begin(), rowOrdering_.end(), RowOrder(this)); |
| 127 |
return rowOrdering_; |
| 128 |
} |
| 129 |
|
| 130 |
/** Remove the duplicated entries.*/ |
| 131 |
void removeDuplicates(); |
| 132 |
|
| 133 |
/** Assuming that this is representing a quadratic form. Produce equivalent |
| 134 |
quadratic form with only upper triange stored.*/ |
| 135 |
void makeQuadUpperDiag(); |
| 136 |
|
| 137 |
void resizeOrdering(vector<int> &ordering, unsigned int newSize){ |
| 138 |
size_t oldSize = ordering.size(); |
| 139 |
ordering.resize(newSize); |
| 140 |
for(size_t i = oldSize ; i < newSize ; i++) |
| 141 |
ordering[i] = static_cast<int>(i); |
| 142 |
} |
| 143 |
|
| 144 |
/** Create the TMat from M.*/ |
| 145 |
void create(const CoinPackedMatrix &M); |
| 146 |
|
| 147 |
vector<int> columnOrdering_; |
| 148 |
|
| 149 |
vector<int> rowOrdering_; |
| 150 |
|
| 151 |
void make_upper_triangular(const MatrixStorageType &T); |
| 152 |
|
| 153 |
void make_lower_to_be_upper(); |
| 154 |
|
| 155 |
void make_full_upper_triangular(); |
| 156 |
|
| 157 |
// Stores non empty rows for computing jacobian structure |
| 158 |
RowS nonEmptyRows_; |
| 159 |
|
| 160 |
// Stores non empty cols for computing jacobian structure |
| 161 |
RowS nonEmptyCols_; |
| 162 |
}; |
| 163 |
|
| 164 |
}//Ends Bonmin namespace |
| 165 |
|
| 166 |
#endif |
| 167 |
|