Ipopt Documentation  
IpVector.hpp
Go to the documentation of this file.
1 // Copyright (C) 2004, 2008 International Business Machines and others.
2 // All Rights Reserved.
3 // This code is published under the Eclipse Public License.
4 //
5 // Authors: Carl Laird, Andreas Waechter IBM 2004-08-13
6 
7 #ifndef __IPVECTOR_HPP__
8 #define __IPVECTOR_HPP__
9 
10 #include "IpTypes.hpp"
11 #include "IpTaggedObject.hpp"
12 #include "IpCachedResults.hpp"
13 #include "IpSmartPtr.hpp"
14 #include "IpJournalist.hpp"
15 #include "IpException.hpp"
16 
17 #include <vector>
18 
19 namespace Ipopt
20 {
21 
24 DECLARE_STD_EXCEPTION(UNIMPLEMENTED_LINALG_METHOD_CALLED);
25 
26 /* forward declarations */
27 class VectorSpace;
28 
48 {
49 public:
51 
56  inline Vector(
57  const VectorSpace* owner_space
58  );
59 
61  inline virtual ~Vector();
63 
65  inline Vector* MakeNew() const;
66 
68  inline Vector* MakeNewCopy() const;
69 
75 
77  inline void Copy(
78  const Vector& x
79  );
80 
82  void Scal(
83  Number alpha
84  );
85 
87  inline void Axpy(
88  Number alpha,
89  const Vector& x
90  );
91 
93  inline Number Dot(
94  const Vector& x
95  ) const;
96 
98  inline Number Nrm2() const;
99 
101  inline Number Asum() const;
102 
104  inline Number Amax() const;
106 
112 
114  inline void Set(
115  Number alpha
116  );
117 
119  inline void ElementWiseDivide(
120  const Vector& x
121  );
122 
124  inline void ElementWiseMultiply(
125  const Vector& x
126  );
127 
129  inline void ElementWiseMax(
130  const Vector& x
131  );
132 
134  inline void ElementWiseMin(
135  const Vector& x
136  );
137 
139  inline void ElementWiseReciprocal();
140 
142  inline void ElementWiseAbs();
143 
145  inline void ElementWiseSqrt();
146 
150  inline void ElementWiseSgn();
151 
153  inline void AddScalar(
154  Number scalar
155  );
156 
158  inline Number Max() const;
159 
161  inline Number Min() const;
162 
164  inline Number Sum() const;
165 
167  inline Number SumLogs() const;
169 
175 
180  inline void AddOneVector(
181  Number a,
182  const Vector& v1,
183  Number c
184  );
185 
190  inline void AddTwoVectors(
191  Number a,
192  const Vector& v1,
193  Number b,
194  const Vector& v2,
195  Number c
196  );
197 
202  inline Number FracToBound(
203  const Vector& delta,
204  Number tau
205  ) const;
206 
208  inline void AddVectorQuotient(
209  Number a,
210  const Vector& z,
211  const Vector& s,
212  Number c
213  );
215 
217  inline bool HasValidNumbers() const;
218 
220 
222  inline Index Dim() const;
223 
225  inline SmartPtr<const VectorSpace> OwnerSpace() const;
227 
233 
235  void Print(
237  EJournalLevel level,
238  EJournalCategory category,
239  const std::string& name,
240  Index indent = 0,
241  const std::string& prefix = ""
242  ) const;
243 
244  void Print(
245  const Journalist& jnlst,
246  EJournalLevel level,
247  EJournalCategory category,
248  const std::string& name,
249  Index indent = 0,
250  const std::string& prefix = ""
251  ) const;
253 
254 protected:
258 
260  virtual void CopyImpl(
261  const Vector& x
262  ) = 0;
263 
265  virtual void ScalImpl(
266  Number alpha
267  ) = 0;
268 
270  virtual void AxpyImpl(
271  Number alpha,
272  const Vector& x
273  ) = 0;
274 
276  virtual Number DotImpl(
277  const Vector& x
278  ) const = 0;
279 
281  virtual Number Nrm2Impl() const = 0;
282 
284  virtual Number AsumImpl() const = 0;
285 
287  virtual Number AmaxImpl() const = 0;
288 
290  virtual void SetImpl(
291  Number alpha
292  ) = 0;
293 
295  virtual void ElementWiseDivideImpl(
296  const Vector& x
297  ) = 0;
298 
300  virtual void ElementWiseMultiplyImpl(
301  const Vector& x
302  ) = 0;
303 
305  virtual void ElementWiseMaxImpl(
306  const Vector& x
307  ) = 0;
308 
310  virtual void ElementWiseMinImpl(
311  const Vector& x
312  ) = 0;
313 
315  virtual void ElementWiseReciprocalImpl() = 0;
316 
318  virtual void ElementWiseAbsImpl() = 0;
319 
321  virtual void ElementWiseSqrtImpl() = 0;
322 
324  virtual void ElementWiseSgnImpl() = 0;
325 
327  virtual void AddScalarImpl(
328  Number scalar
329  ) = 0;
330 
332  virtual Number MaxImpl() const = 0;
333 
335  virtual Number MinImpl() const = 0;
336 
338  virtual Number SumImpl() const = 0;
339 
341  virtual Number SumLogsImpl() const = 0;
342 
347  virtual void AddTwoVectorsImpl(
348  Number a,
349  const Vector& v1,
350  Number b,
351  const Vector& v2,
352  Number c
353  );
354 
356  virtual Number FracToBoundImpl(
357  const Vector& delta,
358  Number tau
359  ) const;
360 
362  virtual void AddVectorQuotientImpl(
363  Number a,
364  const Vector& z,
365  const Vector& s,
366  Number c
367  );
368 
372  virtual bool HasValidNumbersImpl() const;
373 
375  virtual void PrintImpl(
376  const Journalist& jnlst,
377  EJournalLevel level,
378  EJournalCategory category,
379  const std::string& name,
380  Index indent,
381  const std::string& prefix
382  ) const = 0;
384 
385 private:
394 
396  Vector();
397 
399  Vector(
400  const Vector&
401  );
402 
404  Vector& operator=(
405  const Vector&
406  );
408 
411 
413 
416 
419 
422 
425 
428 
431 
434 
437 
439  mutable bool cached_valid_;
440 
441  // AW: I removed this cache since it gets in the way for the
442  // quality function search
443  // /** Cache for FracToBound */
444  // mutable CachedResults<Number> frac_to_bound_cache_;
446 
447 };
448 
459 {
460 public:
462 
466  VectorSpace(
467  Index dim
468  );
469 
471  virtual ~VectorSpace()
472  { }
474 
478  virtual Vector* MakeNew() const = 0;
479 
481  Index Dim() const
482  {
483  return dim_;
484  }
485 
486 private:
495 
497  VectorSpace();
498 
500  VectorSpace(
501  const VectorSpace&
502  );
503 
505  VectorSpace& operator=(
506  const VectorSpace&
507  );
509 
511  const Index dim_;
512 };
513 
514 /* inline methods */
516 { }
517 
519  const VectorSpace* owner_space
520 )
521  : TaggedObject(),
522  owner_space_(owner_space),
523  dot_cache_(10),
524  nrm2_cache_tag_(0),
525  asum_cache_tag_(0),
526  amax_cache_tag_(0),
527  max_cache_tag_(0),
528  min_cache_tag_(0),
529  sum_cache_tag_(0),
530  sumlogs_cache_tag_(0),
531  cached_valid_(0)
532 {
534 }
535 
536 inline Vector* Vector::MakeNew() const
537 {
538  return owner_space_->MakeNew();
539 }
540 
542 {
543  // ToDo: We can probably copy also the cached values for Norms etc here
544  Vector* copy = MakeNew();
545  copy->Copy(*this);
546  return copy;
547 }
548 
549 inline void Vector::Copy(
550  const Vector& x
551 )
552 {
553  CopyImpl(x);
554  ObjectChanged();
555  // Also copy any cached scalar values from the original vector
556  // ToDo: Check if that is too much overhead
557  TaggedObject::Tag x_tag = x.GetTag();
558  if( x_tag == x.nrm2_cache_tag_ )
559  {
562  }
563  if( x_tag == x.asum_cache_tag_ )
564  {
567  }
568  if( x_tag == x.amax_cache_tag_ )
569  {
572  }
573  if( x_tag == x.max_cache_tag_ )
574  {
577  }
578  if( x_tag == x.min_cache_tag_ )
579  {
582  }
583  if( x_tag == x.sum_cache_tag_ )
584  {
587  }
588  if( x_tag == x.sumlogs_cache_tag_ )
589  {
592  }
593 }
594 
595 inline void Vector::Axpy(
596  Number alpha,
597  const Vector& x
598 )
599 {
600  AxpyImpl(alpha, x);
601  ObjectChanged();
602 }
603 
605  const Vector& x
606 ) const
607 {
608  // The current implementation of the caching doesn't allow to have
609  // a dependency of something with itself. Therefore, we use the
610  // Nrm2 method if the dot product is to be taken with the vector
611  // itself. Might be more efficient anyway.
612  if( this == &x )
613  {
614  Number nrm2 = Nrm2();
615  return nrm2 * nrm2;
616  }
617  Number retValue;
618  if( !dot_cache_.GetCachedResult2Dep(retValue, this, &x) )
619  {
620  retValue = DotImpl(x);
621  dot_cache_.AddCachedResult2Dep(retValue, this, &x);
622  }
623  return retValue;
624 }
625 
626 inline Number Vector::Nrm2() const
627 {
628  if( nrm2_cache_tag_ != GetTag() )
629  {
632  }
633  return cached_nrm2_;
634 }
635 
636 inline Number Vector::Asum() const
637 {
638  if( asum_cache_tag_ != GetTag() )
639  {
642  }
643  return cached_asum_;
644 }
645 
646 inline Number Vector::Amax() const
647 {
648  if( amax_cache_tag_ != GetTag() )
649  {
652  }
653  return cached_amax_;
654 }
655 
656 inline Number Vector::Sum() const
657 {
658  if( sum_cache_tag_ != GetTag() )
659  {
660  cached_sum_ = SumImpl();
662  }
663  return cached_sum_;
664 }
665 
666 inline Number Vector::SumLogs() const
667 {
668  if( sumlogs_cache_tag_ != GetTag() )
669  {
672  }
673  return cached_sumlogs_;
674 }
675 
677 {
679  ObjectChanged();
680 }
681 
682 inline void Vector::Set(
683  Number alpha
684 )
685 {
686  // Could initialize caches here
687  SetImpl(alpha);
688  ObjectChanged();
689 }
690 
692  const Vector& x
693 )
694 {
696  ObjectChanged();
697 }
698 
700  const Vector& x
701 )
702 {
704  ObjectChanged();
705 }
706 
708 {
710  ObjectChanged();
711 }
712 
714  const Vector& x
715 )
716 {
717  // Could initialize some caches here
719  ObjectChanged();
720 }
721 
723  const Vector& x
724 )
725 {
726  // Could initialize some caches here
728  ObjectChanged();
729 }
730 
732 {
733  // Could initialize some caches here
735  ObjectChanged();
736 }
737 
739 {
741  ObjectChanged();
742 }
743 
744 inline void Vector::AddScalar(
745  Number scalar)
746 {
747  // Could initialize some caches here
748  AddScalarImpl(scalar);
749  ObjectChanged();
750 }
751 
752 inline Number Vector::Max() const
753 {
754  if( max_cache_tag_ != GetTag() )
755  {
756  cached_max_ = MaxImpl();
758  }
759  return cached_max_;
760 }
761 
762 inline Number Vector::Min() const
763 {
764  if( min_cache_tag_ != GetTag() )
765  {
766  cached_min_ = MinImpl();
768  }
769  return cached_min_;
770 }
771 
773  Number a,
774  const Vector& v1,
775  Number c
776 )
777 {
778  AddTwoVectors(a, v1, 0., v1, c);
779 }
780 
782  Number a,
783  const Vector& v1,
784  Number b,
785  const Vector& v2,
786  Number c
787 )
788 {
789  AddTwoVectorsImpl(a, v1, b, v2, c);
790  ObjectChanged();
791 }
792 
794  const Vector& delta,
795  Number tau
796 ) const
797 {
798  /* AW: I avoid the caching here, since it leads to overhead in the
799  quality function search. Caches for this are in
800  CalculatedQuantities.
801  Number retValue;
802  std::vector<const TaggedObject*> tdeps(1);
803  tdeps[0] = &delta;
804  std::vector<Number> sdeps(1);
805  sdeps[0] = tau;
806  if (!frac_to_bound_cache_.GetCachedResult(retValue, tdeps, sdeps)) {
807  retValue = FracToBoundImpl(delta, tau);
808  frac_to_bound_cache_.AddCachedResult(retValue, tdeps, sdeps);
809  }
810  return retValue;
811  */
812  return FracToBoundImpl(delta, tau);
813 }
814 
816  Number a,
817  const Vector& z,
818  const Vector& s,
819  Number c
820 )
821 {
822  AddVectorQuotientImpl(a, z, s, c);
823  ObjectChanged();
824 }
825 
826 inline bool Vector::HasValidNumbers() const
827 {
828  if( valid_cache_tag_ != GetTag() )
829  {
832  }
833  return cached_valid_;
834 }
835 
836 inline Index Vector::Dim() const
837 {
838  return owner_space_->Dim();
839 }
840 
842 {
843  return owner_space_;
844 }
845 
847  Index dim
848 )
849  : dim_(dim)
850 { }
851 
852 } // namespace Ipopt
853 
854 // Macro definitions for debugging vectors
855 #if IPOPT_VERBOSITY == 0
856 # define DBG_PRINT_VECTOR(__verbose_level, __vec_name, __vec)
857 #else
858 # define DBG_PRINT_VECTOR(__verbose_level, __vec_name, __vec) \
859  if (dbg_jrnl.Verbosity() >= (__verbose_level)) { \
860  if (dbg_jrnl.Jnlst()!=NULL) { \
861  (__vec).Print(dbg_jrnl.Jnlst(), \
862  J_ERROR, J_DBG, \
863  __vec_name, \
864  dbg_jrnl.IndentationLevel()*2, \
865  "# "); \
866  } \
867  }
868 #endif //if IPOPT_VERBOSITY == 0
869 
870 #endif
TaggedObject::Tag valid_cache_tag_
Definition: IpVector.hpp:438
virtual void CopyImpl(const Vector &x)=0
Copy the data of the vector x into this vector (DCOPY).
virtual void ElementWiseMinImpl(const Vector &x)=0
Element-wise min against entries in x.
bool IsValid(const SmartPtr< U > &smart_ptr)
Definition: IpSmartPtr.hpp:674
TaggedObject::Tag max_cache_tag_
Definition: IpVector.hpp:426
virtual void ElementWiseMaxImpl(const Vector &x)=0
Element-wise max against entries in x.
const SmartPtr< const VectorSpace > owner_space_
Vector Space.
Definition: IpVector.hpp:410
void ElementWiseAbs()
Absolute values of the entries in the vector.
Definition: IpVector.hpp:731
Index Max(Index a, Index b)
Definition: IpUtils.hpp:17
Vector()
Default constructor.
Number cached_sumlogs_
Definition: IpVector.hpp:436
bool GetCachedResult2Dep(T &retResult, const TaggedObject *dependent1, const TaggedObject *dependent2)
Method for retrieving a cached result, proving two dependencies as a TaggedObject explicitly...
TaggedObject::Tag sumlogs_cache_tag_
Definition: IpVector.hpp:435
virtual Number MaxImpl() const =0
Max value in the vector.
virtual void AddVectorQuotientImpl(Number a, const Vector &z, const Vector &s, Number c)
Add the quotient of two vectors.
virtual Number AsumImpl() const =0
Computes the 1-norm of this vector (DASUM)
Number Dot(const Vector &x) const
Computes inner product of vector x with this (DDOT)
Definition: IpVector.hpp:604
Vector * MakeNewCopy() const
Create new Vector of the same type and copy the data over.
Definition: IpVector.hpp:541
DECLARE_STD_EXCEPTION(FATAL_ERROR_IN_LINEAR_SOLVER)
double Number
Type of all numbers.
Definition: IpTypes.hpp:15
void ObjectChanged()
Objects derived from TaggedObject MUST call this method every time their internal state changes to up...
Vector Base Class.
Definition: IpVector.hpp:47
void ElementWiseMin(const Vector &x)
Element-wise min against entries in x.
Definition: IpVector.hpp:722
virtual void ElementWiseSqrtImpl()=0
Take elementwise square-root of the elements of the vector.
virtual void AddScalarImpl(Number scalar)=0
Add scalar to every component of vector.
EJournalLevel
Print Level Enum.
virtual bool HasValidNumbersImpl() const
Method for determining if all stored numbers are valid (i.e., no Inf or Nan).
void ElementWiseDivide(const Vector &x)
Element-wise division .
Definition: IpVector.hpp:691
bool HasValidNumbers() const
Method for determining if all stored numbers are valid (i.e., no Inf or Nan).
Definition: IpVector.hpp:826
void AddCachedResult2Dep(const T &result, const TaggedObject *dependent1, const TaggedObject *dependent2)
Method for adding a result to the cache, proving two dependencies as a TaggedObject explicitly...
virtual void ElementWiseSgnImpl()=0
Replaces entries with sgn of the entry.
TaggedObject class.
void Axpy(Number alpha, const Vector &x)
Add the multiple alpha of vector x to this vector (DAXPY)
Definition: IpVector.hpp:595
This file contains a base class for all exceptions and a set of macros to help with exceptions...
Template class for Smart Pointers.
Definition: IpSmartPtr.hpp:171
virtual void ElementWiseReciprocalImpl()=0
Reciprocates the elements of the vector.
Storing the reference count of all the smart pointers that currently reference it.
void ElementWiseMax(const Vector &x)
Element-wise max against entries in x.
Definition: IpVector.hpp:713
void AddScalar(Number scalar)
Add scalar to every vector component.
Definition: IpVector.hpp:744
VectorSpace base class, corresponding to the Vector base class.
Definition: IpVector.hpp:458
TaggedObject::Tag sum_cache_tag_
Definition: IpVector.hpp:432
Tag GetTag() const
Users of TaggedObjects call this to update their own internal tags every time they perform the expens...
virtual ~Vector()
Destructor.
Definition: IpVector.hpp:515
virtual Number SumImpl() const =0
Sum of entries in the vector.
void Set(Number alpha)
Set each element in the vector to the scalar alpha.
Definition: IpVector.hpp:682
Number Nrm2() const
Computes the 2-norm of this vector (DNRM2)
Definition: IpVector.hpp:626
Index Min(Index a, Index b)
Definition: IpUtils.hpp:48
void AddVectorQuotient(Number a, const Vector &z, const Vector &s, Number c)
Add the quotient of two vectors, y = a * z/s + c * y.
Definition: IpVector.hpp:815
void Copy(const Vector &x)
Copy the data of the vector x into this vector (DCOPY).
Definition: IpVector.hpp:549
Number cached_asum_
Definition: IpVector.hpp:421
virtual void ElementWiseMultiplyImpl(const Vector &x)=0
Element-wise multiplication .
Number Min() const
Returns the minimum value in the vector.
Definition: IpVector.hpp:762
SmartPtr< const VectorSpace > OwnerSpace() const
Return the owner VectorSpace.
Definition: IpVector.hpp:841
VectorSpace()
Default constructor.
Number cached_min_
Definition: IpVector.hpp:430
Number Amax() const
Computes the max-norm of this vector (based on IDAMAX)
Definition: IpVector.hpp:646
Number SumLogs() const
Returns the sum of the logs of each vector entry.
Definition: IpVector.hpp:666
Number cached_max_
Definition: IpVector.hpp:427
virtual void ElementWiseAbsImpl()=0
Take elementwise absolute values of the elements of the vector.
int Index
Type of all indices of vectors, matrices etc.
Definition: IpTypes.hpp:17
virtual Number AmaxImpl() const =0
Computes the max-norm of this vector (based on IDAMAX)
void AddTwoVectors(Number a, const Vector &v1, Number b, const Vector &v2, Number c)
Add two vectors, y = a * v1 + b * v2 + c * y.
Definition: IpVector.hpp:781
Number cached_sum_
Definition: IpVector.hpp:433
#define DBG_ASSERT(test)
Definition: IpDebug.hpp:27
TaggedObject::Tag amax_cache_tag_
Definition: IpVector.hpp:423
Number Asum() const
Computes the 1-norm of this vector (DASUM)
Definition: IpVector.hpp:636
virtual Number Nrm2Impl() const =0
Computes the 2-norm of this vector (DNRM2)
Index Dim() const
Dimension of the Vector.
Definition: IpVector.hpp:836
unsigned int Tag
Type for the Tag values.
virtual void SetImpl(Number alpha)=0
Set each element in the vector to the scalar alpha.
TaggedObject::Tag nrm2_cache_tag_
Definition: IpVector.hpp:417
#define IPOPTLIB_EXPORT
TaggedObject::Tag min_cache_tag_
Definition: IpVector.hpp:429
Class responsible for all message output.
virtual void AddTwoVectorsImpl(Number a, const Vector &v1, Number b, const Vector &v2, Number c)
Add two vectors (a * v1 + b * v2).
virtual ~VectorSpace()
Destructor.
Definition: IpVector.hpp:471
Vector * MakeNew() const
Create new Vector of the same type with uninitialized data.
Definition: IpVector.hpp:536
virtual Number SumLogsImpl() const =0
Sum of logs of entries in the vector.
virtual void AxpyImpl(Number alpha, const Vector &x)=0
Add the multiple alpha of vector x to this vector (DAXPY)
TaggedObject::Tag asum_cache_tag_
Definition: IpVector.hpp:420
void ElementWiseMultiply(const Vector &x)
Element-wise multiplication .
Definition: IpVector.hpp:699
Number cached_nrm2_
Definition: IpVector.hpp:418
const Index dim_
Dimension of the vectors in this vector space.
Definition: IpVector.hpp:511
Index Dim() const
Accessor function for the dimension of the vectors of this type.
Definition: IpVector.hpp:481
Number Sum() const
Returns the sum of the vector entries.
Definition: IpVector.hpp:656
void AddOneVector(Number a, const Vector &v1, Number c)
Add one vector, y = a * v1 + c * y.
Definition: IpVector.hpp:772
Number FracToBound(const Vector &delta, Number tau) const
Fraction to the boundary parameter.
Definition: IpVector.hpp:793
Number cached_amax_
Definition: IpVector.hpp:424
void ElementWiseSqrt()
Element-wise square root of the entries in the vector.
Definition: IpVector.hpp:738
void ElementWiseSgn()
Replaces the vector values with their sgn values ( -1 if x_i < 0, 0 if x_i == 0, and 1 if x_i > 0) ...
Definition: IpVector.hpp:676
EJournalCategory
Category Selection Enum.
virtual void ElementWiseDivideImpl(const Vector &x)=0
Element-wise division .
virtual Number DotImpl(const Vector &x) const =0
Computes inner product of vector x with this (DDOT)
CachedResults< Number > dot_cache_
Cache for dot products.
Definition: IpVector.hpp:415
virtual Number FracToBoundImpl(const Vector &delta, Number tau) const
Fraction to boundary parameter.
Number Max() const
Returns the maximum value in the vector.
Definition: IpVector.hpp:752
void ElementWiseReciprocal()
Reciprocates the entries in the vector.
Definition: IpVector.hpp:707
bool cached_valid_
Definition: IpVector.hpp:439
virtual Number MinImpl() const =0
Min number in the vector.