1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Copyright (C) 2005, 2008 International Business Machines and others.
// All Rights Reserved.
// This code is published under the Eclipse Public License.
//
// Authors: Carl Laird, Andreas Waechter IBM 2005-03-13
#ifndef __IPTRIPLETTOCSRCONVERTER_HPP__
#define __IPTRIPLETTOCSRCONVERTER_HPP__
#include "IpUtils.hpp"
#include "IpReferenced.hpp"
namespace Ipopt
{
/** Class for converting symmetric matrices given in triplet format
* to matrices in compressed sparse row (CSR) format of the upper
* triangular part (or, equivalently, compressed sparse column (CSC)
* format for the lower triangular part).
*
* In the description for this class, we assume that we discuss the CSR format.
*/
class TripletToCSRConverter: public ReferencedObject
{
/** Class for one triplet position entry. */
class TripletEntry
{
public:
/** Set the values of an entry */
void Set(
Index i_row,
Index j_col,
Index i_pos_triplet
)
{
if( i_row > j_col )
{
i_row_ = j_col;
j_col_ = i_row;
}
else
{
i_row_ = i_row;
j_col_ = j_col;
}
i_pos_triplet_ = i_pos_triplet;
}
/** @name Accessor methods. */
///@{
/** Row position. */
Index IRow() const
{
return i_row_;
}
/** Column position. */
Index JCol() const
{
return j_col_;
}
/** Index in original triplet matrix. */
Index PosTriplet() const
{
return i_pos_triplet_;
}
///@}
/** Comparison operator.
*
* This is required for the sort function.
*/
bool operator<(
const TripletEntry& Tentry
) const
{
return ((i_row_ < Tentry.i_row_) || (i_row_ == Tentry.i_row_ && j_col_ < Tentry.j_col_));
}
private:
/** @name Entry content. */
///@{
Index i_row_;
Index j_col_;
Index i_pos_triplet_;
///@}
};
public:
/** Enum to specify half or full matrix storage */
enum ETriFull
{
/** Lower (or Upper) triangular stored only */
Triangular_Format,
/** Store both lower and upper parts */
Full_Format
};
/** @name Constructor/Destructor */
///@{
/* Constructor.
*
* If offset is 0, then the counting of indices in the compressed
* format starts a 0 (C-style numbering); if offset is 1, then the
* counting starts at 1 (Fortran-type numbering).
*/
TripletToCSRConverter(
Index offset,
ETriFull hf = Triangular_Format
);
/** Destructor */
virtual ~TripletToCSRConverter();
///@}
/** Initialize the converter, given the fixed structure of the matrix.
*
* There, ndim gives the number of rows and columns of
* the matrix, nonzeros give the number of nonzero elements, and
* airn and acjn give the positions of the nonzero elements.
*
* @return number of nonzeros in the condensed matrix.
* (Since nonzero elements can be listed several times
* in the triplet format, it is possible that this value is
* different from the input value nonzeros.)
*
* This method must be called before IA, JA, or ConvertValues.
*/
Index InitializeConverter(
Index dim,
Index nonzeros,
const Index* airn,
const Index* ajcn
);
/** @name Accessor methods */
///@{
/** Return the IA array for the condensed format. */
const Index* IA() const
{
DBG_ASSERT(initialized_);
return ia_;
}
/** Return the JA array for the condensed format. */
const Index* JA() const
{
DBG_ASSERT(initialized_);
return ja_;
}
const Index* iPosFirst() const
{
DBG_ASSERT(initialized_);
return ipos_first_;
}
///@}
/** Convert the values of the nonzero elements.
*
* Given the values a_triplet for the triplet format, return
* the array of values for the condensed format in a_condensed.
* nonzeros_condensed is the length of the array a_condensed
* and must be identical to the return value of InitializeConverter.
*/
void ConvertValues(
Index nonzeros_triplet,
const Number* a_triplet,
Index nonzeros_compressed,
Number* a_compressed
);
private:
/**@name Default Compiler Generated Methods
* (Hidden to avoid implicit creation/calling).
* These methods are not implemented and
* we do not want the compiler to implement
* them for us, so we declare them private
* and do not define them. This ensures that
* they will not be implicitly created/called. */
///@{
/** Default Constructor */
TripletToCSRConverter();
/** Copy Constructor */
TripletToCSRConverter(
const TripletToCSRConverter&
);
/** Default Assignment Operator */
void operator=(
const TripletToCSRConverter&
);
///@}
/** Offset for CSR numbering. */
Index offset_;
/** Indicator of half (ie lower only) or full (both upr and lwr) matrix */
ETriFull hf_;
/** Array storing the values for IA in the condensed format */
Index* ia_;
/** Array storing the values for JA in the condensed format */
Index* ja_;
/** Dimension of the matrix. */
Index dim_;
/** Number of nonzeros in the triplet format. */
Index nonzeros_triplet_;
/** Number of nonzeros in the compressed format. */
Index nonzeros_compressed_;
/** Number of repeated entries */
Index num_doubles_;
/** Flag indicating if initialize method had been called. */
bool initialized_;
/** @name Arrays for cross-positions for the conversion of values. */
///@{
/** First elements assignment.
*
* For i with 0 <= i <= nonzeros_compressed-1, the i-th element in
* the compressed format is obtained from copying the ipos_filter_[i]-th
* element from the triplet format.
*/
Index* ipos_first_;
/** Position of multiple elements in triplet matrix.
* For i = 0,..,nonzeros_triplet_-nonzeros_compressed_, the
* ipos_double_triplet_[i]-th element in the triplet matrix has
* to be added to the ipos_double_compressed_[i]-th element in
* the compressed matrix. */
Index* ipos_double_triplet_;
/** Position of multiple elements in compressed matrix. */
Index* ipos_double_compressed_;
///@}
};
} // namespace Ipopt
#endif