BonTMatrix.cpp
Go to the documentation of this file.
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 
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 
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);
31 }
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_);
44  nonEmptyCols_.clear();
45  nonEmptyRows_.clear();
46  }
47  return (*this);
48 }
49 
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
96  delete [] iRow_;
97  delete [] jCol_;
98  delete [] value_;
99 }
100 
103 int
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 
122 int
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 }
138 void
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  }
156  nnz_ = j;
157 }
158 
159 void
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  }
172  break;
173  case Full:
175  break;
176  }
177  for(int i = 0 ; i < nnz_ ; i++){
178  assert(jCol_[i] >= iRow_[i]);
179  }
180 }
181 
184 void
186  int * buff = iRow_;
187  iRow_ = jCol_;
188  jCol_ = buff;
189 }
190 
192 void
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
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 
Stores the whole matrix of a non-symetric Q.
Definition: BonQuadCut.hpp:25
void make_lower_to_be_upper()
Assuing that this is representing the lower triangle of a symetric matrix makes it the upper triangle...
Definition: BonTMatrix.cpp:185
int numNonEmptyCols()
Get number of non empty cols.
Definition: BonTMatrix.cpp:123
void make_upper_triangular(const MatrixStorageType &T)
Definition: BonTMatrix.cpp:160
TMat & operator=(const TMat &rhs)
Assignment operator.
Definition: BonTMatrix.cpp:34
vector< int > rowOrdering_
Definition: BonTMatrix.hpp:149
RowS nonEmptyCols_
Definition: BonTMatrix.hpp:161
const vector< int > & orderByColumns()
Orders current matrix by columns.
Definition: BonTMatrix.hpp:118
TMat()
Default constructor.
Definition: BonTMatrix.hpp:35
MatrixStorageType
Definition: BonQuadCut.hpp:22
static char * j
Definition: OSdtoa.cpp:3622
const vector< int > & orderByRows()
Orders current matrix by rows.
Definition: BonTMatrix.hpp:124
Stores only the upper triangle of a symetric Q.
Definition: BonQuadCut.hpp:23
RowS nonEmptyRows_
Definition: BonTMatrix.hpp:158
fint end
void make_full_upper_triangular()
Assuming that this is representing a quadratic form.
Definition: BonTMatrix.cpp:193
void fint fint * k
double * value_
Definition: BonTMatrix.hpp:25
vector< int > columnOrdering_
Definition: BonTMatrix.hpp:147
Stores the lower triangle of a symetric Q.
Definition: BonQuadCut.hpp:24
int nnz
ATTENTION: Filter expect the jacobian to be ordered by row.
void freeSpace()
Definition: BonTMatrix.hpp:40
void removeDuplicates()
Remove the duplicated entries.
Definition: BonTMatrix.cpp:139
void resizeAndCopyArray(X *&array, unsigned int oldSize, unsigned int newSize)
void create(const CoinPackedMatrix &M)
Create the TMat from M.
Definition: BonTMatrix.cpp:61
int numNonEmptyRows()
Get number of non empty rows.
Definition: BonTMatrix.cpp:104