IpCompoundVector.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // Copyright (C) 2004, 2006 International Business Machines and others.
  2. // All Rights Reserved.
  3. // This code is published under the Eclipse Public License.
  4. //
  5. // $Id: IpCompoundVector.hpp 1861 2010-12-21 21:34:47Z andreasw $
  6. //
  7. // Authors: Carl Laird, Andreas Waechter IBM 2004-08-13
  8. #ifndef __IPCOMPOUNDVECTOR_HPP__
  9. #define __IPCOMPOUNDVECTOR_HPP__
  10. #include "IpUtils.hpp"
  11. #include "IpVector.hpp"
  12. #include <vector>
  13. namespace Ipopt
  14. {
  15. /* forward declarations */
  16. class CompoundVectorSpace;
  17. /** Class of Vectors consisting of other vectors. This vector is a
  18. * vector that consists of zero, one or more Vector's which are
  19. * stacked on each others: \f$ x_{\rm compound} =
  20. * \left(\begin{array}{c}x_0\\\dots\\x_{{\rm
  21. * ncomps} - 1}\end{array}\right)\f$. The individual components can be
  22. * associated to different VectorSpaces. The individual components
  23. * can also be const and non-const Vectors.
  24. */
  25. class CompoundVector : public Vector
  26. {
  27. public:
  28. /**@name Constructors/Destructors */
  29. //@{
  30. /** Constructor, given the corresponding CompoundVectorSpace.
  31. * Before this constructor can be called, all components of the
  32. * CompoundVectorSpace have to be set, so that the constructors
  33. * for the individual components can be called. If the flag
  34. * create_new is true, then the individual components of the new
  35. * CompoundVector are initialized with the MakeNew methods of
  36. * each VectorSpace (and are non-const). Otherwise, the
  37. * individual components can later be set using the SetComp and
  38. * SetCompNonConst method.
  39. */
  40. CompoundVector(const CompoundVectorSpace* owner_space, bool create_new);
  41. /** Default destructor */
  42. virtual ~CompoundVector();
  43. //@}
  44. /** Method for setting the pointer for a component that is a const
  45. * Vector
  46. */
  47. void SetComp(Index icomp, const Vector& vec);
  48. /** Method for setting the pointer for a component that is a
  49. * non-const Vector
  50. */
  51. void SetCompNonConst(Index icomp, Vector& vec);
  52. /** Number of components of this compound vector */
  53. inline Index NComps() const;
  54. /** Check if a particular component is const or not */
  55. bool IsCompConst(Index i) const
  56. {
  57. DBG_ASSERT(i > 0 && i < NComps());
  58. DBG_ASSERT(IsValid(comps_[i]) || IsValid(const_comps_[i]));
  59. if (IsValid(const_comps_[i])) {
  60. return true;
  61. }
  62. return false;
  63. }
  64. /** Check if a particular component is null or not */
  65. bool IsCompNull(Index i) const
  66. {
  67. DBG_ASSERT(i >= 0 && i < NComps());
  68. if (IsValid(comps_[i]) || IsValid(const_comps_[i])) {
  69. return false;
  70. }
  71. return true;
  72. }
  73. /** Return a particular component (const version) */
  74. SmartPtr<const Vector> GetComp(Index i) const
  75. {
  76. return ConstComp(i);
  77. }
  78. /** Return a particular component (non-const version). Note that
  79. * calling this method with mark the CompoundVector as changed.
  80. * Therefore, only use this method if you are intending to change
  81. * the Vector that you receive.
  82. */
  83. SmartPtr<Vector> GetCompNonConst(Index i)
  84. {
  85. ObjectChanged();
  86. return Comp(i);
  87. }
  88. protected:
  89. /** @name Overloaded methods from Vector base class */
  90. //@{
  91. /** Copy the data of the vector x into this vector (DCOPY). */
  92. virtual void CopyImpl(const Vector& x);
  93. /** Scales the vector by scalar alpha (DSCAL) */
  94. virtual void ScalImpl(Number alpha);
  95. /** Add the multiple alpha of vector x to this vector (DAXPY) */
  96. virtual void AxpyImpl(Number alpha, const Vector &x);
  97. /** Computes inner product of vector x with this (DDOT) */
  98. virtual Number DotImpl(const Vector &x) const;
  99. /** Computes the 2-norm of this vector (DNRM2) */
  100. virtual Number Nrm2Impl() const;
  101. /** Computes the 1-norm of this vector (DASUM) */
  102. virtual Number AsumImpl() const;
  103. /** Computes the max-norm of this vector (based on IDAMAX) */
  104. virtual Number AmaxImpl() const;
  105. /** Set each element in the vector to the scalar alpha. */
  106. virtual void SetImpl(Number value);
  107. /** Element-wise division \f$y_i \gets y_i/x_i\f$.*/
  108. virtual void ElementWiseDivideImpl(const Vector& x);
  109. /** Element-wise multiplication \f$y_i \gets y_i*x_i\f$.*/
  110. virtual void ElementWiseMultiplyImpl(const Vector& x);
  111. /** Element-wise max against entries in x */
  112. virtual void ElementWiseMaxImpl(const Vector& x);
  113. /** Element-wise min against entries in x */
  114. virtual void ElementWiseMinImpl(const Vector& x);
  115. /** Element-wise reciprocal */
  116. virtual void ElementWiseReciprocalImpl();
  117. /** Element-wise absolute values */
  118. virtual void ElementWiseAbsImpl();
  119. /** Element-wise square-root */
  120. virtual void ElementWiseSqrtImpl();
  121. /** Replaces entries with sgn of the entry */
  122. virtual void ElementWiseSgnImpl();
  123. /** Add scalar to every component of the vector.*/
  124. virtual void AddScalarImpl(Number scalar);
  125. /** Max value in the vector */
  126. virtual Number MaxImpl() const;
  127. /** Min value in the vector */
  128. virtual Number MinImpl() const;
  129. /** Computes the sum of the lements of vector */
  130. virtual Number SumImpl() const;
  131. /** Computes the sum of the logs of the elements of vector */
  132. virtual Number SumLogsImpl() const;
  133. /** @name Implemented specialized functions */
  134. //@{
  135. /** Add two vectors (a * v1 + b * v2). Result is stored in this
  136. vector. */
  137. void AddTwoVectorsImpl(Number a, const Vector& v1,
  138. Number b, const Vector& v2, Number c);
  139. /** Fraction to the boundary parameter. */
  140. Number FracToBoundImpl(const Vector& delta, Number tau) const;
  141. /** Add the quotient of two vectors, y = a * z/s + c * y. */
  142. void AddVectorQuotientImpl(Number a, const Vector& z, const Vector& s,
  143. Number c);
  144. //@}
  145. /** Method for determining if all stored numbers are valid (i.e.,
  146. * no Inf or Nan). */
  147. virtual bool HasValidNumbersImpl() const;
  148. /** @name Output methods */
  149. //@{
  150. /* Print the entire vector with padding */
  151. virtual void PrintImpl(const Journalist& jnlst,
  152. EJournalLevel level,
  153. EJournalCategory category,
  154. const std::string& name,
  155. Index indent,
  156. const std::string& prefix) const;
  157. //@}
  158. private:
  159. /**@name Default Compiler Generated Methods
  160. * (Hidden to avoid implicit creation/calling).
  161. * These methods are not implemented and
  162. * we do not want the compiler to implement
  163. * them for us, so we declare them private
  164. * and do not define them. This ensures that
  165. * they will not be implicitly created/called.
  166. */
  167. //@{
  168. /** Default Constructor */
  169. CompoundVector();
  170. /** Copy Constructor */
  171. CompoundVector(const CompoundVector&);
  172. /** Overloaded Equals Operator */
  173. void operator=(const CompoundVector&);
  174. //@}
  175. /** Components of the compound vector. The components
  176. * are stored by SmartPtrs in a std::vector
  177. */
  178. std::vector< SmartPtr<Vector> > comps_;
  179. std::vector< SmartPtr<const Vector> > const_comps_;
  180. const CompoundVectorSpace* owner_space_;
  181. bool vectors_valid_;
  182. bool VectorsValid();
  183. inline const Vector* ConstComp(Index i) const;
  184. inline Vector* Comp(Index i);
  185. };
  186. /** This vectors space is the vector space for CompoundVector.
  187. * Before a CompoundVector can be created, all components of this
  188. * CompoundVectorSpace have to be set. When calling the constructor,
  189. * the number of component has to be specified. The individual
  190. * VectorSpaces can be set with the SetComp method.
  191. */
  192. class CompoundVectorSpace : public VectorSpace
  193. {
  194. public:
  195. /** @name Constructors/Destructors. */
  196. //@{
  197. /** Constructor, has to be given the number of components and the
  198. * total dimension of all components combined. */
  199. CompoundVectorSpace(Index ncomp_spaces, Index total_dim);
  200. /** Destructor */
  201. ~CompoundVectorSpace()
  202. {}
  203. //@}
  204. /** Method for setting the individual component VectorSpaces */
  205. virtual void SetCompSpace(Index icomp /** Number of the component to be set */ ,
  206. const VectorSpace& vec_space /** VectorSpace for component icomp */
  207. );
  208. /** Method for obtaining an individual component VectorSpace */
  209. SmartPtr<const VectorSpace> GetCompSpace(Index icomp) const;
  210. /** Accessor method to obtain the number of components */
  211. Index NCompSpaces() const
  212. {
  213. return ncomp_spaces_;
  214. }
  215. /** Method for creating a new vector of this specific type. */
  216. virtual CompoundVector* MakeNewCompoundVector(bool create_new = true) const
  217. {
  218. return new CompoundVector(this, create_new);
  219. }
  220. /** Overloaded MakeNew method for the VectorSpace base class.
  221. */
  222. virtual Vector* MakeNew() const
  223. {
  224. return MakeNewCompoundVector();
  225. }
  226. private:
  227. /**@name Default Compiler Generated Methods
  228. * (Hidden to avoid implicit creation/calling).
  229. * These methods are not implemented and
  230. * we do not want the compiler to implement
  231. * them for us, so we declare them private
  232. * and do not define them. This ensures that
  233. * they will not be implicitly created/called. */
  234. //@{
  235. /** Default constructor */
  236. CompoundVectorSpace();
  237. /** Copy Constructor */
  238. CompoundVectorSpace(const CompoundVectorSpace&);
  239. /** Overloaded Equals Operator */
  240. CompoundVectorSpace& operator=(const CompoundVectorSpace&);
  241. //@}
  242. /** Number of components */
  243. const Index ncomp_spaces_;
  244. /** std::vector of vector spaces for the components */
  245. std::vector< SmartPtr<const VectorSpace> > comp_spaces_;
  246. };
  247. /* inline methods */
  248. inline
  249. Index CompoundVector::NComps() const
  250. {
  251. return owner_space_->NCompSpaces();
  252. }
  253. inline
  254. const Vector* CompoundVector::ConstComp(Index i) const
  255. {
  256. DBG_ASSERT(i < NComps());
  257. DBG_ASSERT(IsValid(comps_[i]) || IsValid(const_comps_[i]));
  258. if (IsValid(comps_[i])) {
  259. return GetRawPtr(comps_[i]);
  260. }
  261. else if (IsValid(const_comps_[i])) {
  262. return GetRawPtr(const_comps_[i]);
  263. }
  264. DBG_ASSERT(false && "shouldn't be here");
  265. return NULL;
  266. }
  267. inline
  268. Vector* CompoundVector::Comp(Index i)
  269. {
  270. DBG_ASSERT(i < NComps());
  271. DBG_ASSERT(IsValid(comps_[i]));
  272. return GetRawPtr(comps_[i]);
  273. }
  274. } // namespace Ipopt
  275. #endif