forked from coin-or/Ipopt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIpDenseSymMatrix.cpp
297 lines (267 loc) · 6.76 KB
/
IpDenseSymMatrix.cpp
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright (C) 2005, 2008 International Business Machines and others.
// All Rights Reserved.
// This code is published under the Eclipse Public License.
//
// Authors: Andreas Waechter IBM 2005-12-25
#include "IpDenseSymMatrix.hpp"
#include "IpDenseVector.hpp"
#include "IpDenseGenMatrix.hpp"
#include "IpBlas.hpp"
#include <cmath>
namespace Ipopt
{
#if IPOPT_VERBOSITY > 0
static const Index dbg_verbosity = 0;
#endif
DenseSymMatrix::DenseSymMatrix(
const DenseSymMatrixSpace* owner_space
)
: SymMatrix(owner_space),
owner_space_(owner_space),
values_(new Number[NCols() * NRows()]),
initialized_(false)
{
}
DenseSymMatrix::~DenseSymMatrix()
{
delete[] values_;
}
void DenseSymMatrix::MultVectorImpl(
Number alpha,
const Vector& x,
Number beta,
Vector& y
) const
{
// A few sanity checks
DBG_ASSERT(NCols() == x.Dim());
DBG_ASSERT(NRows() == y.Dim());
DBG_ASSERT(initialized_);
// See if we can understand the data
const DenseVector* dense_x = static_cast<const DenseVector*>(&x);
DBG_ASSERT(dynamic_cast<const DenseVector*>(&x));
DenseVector* dense_y = static_cast<DenseVector*>(&y);
DBG_ASSERT(dynamic_cast<DenseVector*>(&y));
IpBlasSymv(Dim(), alpha, values_, NRows(), dense_x->Values(), 1, beta, dense_y->Values(), 1);
}
void DenseSymMatrix::FillIdentity(
Number factor /*=1.*/
)
{
const Index dim = Dim();
for( Index j = 0; j < dim; j++ )
{
values_[j + j * dim] = factor;
for( Index i = j + 1; i < dim; i++ )
{
values_[i + j * dim] = 0.;
}
}
ObjectChanged();
initialized_ = true;
}
void DenseSymMatrix::AddMatrix(
Number alpha,
const DenseSymMatrix& A,
Number beta
)
{
DBG_ASSERT(beta == 0. || initialized_);
DBG_ASSERT(Dim() == A.Dim());
if( alpha == 0. )
{
return;
}
const Number* Avalues = A.Values();
const Index dim = Dim();
if( beta == 0. )
{
for( Index j = 0; j < dim; j++ )
{
for( Index i = j; i < dim; i++ )
{
values_[i + j * dim] = alpha * Avalues[i + j * dim];
}
}
}
else if( beta == 1. )
{
for( Index j = 0; j < dim; j++ )
{
for( Index i = j; i < dim; i++ )
{
values_[i + j * dim] += alpha * Avalues[i + j * dim];
}
}
}
else
{
for( Index j = 0; j < dim; j++ )
{
for( Index i = j; i < dim; i++ )
{
values_[i + j * dim] = alpha * Avalues[i + j * dim] + beta * values_[i + j * dim];
}
}
}
ObjectChanged();
initialized_ = true;
}
void DenseSymMatrix::HighRankUpdateTranspose(
Number alpha,
const MultiVectorMatrix& V1,
const MultiVectorMatrix& V2,
Number beta
)
{
DBG_ASSERT(Dim() == V1.NCols());
DBG_ASSERT(Dim() == V2.NCols());
DBG_ASSERT(beta == 0. || initialized_);
const Index dim = Dim();
if( beta == 0. )
{
for( Index j = 0; j < dim; j++ )
{
for( Index i = j; i < dim; i++ )
{
values_[i + j * dim] = alpha * V1.GetVector(i)->Dot(*V2.GetVector(j));
}
}
}
else
{
for( Index j = 0; j < dim; j++ )
{
for( Index i = j; i < dim; i++ )
{
values_[i + j * dim] = alpha * V1.GetVector(i)->Dot(*V2.GetVector(j)) + beta * values_[i + j * dim];
}
}
}
initialized_ = true;
ObjectChanged();
}
void DenseSymMatrix::HighRankUpdate(
bool trans,
Number alpha,
const DenseGenMatrix& V,
Number beta
)
{
DBG_ASSERT((!trans && Dim() == V.NRows()) || (trans && Dim() == V.NCols()));
DBG_ASSERT(beta == 0. || initialized_);
Index nrank;
if( trans )
{
nrank = V.NRows();
}
else
{
nrank = V.NCols();
}
IpBlasSyrk(trans, Dim(), nrank, alpha, V.Values(), V.NRows(), beta, values_, NRows());
initialized_ = true;
ObjectChanged();
}
void DenseSymMatrix::SpecialAddForLMSR1(
const DenseVector& D,
const DenseGenMatrix& L
)
{
const Index dim = Dim();
DBG_ASSERT(initialized_);
DBG_ASSERT(dim == D.Dim());
DBG_ASSERT(dim == L.NRows());
DBG_ASSERT(dim == L.NCols());
// First add the diagonal matrix
const Number* Dvalues = D.Values();
for( Index i = 0; i < dim; i++ )
{
values_[i + i * dim] += Dvalues[i];
}
// Now add the strictly-lower triagular matrix L and its transpose
const Number* Lvalues = L.Values();
for( Index j = 0; j < dim; j++ )
{
for( Index i = j + 1; i < dim; i++ )
{
values_[i + j * dim] += Lvalues[i + j * dim];
}
}
ObjectChanged();
}
bool DenseSymMatrix::HasValidNumbersImpl() const
{
DBG_ASSERT(initialized_);
Number sum = 0.;
const Index dim = Dim();
for( Index j = 0; j < dim; j++ )
{
sum += values_[j + j * dim];
for( Index i = j + 1; i < dim; i++ )
{
sum += values_[i + j * dim];
}
}
return IsFiniteNumber(sum);
}
void DenseSymMatrix::ComputeRowAMaxImpl(
Vector& rows_norms,
bool /*init*/
) const
{
// A few sanity checks
DBG_ASSERT(initialized_);
DenseVector* dense_vec = static_cast<DenseVector*>(&rows_norms);
DBG_ASSERT(dynamic_cast<DenseVector*>(&rows_norms));
Number* vec_vals = dense_vec->Values();
const Number* vals = values_;
for( Index irow = 0; irow < NRows(); irow++ )
{
for( Index jcol = 0; jcol <= irow; jcol++ )
{
const Number f = std::abs(*vals);
vec_vals[irow] = Max(vec_vals[irow], f);
vec_vals[jcol] = Max(vec_vals[jcol], f);
vals++;
}
}
}
void DenseSymMatrix::PrintImpl(
const Journalist& jnlst,
EJournalLevel level,
EJournalCategory category,
const std::string& name,
Index indent,
const std::string& prefix
) const
{
jnlst.Printf(level, category,
"\n");
jnlst.PrintfIndented(level, category, indent,
"%sDenseSymMatrix \"%s\" of dimension %" IPOPT_INDEX_FORMAT " (only lower triangular part printed):\n", prefix.c_str(), name.c_str(),
Dim());
if( initialized_ )
{
for( Index j = 0; j < NCols(); j++ )
{
for( Index i = j; i < NRows(); i++ )
{
jnlst.PrintfIndented(level, category, indent,
"%s%s[%5" IPOPT_INDEX_FORMAT ",%5" IPOPT_INDEX_FORMAT "]=%23.16e\n", prefix.c_str(), name.c_str(), i, j, values_[i + NRows() * j]);
}
}
}
else
{
jnlst.PrintfIndented(level, category, indent,
"The matrix has not yet been initialized!\n");
}
}
DenseSymMatrixSpace::DenseSymMatrixSpace(
Index nDim
)
: SymMatrixSpace(nDim)
{
}
} // namespace Ipopt