Debian Codegraph @

Bonmin/src/Algorithms/QuadCuts/BonTMatrix.cpp

raw
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 #include "BonTMatrix.hpp"
11
12 namespace Bonmin{
13
14 /** Copy constructor.*/
15 TMat::TMat(const TMat &other):
16 iRow_(NULL), jCol_(NULL), value_(NULL), nnz_(other.nnz_),
17 capacity_(other.nnz_), columnOrdering_(other.columnOrdering_),
18 rowOrdering_(other.rowOrdering_), nonEmptyRows_(), nonEmptyCols_(){
19 iRow_ = CoinCopyOfArray(other.iRow_, other.nnz_);
20 jCol_ = CoinCopyOfArray(other.jCol_, other.nnz_);
21 value_ = CoinCopyOfArray(other.value_, other.nnz_);
22 }
23
24 /** Construct from a CoinPackedMatrix*/
25 TMat::TMat(const CoinPackedMatrix &M, MatrixStorageType T):
26 iRow_(NULL), jCol_(NULL), value_(NULL), nnz_(M.getNumElements()),
27 capacity_(M.getNumElements()), columnOrdering_(), rowOrdering_(),
28 nonEmptyRows_(), nonEmptyCols_(){
29 create(M);
30 make_upper_triangular(T);
31 }
32 /** Assignment operator.*/
33 TMat&
34 TMat::operator=(const TMat &rhs){
35 if(this != &rhs){
36 freeSpace();
37 nnz_ = rhs.nnz_;
38 capacity_ = rhs.capacity_;
39 iRow_ = CoinCopyOfArray(rhs.iRow_, rhs.nnz_);
40 jCol_ = CoinCopyOfArray(rhs.jCol_, rhs.nnz_);
41 value_ = CoinCopyOfArray(rhs.value_, rhs.nnz_);
42 columnOrdering_ = rhs.columnOrdering_;
43 rowOrdering_ = rhs.rowOrdering_;
44 nonEmptyCols_.clear();
45 nonEmptyRows_.clear();
46 }
47 return (*this);
48 }
49
50 /** Assignment from a CoinPackedMatrix.*/
51 TMat &
52 TMat::operator=(const CoinPackedMatrix &M){
53 freeSpace();
54 columnOrdering_.clear();
55 rowOrdering_.clear();
56 nnz_ = capacity_ = M.getNumElements();
57 create(M);
58 return (*this);
59 }
60
61 void TMat::create(const CoinPackedMatrix &M){
62 // Allocate arrays;
63 iRow_ = new int[capacity_];
64 jCol_ = new int[capacity_];
65 value_ = new double[capacity_];
66
67 int * iRow = iRow_;
68 int * jCol = jCol_;
69 if(!M.isColOrdered()){// Have to swap
70 std::cout<<"Matrix is not col ordered"<<std::endl;
71 iRow = jCol_;
72 jCol = iRow_;
73 }
74
75 // Now we can safely assume that M is colorderd.
76 int numcols = M.getMajorDim();
77 const int * start = M.getVectorStarts();
78 const int * length = M.getVectorLengths();
79 const int * indice = M.getIndices();
80 const double * value = M.getElements();
81 int nnz = 0;
82 for(int i = 0 ; i < numcols ; i++){
83 int begin = start[i];
84 int end = start[i] + length[i];
85 for(int k = begin ; k < end ; k++){
86 value_[nnz] = value[k];
87 iRow[nnz] = indice[k];
88 jCol[nnz++] = i;
89 }
90 }
91 assert(nnz==nnz_);
92 }
93
94 // Destructor
95 TMat::~TMat(){
96 delete [] iRow_;
97 delete [] jCol_;
98 delete [] value_;
99 }
100
101 /** Put the non-empty rows of quadratic form currently stored into
102 indices. Allocate the array and returns its size.*/
103 int
104 TMat::numNonEmptyRows(){
105 if(nnz_ == 0) return 0;
106 orderByRows();
107 nonEmptyRows_.clear();
108 nonEmptyRows_.push_back(std::pair<int, int>(iRow_[rowOrdering_[0]], 0));
109 int num = 1;
110 for(int i = 1 ; i < nnz_ ; i++){
111 if(iRow_[rowOrdering_[i]] > nonEmptyRows_.back().first){
112 nonEmptyRows_.push_back(std::pair<int, int>(iRow_[rowOrdering_[i]],i));
113 num++;
114 }
115 }
116 return num;
117 }
118
119
120 /** Put the non-empty rows of quadratic form currently stored into
121 indices. Allocate the array and returns its size.*/
122 int
123 TMat::numNonEmptyCols(){
124 if(nnz_ == 0) return 0;
125 orderByColumns();
126 nonEmptyCols_.clear();
127 nonEmptyCols_.push_back(std::pair<int, int>(jCol_[columnOrdering_[0]], 0));
128 int num = 1;
129 for(int i = 1 ; i < nnz_ ; i++){
130 if(jCol_[columnOrdering_[i]] > nonEmptyCols_.back().first){
131 nonEmptyCols_.push_back(std::pair<int, int>(jCol_[columnOrdering_[i]],i));
132 num++;
133 }
134 }
135 return num;
136 }
137 /** Remove the duplicated entries.*/
138 void
139 TMat::removeDuplicates(){
140 orderByRows();
141 int j = 0;
142 for(int i = 1; i < nnz_ ; i++){
143 if((jCol_[rowOrdering_[i]] == jCol_[rowOrdering_[j]]) &&
144 (iRow_[rowOrdering_[i]] == iRow_[rowOrdering_[j]])){
145 value_[rowOrdering_[j]] += value_[rowOrdering_[i]];
146 }
147 else{
148 jCol_[rowOrdering_[++j]] = jCol_[rowOrdering_[i]];
149 iRow_[rowOrdering_[j]] = iRow_[rowOrdering_[i]];
150 value_[rowOrdering_[j]] = value_[rowOrdering_[i]];
151 }
152 }
153 resizeAndCopyArray(jCol_, j, capacity_);
154 resizeAndCopyArray(iRow_, j, capacity_);
155 resizeAndCopyArray(value_, j, capacity_);
156 nnz_ = j;
157 }
158
159 void
160 TMat::make_upper_triangular(const MatrixStorageType &T){
161 switch (T){
162 case Upper:
163 for(int i = 0 ; i < nnz_ ; i++){
164 assert(jCol_[i] >= iRow_[i]);
165 }
166 break;
167 case Lower:
168 for(int i = 0 ; i < nnz_ ; i++){
169 assert(jCol_[i] <= iRow_[i]);
170 }
171 make_lower_to_be_upper();
172 break;
173 case Full:
174 make_full_upper_triangular();
175 break;
176 }
177 for(int i = 0 ; i < nnz_ ; i++){
178 assert(jCol_[i] >= iRow_[i]);
179 }
180 }
181
182 /** Assuing that this is representing the lower triangle of a symetric matrix
183 makes it the upper triangle.*/
184 void
185 TMat::make_lower_to_be_upper(){
186 int * buff = iRow_;
187 iRow_ = jCol_;
188 jCol_ = buff;
189 }
190
191 /** Assuming that this is representing a quadratic form. Makes it upper diagonal.*/
192 void
193 TMat::make_full_upper_triangular(){
194 // Make it upper triangular
195 for(int i = 0 , j = 0; i < nnz_ && j < nnz_ ;){
196 if(iRow_[i] < jCol_[i]){//swap the two entries
197 int buf = iRow_[i];
198 iRow_[i] = jCol_[i];
199 jCol_[i] = buf;
200 }
201 }
202 // add up the duplicated entries
203 removeDuplicates();
204
205 //Now divide all non-diagonal entries by two;
206 for(int i = 0 ; i < nnz_ ; i++){
207 if(jCol_[i] == iRow_[i]){//skip
208 continue;
209 }
210 assert(iRow_[i] < jCol_[i]);
211 value_[i] /= 2.;
212 }
213 }
214
215 }//Ends Bonmin namepace
216