summaryrefslogtreecommitdiff
path: root/thirdparty/linux/include/coin1/IpCompoundVector.hpp
blob: a4c52abdbe850788d43c489b242d985ffa2dc787 (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
// Copyright (C) 2004, 2006 International Business Machines and others.
// All Rights Reserved.
// This code is published under the Eclipse Public License.
//
// $Id: IpCompoundVector.hpp 2269 2013-05-05 11:32:40Z stefan $
//
// Authors:  Carl Laird, Andreas Waechter     IBM    2004-08-13

#ifndef __IPCOMPOUNDVECTOR_HPP__
#define __IPCOMPOUNDVECTOR_HPP__

#include "IpUtils.hpp"
#include "IpVector.hpp"
#include <vector>

namespace Ipopt
{

  /* forward declarations */
  class CompoundVectorSpace;

  /** Class of Vectors consisting of other vectors.  This vector is a
   *  vector that consists of zero, one or more Vector's which are
   *  stacked on each others: \f$ x_{\rm compound} =
   *  \left(\begin{array}{c}x_0\\\dots\\x_{{\rm
   *  ncomps} - 1}\end{array}\right)\f$.  The individual components can be
   *  associated to different VectorSpaces.  The individual components
   *  can also be const and non-const Vectors.
   */
  class CompoundVector : public Vector
  {
  public:
    /**@name Constructors/Destructors */
    //@{
    /** Constructor, given the corresponding CompoundVectorSpace.
     *  Before this constructor can be called, all components of the
     *  CompoundVectorSpace have to be set, so that the constructors
     *  for the individual components can be called.  If the flag
     *  create_new is true, then the individual components of the new
     *  CompoundVector are initialized with the MakeNew methods of
     *  each VectorSpace (and are non-const).  Otherwise, the
     *  individual components can later be set using the SetComp and
     *  SetCompNonConst method.
     */
    CompoundVector(const CompoundVectorSpace* owner_space, bool create_new);

    /** Default destructor */
    virtual ~CompoundVector();
    //@}

    /** Method for setting the pointer for a component that is a const
     *  Vector
     */
    void SetComp(Index icomp, const Vector& vec);

    /** Method for setting the pointer for a component that is a
     *  non-const Vector
     */
    void SetCompNonConst(Index icomp, Vector& vec);

    /** Number of components of this compound vector */
    inline Index NComps() const;

    /** Check if a particular component is const or not */
    bool IsCompConst(Index i) const
    {
      DBG_ASSERT(i > 0 && i < NComps());
      DBG_ASSERT(IsValid(comps_[i]) || IsValid(const_comps_[i]));
      if (IsValid(const_comps_[i])) {
        return true;
      }
      return false;
    }

    /** Check if a particular component is null or not */
    bool IsCompNull(Index i) const
    {
      DBG_ASSERT(i >= 0 && i < NComps());
      if (IsValid(comps_[i]) || IsValid(const_comps_[i])) {
        return false;
      }
      return true;
    }

    /** Return a particular component (const version) */
    SmartPtr<const Vector> GetComp(Index i) const
    {
      return ConstComp(i);
    }

    /** Return a particular component (non-const version).  Note that
     *  calling this method with mark the CompoundVector as changed.
     *  Therefore, only use this method if you are intending to change
     *  the Vector that you receive.
     */
    SmartPtr<Vector> GetCompNonConst(Index i)
    {
      ObjectChanged();
      return Comp(i);
    }

  protected:
    /** @name Overloaded methods from Vector base class */
    //@{
    /** Copy the data of the vector x into this vector (DCOPY). */
    virtual void CopyImpl(const Vector& x);

    /** Scales the vector by scalar alpha (DSCAL) */
    virtual void ScalImpl(Number alpha);

    /** Add the multiple alpha of vector x to this vector (DAXPY) */
    virtual void AxpyImpl(Number alpha, const Vector &x);

    /** Computes inner product of vector x with this (DDOT) */
    virtual Number DotImpl(const Vector &x) const;

    /** Computes the 2-norm of this vector (DNRM2) */
    virtual Number Nrm2Impl() const;

    /** Computes the 1-norm of this vector (DASUM) */
    virtual Number AsumImpl() const;

    /** Computes the max-norm of this vector (based on IDAMAX) */
    virtual Number AmaxImpl() const;

    /** Set each element in the vector to the scalar alpha. */
    virtual void SetImpl(Number value);

    /** Element-wise division  \f$y_i \gets y_i/x_i\f$.*/
    virtual void ElementWiseDivideImpl(const Vector& x);

    /** Element-wise multiplication \f$y_i \gets y_i*x_i\f$.*/
    virtual void ElementWiseMultiplyImpl(const Vector& x);

    /** Element-wise max against entries in x */
    virtual void ElementWiseMaxImpl(const Vector& x);

    /** Element-wise min against entries in x */
    virtual void ElementWiseMinImpl(const Vector& x);

    /** Element-wise reciprocal */
    virtual void ElementWiseReciprocalImpl();

    /** Element-wise absolute values */
    virtual void ElementWiseAbsImpl();

    /** Element-wise square-root */
    virtual void ElementWiseSqrtImpl();

    /** Replaces entries with sgn of the entry */
    virtual void ElementWiseSgnImpl();

    /** Add scalar to every component of the vector.*/
    virtual void AddScalarImpl(Number scalar);

    /** Max value in the vector */
    virtual Number MaxImpl() const;

    /** Min value in the vector */
    virtual Number MinImpl() const;

    /** Computes the sum of the lements of vector */
    virtual Number SumImpl() const;

    /** Computes the sum of the logs of the elements of vector */
    virtual Number SumLogsImpl() const;

    /** @name Implemented specialized functions */
    //@{
    /** Add two vectors (a * v1 + b * v2).  Result is stored in this
    vector. */
    void AddTwoVectorsImpl(Number a, const Vector& v1,
                           Number b, const Vector& v2, Number c);
    /** Fraction to the boundary parameter. */
    Number FracToBoundImpl(const Vector& delta, Number tau) const;
    /** Add the quotient of two vectors, y = a * z/s + c * y. */
    void AddVectorQuotientImpl(Number a, const Vector& z, const Vector& s,
                               Number c);
    //@}

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

    /** @name Output methods */
    //@{
    /* Print the entire vector with padding */
    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 */
    CompoundVector();

    /** Copy Constructor */
    CompoundVector(const CompoundVector&);

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

    /** Components of the compound vector.  The components
     *  are stored by SmartPtrs in a std::vector
     */
    std::vector< SmartPtr<Vector> > comps_;
    std::vector< SmartPtr<const Vector> > const_comps_;

    const CompoundVectorSpace* owner_space_;

    bool vectors_valid_;

    bool VectorsValid();

    inline const Vector* ConstComp(Index i) const;

    inline Vector* Comp(Index i);
  };

  /** This vectors space is the vector space for CompoundVector.
   *  Before a CompoundVector can be created, all components of this
   *  CompoundVectorSpace have to be set.  When calling the constructor,
   *  the number of component has to be specified.  The individual
   *  VectorSpaces can be set with the SetComp method.
   */
  class CompoundVectorSpace : public VectorSpace
  {
  public:
    /** @name Constructors/Destructors. */
    //@{
    /** Constructor, has to be given the number of components and the
     *  total dimension of all components combined. */
    CompoundVectorSpace(Index ncomp_spaces, Index total_dim);

    /** Destructor */
    ~CompoundVectorSpace()
    {}
    //@}

    /** Method for setting the individual component VectorSpaces */
    virtual void SetCompSpace(Index icomp                  /** Number of the component to be set */ ,
                              const VectorSpace& vec_space /** VectorSpace for component icomp */
                             );

    /** Method for obtaining an individual component VectorSpace */
    SmartPtr<const VectorSpace> GetCompSpace(Index icomp) const;

    /** Accessor method to obtain the number of components */
    Index NCompSpaces() const
    {
      return ncomp_spaces_;
    }

    /** Method for creating a new vector of this specific type. */
    virtual CompoundVector* MakeNewCompoundVector(bool create_new = true) const
    {
      return new CompoundVector(this, create_new);
    }

    /** Overloaded MakeNew method for the VectorSpace base class.
     */
    virtual Vector* MakeNew() const
    {
      return MakeNewCompoundVector();
    }

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

    /** Copy Constructor */
    CompoundVectorSpace(const CompoundVectorSpace&);

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

    /** Number of components */
    const Index ncomp_spaces_;

    /** std::vector of vector spaces for the components */
    std::vector< SmartPtr<const VectorSpace> > comp_spaces_;
  };

  /* inline methods */
  inline
  Index CompoundVector::NComps() const
  {
    return owner_space_->NCompSpaces();
  }

  inline
  const Vector* CompoundVector::ConstComp(Index i) const
  {
    DBG_ASSERT(i < NComps());
    DBG_ASSERT(IsValid(comps_[i]) || IsValid(const_comps_[i]));
    if (IsValid(comps_[i])) {
      return GetRawPtr(comps_[i]);
    }
    else if (IsValid(const_comps_[i])) {
      return GetRawPtr(const_comps_[i]);
    }

    DBG_ASSERT(false && "shouldn't be here");
    return NULL;
  }

  inline
  Vector* CompoundVector::Comp(Index i)
  {
    DBG_ASSERT(i < NComps());
    DBG_ASSERT(IsValid(comps_[i]));
    return GetRawPtr(comps_[i]);
  }

} // namespace Ipopt

#endif