summaryrefslogtreecommitdiff
path: root/newstructure/thirdparty/linux/include/coin/IpMatrix.hpp
blob: 79018da8e35d480cb490babb8cf486c102ad537a (plain)
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright (C) 2004, 2008 International Business Machines and others.
// All Rights Reserved.
// This code is published under the Eclipse Public License.
//
// $Id: IpMatrix.hpp 2472 2014-04-05 17:47:20Z stefan $
//
// Authors:  Carl Laird, Andreas Waechter     IBM    2004-08-13

#ifndef __IPMATRIX_HPP__
#define __IPMATRIX_HPP__

#include "IpVector.hpp"

namespace Ipopt
{

  /* forward declarations */
  class MatrixSpace;

  /** Matrix Base Class. This is the base class for all derived matrix
   *  types.  All Matrices, such as Jacobian and Hessian matrices, as
   *  well as possibly the iteration matrices needed for the step
   *  computation, are of this type.
   *
   *  Deriving from Matrix:  Overload the protected XXX_Impl method.
   */
  class Matrix : public TaggedObject
  {
  public:
    /** @name Constructor/Destructor */
    //@{
    /** Constructor.  It has to be given a pointer to the
     *  corresponding MatrixSpace.
     */
    Matrix(const MatrixSpace* owner_space)
        :
        TaggedObject(),
        owner_space_(owner_space),
        valid_cache_tag_(0)
    {}

    /** Destructor */
    virtual ~Matrix()
    {}
    //@}

    /**@name Operations of the Matrix on a Vector */
    //@{
    /** Matrix-vector multiply.  Computes y = alpha * Matrix * x +
     *  beta * y.  Do not overload.  Overload MultVectorImpl instead.
     */
    void MultVector(Number alpha, const Vector& x, Number beta,
                    Vector& y) const
    {
      MultVectorImpl(alpha, x, beta, y);
    }

    /** Matrix(transpose) vector multiply.  Computes y = alpha *
     *  Matrix^T * x + beta * y.  Do not overload.  Overload
     *  TransMultVectorImpl instead.
     */
    void TransMultVector(Number alpha, const Vector& x, Number beta,
                         Vector& y) const
    {
      TransMultVectorImpl(alpha, x, beta, y);
    }
    //@}

    /** @name Methods for specialized operations.  A prototype
     *  implementation is provided, but for efficient implementation
     *  those should be specially implemented.
     */
    //@{
    /** X = X + alpha*(Matrix S^{-1} Z).  Should be implemented
     *  efficiently for the ExansionMatrix
     */
    void AddMSinvZ(Number alpha, const Vector& S, const Vector& Z,
                   Vector& X) const;

    /** X = S^{-1} (r + alpha*Z*M^Td).   Should be implemented
     *  efficiently for the ExansionMatrix
     */
    void SinvBlrmZMTdBr(Number alpha, const Vector& S,
                        const Vector& R, const Vector& Z,
                        const Vector& D, Vector& X) const;
    //@}

    /** Method for determining if all stored numbers are valid (i.e.,
     *  no Inf or Nan). */
    bool HasValidNumbers() const;

    /** @name Information about the size of the matrix */
    //@{
    /** Number of rows */
    inline
    Index  NRows() const;

    /** Number of columns */
    inline
    Index  NCols() const;
    //@}

    /** @name Norms of the individual rows and columns */
    //@{
    /** Compute the max-norm of the rows in the matrix.  The result is
     *  stored in rows_norms.  The vector is assumed to be initialized
     *  of init is false. */
    void ComputeRowAMax(Vector& rows_norms, bool init=true) const
    {
      DBG_ASSERT(NRows() == rows_norms.Dim());
      if (init) rows_norms.Set(0.);
      ComputeRowAMaxImpl(rows_norms, init);
    }
    /** Compute the max-norm of the columns in the matrix.  The result
     *  is stored in cols_norms  The vector is assumed to be initialized
     *  of init is false. */
    void ComputeColAMax(Vector& cols_norms, bool init=true) const
    {
      DBG_ASSERT(NCols() == cols_norms.Dim());
      if (init) cols_norms.Set(0.);
      ComputeColAMaxImpl(cols_norms, init);
    }
    //@}

    /** Print detailed information about the matrix. Do not overload.
     *  Overload PrintImpl instead.
     */
    //@{
    virtual void Print(SmartPtr<const Journalist> jnlst,
                       EJournalLevel level,
                       EJournalCategory category,
                       const std::string& name,
                       Index indent=0,
                       const std::string& prefix="") const;
    virtual void Print(const Journalist& jnlst,
                       EJournalLevel level,
                       EJournalCategory category,
                       const std::string& name,
                       Index indent=0,
                       const std::string& prefix="") const;
    //@}

    /** Return the owner MatrixSpace*/
    inline
    SmartPtr<const MatrixSpace> OwnerSpace() const;

  protected:
    /** @name implementation methods (derived classes MUST
     *  overload these pure virtual protected methods.
     */
    //@{
    /** Matrix-vector multiply.  Computes y = alpha * Matrix * x +
     *  beta * y
     */
    virtual void MultVectorImpl(Number alpha, const Vector& x, Number beta, Vector& y) const =0;

    /** Matrix(transpose) vector multiply.
     * Computes y = alpha * Matrix^T * x  +  beta * y
     */
    virtual void TransMultVectorImpl(Number alpha, const Vector& x, Number beta, Vector& y) const =0;

    /** X = X + alpha*(Matrix S^{-1} Z).  Prototype for this
     *  specialize method is provided, but for efficient
     *  implementation it should be overloaded for the expansion matrix.
     */
    virtual void AddMSinvZImpl(Number alpha, const Vector& S, const Vector& Z,
                               Vector& X) const;

    /** X = S^{-1} (r + alpha*Z*M^Td).   Should be implemented
     *  efficiently for the ExpansionMatrix.
     */
    virtual void SinvBlrmZMTdBrImpl(Number alpha, const Vector& S,
                                    const Vector& R, const Vector& Z,
                                    const Vector& D, Vector& X) const;

    /** Method for determining if all stored numbers are valid (i.e.,
     *  no Inf or Nan). A default implementation always returning true
     *  is provided, but if possible it should be implemented. */
    virtual bool HasValidNumbersImpl() const
    {
      return true;
    }

    /** Compute the max-norm of the rows in the matrix.  The result is
     *  stored in rows_norms.  The vector is assumed to be
     *  initialized. */
    virtual void ComputeRowAMaxImpl(Vector& rows_norms, bool init) const = 0;
    /** Compute the max-norm of the columns in the matrix.  The result
     *  is stored in cols_norms.  The vector is assumed to be
     *  initialized. */
    virtual void ComputeColAMaxImpl(Vector& cols_norms, bool init) const = 0;

    /** Print detailed information about the matrix. */
    virtual void PrintImpl(const Journalist& jnlst,
                           EJournalLevel level,
                           EJournalCategory category,
                           const std::string& name,
                           Index indent,
                           const std::string& prefix) const =0;
    //@}

  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 */
    Matrix();

    /** Copy constructor */
    Matrix(const Matrix&);

    /** Overloaded Equals Operator */
    Matrix& operator=(const Matrix&);
    //@}

    const SmartPtr<const MatrixSpace> owner_space_;

    /**@name CachedResults data members */
    //@{
    mutable TaggedObject::Tag valid_cache_tag_;
    mutable bool cached_valid_;
    //@}
  };


  /** MatrixSpace base class, corresponding to the Matrix base class.
   *  For each Matrix implementation, a corresponding MatrixSpace has
   *  to be implemented.  A MatrixSpace is able to create new Matrices
   *  of a specific type.  The MatrixSpace should also store
   *  information that is common to all Matrices of that type.  For
   *  example, the dimensions of a Matrix is stored in the MatrixSpace
   *  base class.
   */
  class MatrixSpace : public ReferencedObject
  {
  public:
    /** @name Constructors/Destructors */
    //@{
    /** Constructor, given the number rows and columns of all matrices
     *  generated by this MatrixSpace.
     */
    MatrixSpace(Index nRows, Index nCols)
        :
        nRows_(nRows),
        nCols_(nCols)
    {}

    /** Destructor */
    virtual ~MatrixSpace()
    {}
    //@}

    /** Pure virtual method for creating a new Matrix of the
     *  corresponding type.
     */
    virtual Matrix* MakeNew() const=0;

    /** Accessor function for the number of rows. */
    Index NRows() const
    {
      return nRows_;
    }
    /** Accessor function for the number of columns. */
    Index NCols() const
    {
      return nCols_;
    }

    /** Method to test if a given matrix belongs to a particular
     *  matrix space.
     */
    bool IsMatrixFromSpace(const Matrix& matrix) const
    {
      return (matrix.OwnerSpace() == this);
    }

  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 */
    MatrixSpace();

    /** Copy constructor */
    MatrixSpace(const MatrixSpace&);

    /** Overloaded Equals Operator */
    MatrixSpace& operator=(const MatrixSpace&);
    //@}

    /** Number of rows for all matrices of this type. */
    const Index nRows_;
    /** Number of columns for all matrices of this type. */
    const Index nCols_;
  };


  /* Inline Methods */
  inline
  Index  Matrix::NRows() const
  {
    return owner_space_->NRows();
  }

  inline
  Index  Matrix::NCols() const
  {
    return owner_space_->NCols();
  }

  inline
  SmartPtr<const MatrixSpace> Matrix::OwnerSpace() const
  {
    return owner_space_;
  }

} // namespace Ipopt

// Macro definitions for debugging matrices
#if COIN_IPOPT_VERBOSITY == 0
# define DBG_PRINT_MATRIX(__verbose_level, __mat_name, __mat)
#else
# define DBG_PRINT_MATRIX(__verbose_level, __mat_name, __mat) \
   if (dbg_jrnl.Verbosity() >= (__verbose_level)) { \
      if (dbg_jrnl.Jnlst()!=NULL) { \
        (__mat).Print(dbg_jrnl.Jnlst(), \
        J_ERROR, J_DBG, \
        __mat_name, \
        dbg_jrnl.IndentationLevel()*2, \
        "# "); \
      } \
   }
#endif // #if COIN_IPOPT_VERBOSITY == 0

#endif