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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright (C) 2005, 2009 International Business Machines and others.
// All Rights Reserved.
// This code is published under the Eclipse Public License.
//
// Authors: Andreas Waechter IBM 2005-12-24
#ifndef __IPDENSEGENMATRIX_HPP__
#define __IPDENSEGENMATRIX_HPP__
#include "IpUtils.hpp"
#include "IpMatrix.hpp"
#include "IpDenseVector.hpp"
#include "IpDenseSymMatrix.hpp"
namespace Ipopt
{
/** forward declarations */
class DenseGenMatrixSpace;
/** Class for dense general matrices.
*
* Matrix elements are stored in one array in "Fortran" format.
*/
class IPOPTLIB_EXPORT DenseGenMatrix: public Matrix
{
public:
/**@name Constructors / Destructors */
///@{
/** Constructor, taking the owner_space. */
DenseGenMatrix(
const DenseGenMatrixSpace* owner_space
);
/** Destructor */
~DenseGenMatrix();
///@}
/** Create a new DenseGenMatrix from same MatrixSpace */
SmartPtr<DenseGenMatrix> MakeNewDenseGenMatrix() const;
/** Retrieve the array for storing the matrix elements.
*
* This is the non-const version, and it is assume that afterwards
* the calling method will set all matrix elements. The matrix
* elements are stored one column after each other.
*/
Number* Values()
{
initialized_ = true;
ObjectChanged();
return values_;
}
/** Retrieve the array that stores the matrix elements.
*
* This is the const version, i.e., read-only. The matrix
* elements are stored one column after each other.
*/
const Number* Values() const
{
DBG_ASSERT(initialized_);
return values_;
}
/** Method for copying the content of another matrix into this matrix */
void Copy(
const DenseGenMatrix& M
);
/** Set this matrix to be a multiple of the identity matrix.
*
* This assumes that this matrix is square.
*/
void FillIdentity(
Number factor = 1.
);
/** Method for scaling the columns of the matrix.
*
* The scaling factors are given in form of a DenseVector.
*/
void ScaleColumns(
const DenseVector& scal_vec
);
/** Method for adding the product of two matrices to this matrix. */
void AddMatrixProduct(
Number alpha,
const DenseGenMatrix& A,
bool transA,
const DenseGenMatrix& B,
bool transB,
Number beta
);
/** Method for adding a high-rank update to this matrix.
*
* It computes M = alpha*V1^T V2 + beta*M, where V1 and V2 are
* MultiVectorMatrices.
*/
void HighRankUpdateTranspose(
Number alpha,
const MultiVectorMatrix& V1,
const MultiVectorMatrix& V2,
Number beta
);
/** Method for computing the Cholesky factorization of a positive
* definite matrix.
*
* The factor is stored in this matrix, as
* lower-triangular matrix, i.e., M = J * J^T.
*
* @return false if the factorization could not be done, e.g., when
* the matrix is not positive definite.
*/
bool ComputeCholeskyFactor(
const DenseSymMatrix& M
);
/** Method for computing an eigenvalue decomposition of the given
* symmetrix matrix M.
*
* On return, this matrix contains the
* eigenvalues in its columns, and Evalues contains the
* eigenvalues.
*
* @return false, if there were problems during the computation
*/
bool ComputeEigenVectors(
const DenseSymMatrix& M,
DenseVector& Evalues
);
/** Method for performing one backsolve with an entire matrix on
* the right hand side, assuming that the this matrix is square
* and contains a lower triangular matrix.
*
* The incoming right hand side B is overwritten with the solution
* X of op(A)*X = alpha*B. op(A) = A or op(A) = A^T.
*/
void CholeskyBackSolveMatrix(
bool trans,
Number alpha,
DenseGenMatrix& B
) const;
/** Method for performing a solve of a linear system for one
* vector, assuming that this matrix contains the Cholesky factor
* for the linear system.
*
* The vector b contains the right hand
* side on input, and contains the solution on output.
*/
void CholeskySolveVector(
DenseVector& b
) const;
/** Method for performing a solve of a linear system for one
* right-hand-side matrix, assuming that this matrix contains the
* Cholesky factor for the linear system.
*
* The matrix B contains the right hand sides on input, and contains
* the solution on output.
*/
void CholeskySolveMatrix(
DenseGenMatrix& B
) const;
/** Method for computing the LU factorization of an unsymmetric matrix.
*
* The factorization is done in place. */
bool ComputeLUFactorInPlace();
/** Method for using a previously computed LU factorization for a
* backsolve with a matrix on the rhs.
*/
void LUSolveMatrix(
DenseGenMatrix& B
) const;
/** Method for using a previously computed LU fatorization for a
* backsolve with a single vector.
*/
void LUSolveVector(
DenseVector& b
) const;
protected:
/**@name Overloaded methods from Matrix base class*/
///@{
virtual void MultVectorImpl(
Number alpha,
const Vector& x,
Number beta,
Vector& y
) const;
virtual void TransMultVectorImpl(
Number alpha,
const Vector& x,
Number beta,
Vector& y
) const;
virtual bool HasValidNumbersImpl() const;
virtual void ComputeRowAMaxImpl(
Vector& rows_norms,
bool init
) const;
virtual void ComputeColAMaxImpl(
Vector& cols_norms,
bool init
) const;
virtual void PrintImpl(
const Journalist& jnlst,
EJournalLevel level,
EJournalCategory category,
const std::string& name,
Index indent,
const std::string& prefix
) const;
///@}
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 */
DenseGenMatrix();
/** Copy Constructor */
DenseGenMatrix(
const DenseGenMatrix&
);
/** Default Assignment Operator */
void operator=(
const DenseGenMatrix&
);
///@}
const DenseGenMatrixSpace* owner_space_;
/** Array for storing the matrix elements (one columns after each other) */
Number* values_;
/** Flag indicating whether the values_ array has been initialized */
bool initialized_;
/** Enum for factorization type */
enum Factorization
{
NONE,
LU,
CHOL
};
/** Flag indicating if and which factorization has been applied */
Factorization factorization_;
/** Array for storing the pivot sequences if the matrix has been LU-factorized */
Index* pivot_;
};
/** This is the matrix space for DenseGenMatrix.
*/
class IPOPTLIB_EXPORT DenseGenMatrixSpace: public MatrixSpace
{
public:
/** @name Constructors / Destructors */
///@{
/** Constructor for matrix space for DenseGenMatrices.
*
* Takes in dimension of the matrices.
*/
DenseGenMatrixSpace(
Index nRows,
Index nCols
);
/** Destructor */
~DenseGenMatrixSpace()
{ }
///@}
/** Method for creating a new matrix of this specific type. */
DenseGenMatrix* MakeNewDenseGenMatrix() const
{
return new DenseGenMatrix(this);
}
virtual Matrix* MakeNew() const
{
return MakeNewDenseGenMatrix();
}
};
inline SmartPtr<DenseGenMatrix> DenseGenMatrix::MakeNewDenseGenMatrix() const
{
return owner_space_->MakeNewDenseGenMatrix();
}
} // namespace Ipopt
#endif