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 
133  inline void ElementWiseSelect(
134  const Vector& x
135  );
136 
138  inline void ElementWiseMax(
139  const Vector& x
140  );
141 
143  inline void ElementWiseMin(
144  const Vector& x
145  );
146 
148  inline void ElementWiseReciprocal();
149 
151  inline void ElementWiseAbs();
152 
154  inline void ElementWiseSqrt();
155 
159  inline void ElementWiseSgn();
160 
162  inline void AddScalar(
163  Number scalar
164  );
165 
167  inline Number Max() const;
168 
170  inline Number Min() const;
171 
173  inline Number Sum() const;
174 
176  inline Number SumLogs() const;
178 
184 
189  inline void AddOneVector(
190  Number a,
191  const Vector& v1,
192  Number c
193  );
194 
199  inline void AddTwoVectors(
200  Number a,
201  const Vector& v1,
202  Number b,
203  const Vector& v2,
204  Number c
205  );
206 
211  inline Number FracToBound(
212  const Vector& delta,
213  Number tau
214  ) const;
215 
217  inline void AddVectorQuotient(
218  Number a,
219  const Vector& z,
220  const Vector& s,
221  Number c
222  );
224 
226  inline bool HasValidNumbers() const;
227 
229 
231  inline Index Dim() const;
232 
234  inline SmartPtr<const VectorSpace> OwnerSpace() const;
236 
242 
244  void Print(
246  EJournalLevel level,
247  EJournalCategory category,
248  const std::string& name,
249  Index indent = 0,
250  const std::string& prefix = ""
251  ) const;
252 
253  void Print(
254  const Journalist& jnlst,
255  EJournalLevel level,
256  EJournalCategory category,
257  const std::string& name,
258  Index indent = 0,
259  const std::string& prefix = ""
260  ) const;
262 
263 protected:
267 
269  virtual void CopyImpl(
270  const Vector& x
271  ) = 0;
272 
274  virtual void ScalImpl(
275  Number alpha
276  ) = 0;
277 
279  virtual void AxpyImpl(
280  Number alpha,
281  const Vector& x
282  ) = 0;
283 
285  virtual Number DotImpl(
286  const Vector& x
287  ) const = 0;
288 
290  virtual Number Nrm2Impl() const = 0;
291 
293  virtual Number AsumImpl() const = 0;
294 
296  virtual Number AmaxImpl() const = 0;
297 
299  virtual void SetImpl(
300  Number alpha
301  ) = 0;
302 
304  virtual void ElementWiseDivideImpl(
305  const Vector& x
306  ) = 0;
307 
309  virtual void ElementWiseMultiplyImpl(
310  const Vector& x
311  ) = 0;
312 
314  virtual void ElementWiseSelectImpl(
315  const Vector& x
316  ) = 0;
317 
319  virtual void ElementWiseMaxImpl(
320  const Vector& x
321  ) = 0;
322 
324  virtual void ElementWiseMinImpl(
325  const Vector& x
326  ) = 0;
327 
329  virtual void ElementWiseReciprocalImpl() = 0;
330 
332  virtual void ElementWiseAbsImpl() = 0;
333 
335  virtual void ElementWiseSqrtImpl() = 0;
336 
338  virtual void ElementWiseSgnImpl() = 0;
339 
341  virtual void AddScalarImpl(
342  Number scalar
343  ) = 0;
344 
346  virtual Number MaxImpl() const = 0;
347 
349  virtual Number MinImpl() const = 0;
350 
352  virtual Number SumImpl() const = 0;
353 
355  virtual Number SumLogsImpl() const = 0;
356 
361  virtual void AddTwoVectorsImpl(
362  Number a,
363  const Vector& v1,
364  Number b,
365  const Vector& v2,
366  Number c
367  );
368 
370  virtual Number FracToBoundImpl(
371  const Vector& delta,
372  Number tau
373  ) const;
374 
376  virtual void AddVectorQuotientImpl(
377  Number a,
378  const Vector& z,
379  const Vector& s,
380  Number c
381  );
382 
386  virtual bool HasValidNumbersImpl() const;
387 
389  virtual void PrintImpl(
390  const Journalist& jnlst,
391  EJournalLevel level,
392  EJournalCategory category,
393  const std::string& name,
394  Index indent,
395  const std::string& prefix
396  ) const = 0;
398 
399 private:
408 
410  Vector();
411 
413  Vector(
414  const Vector&
415  );
416 
418  Vector& operator=(
419  const Vector&
420  );
422 
425 
427 
430 
433 
436 
439 
442 
445 
448 
451 
453  mutable bool cached_valid_;
454 
455  // AW: I removed this cache since it gets in the way for the
456  // quality function search
457  // /** Cache for FracToBound */
458  // mutable CachedResults<Number> frac_to_bound_cache_;
460 
461 };
462 
473 {
474 public:
476 
480  VectorSpace(
481  Index dim
482  );
483 
485  virtual ~VectorSpace()
486  { }
488 
492  virtual Vector* MakeNew() const = 0;
493 
495  Index Dim() const
496  {
497  return dim_;
498  }
499 
500 private:
509 
511  VectorSpace();
512 
514  VectorSpace(
515  const VectorSpace&
516  );
517 
519  VectorSpace& operator=(
520  const VectorSpace&
521  );
523 
525  const Index dim_;
526 };
527 
528 /* inline methods */
530 { }
531 
533  const VectorSpace* owner_space
534 )
535  : TaggedObject(),
536  owner_space_(owner_space),
537  dot_cache_(10),
538  nrm2_cache_tag_(0),
539  asum_cache_tag_(0),
540  amax_cache_tag_(0),
541  max_cache_tag_(0),
542  min_cache_tag_(0),
543  sum_cache_tag_(0),
544  sumlogs_cache_tag_(0),
545  cached_valid_(0)
546 {
548 }
549 
550 inline Vector* Vector::MakeNew() const
551 {
552  return owner_space_->MakeNew();
553 }
554 
556 {
557  // ToDo: We can probably copy also the cached values for Norms etc here
558  Vector* copy = MakeNew();
559  copy->Copy(*this);
560  return copy;
561 }
562 
563 inline void Vector::Copy(
564  const Vector& x
565 )
566 {
567  CopyImpl(x);
568  ObjectChanged();
569  // Also copy any cached scalar values from the original vector
570  // ToDo: Check if that is too much overhead
571  TaggedObject::Tag x_tag = x.GetTag();
572  if( x_tag == x.nrm2_cache_tag_ )
573  {
576  }
577  if( x_tag == x.asum_cache_tag_ )
578  {
581  }
582  if( x_tag == x.amax_cache_tag_ )
583  {
586  }
587  if( x_tag == x.max_cache_tag_ )
588  {
591  }
592  if( x_tag == x.min_cache_tag_ )
593  {
596  }
597  if( x_tag == x.sum_cache_tag_ )
598  {
601  }
602  if( x_tag == x.sumlogs_cache_tag_ )
603  {
606  }
607 }
608 
609 inline void Vector::Axpy(
610  Number alpha,
611  const Vector& x
612 )
613 {
614  AxpyImpl(alpha, x);
615  ObjectChanged();
616 }
617 
619  const Vector& x
620 ) const
621 {
622  // The current implementation of the caching doesn't allow to have
623  // a dependency of something with itself. Therefore, we use the
624  // Nrm2 method if the dot product is to be taken with the vector
625  // itself. Might be more efficient anyway.
626  if( this == &x )
627  {
628  Number nrm2 = Nrm2();
629  return nrm2 * nrm2;
630  }
631  Number retValue;
632  if( !dot_cache_.GetCachedResult2Dep(retValue, this, &x) )
633  {
634  retValue = DotImpl(x);
635  dot_cache_.AddCachedResult2Dep(retValue, this, &x);
636  }
637  return retValue;
638 }
639 
640 inline Number Vector::Nrm2() const
641 {
642  if( nrm2_cache_tag_ != GetTag() )
643  {
646  }
647  return cached_nrm2_;
648 }
649 
650 inline Number Vector::Asum() const
651 {
652  if( asum_cache_tag_ != GetTag() )
653  {
656  }
657  return cached_asum_;
658 }
659 
660 inline Number Vector::Amax() const
661 {
662  if( amax_cache_tag_ != GetTag() )
663  {
666  }
667  return cached_amax_;
668 }
669 
670 inline Number Vector::Sum() const
671 {
672  if( sum_cache_tag_ != GetTag() )
673  {
674  cached_sum_ = SumImpl();
676  }
677  return cached_sum_;
678 }
679 
680 inline Number Vector::SumLogs() const
681 {
682  if( sumlogs_cache_tag_ != GetTag() )
683  {
686  }
687  return cached_sumlogs_;
688 }
689 
691 {
693  ObjectChanged();
694 }
695 
696 inline void Vector::Set(
697  Number alpha
698 )
699 {
700  // Could initialize caches here
701  SetImpl(alpha);
702  ObjectChanged();
703 }
704 
706  const Vector& x
707 )
708 {
710  ObjectChanged();
711 }
712 
714  const Vector& x
715 )
716 {
718  ObjectChanged();
719 }
720 
722  const Vector& x
723 )
724 {
726  ObjectChanged();
727 }
728 
730 {
732  ObjectChanged();
733 }
734 
736  const Vector& x
737 )
738 {
739  // Could initialize some caches here
741  ObjectChanged();
742 }
743 
745  const Vector& x
746 )
747 {
748  // Could initialize some caches here
750  ObjectChanged();
751 }
752 
754 {
755  // Could initialize some caches here
757  ObjectChanged();
758 }
759 
761 {
763  ObjectChanged();
764 }
765 
766 inline void Vector::AddScalar(
767  Number scalar)
768 {
769  // Could initialize some caches here
770  AddScalarImpl(scalar);
771  ObjectChanged();
772 }
773 
774 inline Number Vector::Max() const
775 {
776  if( max_cache_tag_ != GetTag() )
777  {
778  cached_max_ = MaxImpl();
780  }
781  return cached_max_;
782 }
783 
784 inline Number Vector::Min() const
785 {
786  if( min_cache_tag_ != GetTag() )
787  {
788  cached_min_ = MinImpl();
790  }
791  return cached_min_;
792 }
793 
795  Number a,
796  const Vector& v1,
797  Number c
798 )
799 {
800  AddTwoVectors(a, v1, 0., v1, c);
801 }
802 
804  Number a,
805  const Vector& v1,
806  Number b,
807  const Vector& v2,
808  Number c
809 )
810 {
811  AddTwoVectorsImpl(a, v1, b, v2, c);
812  ObjectChanged();
813 }
814 
816  const Vector& delta,
817  Number tau
818 ) const
819 {
820  /* AW: I avoid the caching here, since it leads to overhead in the
821  quality function search. Caches for this are in
822  CalculatedQuantities.
823  Number retValue;
824  std::vector<const TaggedObject*> tdeps(1);
825  tdeps[0] = &delta;
826  std::vector<Number> sdeps(1);
827  sdeps[0] = tau;
828  if (!frac_to_bound_cache_.GetCachedResult(retValue, tdeps, sdeps)) {
829  retValue = FracToBoundImpl(delta, tau);
830  frac_to_bound_cache_.AddCachedResult(retValue, tdeps, sdeps);
831  }
832  return retValue;
833  */
834  return FracToBoundImpl(delta, tau);
835 }
836 
838  Number a,
839  const Vector& z,
840  const Vector& s,
841  Number c
842 )
843 {
844  AddVectorQuotientImpl(a, z, s, c);
845  ObjectChanged();
846 }
847 
848 inline bool Vector::HasValidNumbers() const
849 {
850  if( valid_cache_tag_ != GetTag() )
851  {
854  }
855  return cached_valid_;
856 }
857 
858 inline Index Vector::Dim() const
859 {
860  return owner_space_->Dim();
861 }
862 
864 {
865  return owner_space_;
866 }
867 
869  Index dim
870 )
871  : dim_(dim)
872 { }
873 
874 } // namespace Ipopt
875 
876 // Macro definitions for debugging vectors
877 #if IPOPT_VERBOSITY == 0
878 # define DBG_PRINT_VECTOR(__verbose_level, __vec_name, __vec)
879 #else
880 # define DBG_PRINT_VECTOR(__verbose_level, __vec_name, __vec) \
881  if (dbg_jrnl.Verbosity() >= (__verbose_level)) { \
882  if (dbg_jrnl.Jnlst()!=NULL) { \
883  (__vec).Print(dbg_jrnl.Jnlst(), \
884  J_ERROR, J_DBG, \
885  __vec_name, \
886  dbg_jrnl.IndentationLevel()*2, \
887  "# "); \
888  } \
889  }
890 #endif //if IPOPT_VERBOSITY == 0
891 
892 #endif
TaggedObject::Tag valid_cache_tag_
Definition: IpVector.hpp:452
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:672
TaggedObject::Tag max_cache_tag_
Definition: IpVector.hpp:440
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:424
void ElementWiseAbs()
Absolute values of the entries in the vector.
Definition: IpVector.hpp:753
Vector()
Default constructor.
Number cached_sumlogs_
Definition: IpVector.hpp:450
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:449
virtual Number MaxImpl() const =0
Max value in the vector.
#define IPOPTLIB_EXPORT
Definition: config.h:94
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:618
Vector * MakeNewCopy() const
Create new Vector of the same type and copy the data over.
Definition: IpVector.hpp:555
DECLARE_STD_EXCEPTION(FATAL_ERROR_IN_LINEAR_SOLVER)
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:744
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:705
bool HasValidNumbers() const
Method for determining if all stored numbers are valid (i.e., no Inf or Nan).
Definition: IpVector.hpp:848
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...
ipindex Index
Type of all indices of vectors, matrices etc.
Definition: IpTypes.hpp:20
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:609
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:164
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:735
void AddScalar(Number scalar)
Add scalar to every vector component.
Definition: IpVector.hpp:766
VectorSpace base class, corresponding to the Vector base class.
Definition: IpVector.hpp:472
TaggedObject::Tag sum_cache_tag_
Definition: IpVector.hpp:446
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:529
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:696
void ElementWiseSelect(const Vector &x)
Element-wise selection .
Definition: IpVector.hpp:721
Number Nrm2() const
Computes the 2-norm of this vector (DNRM2)
Definition: IpVector.hpp:640
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:837
void Copy(const Vector &x)
Copy the data of the vector x into this vector (DCOPY).
Definition: IpVector.hpp:563
Number cached_asum_
Definition: IpVector.hpp:435
virtual void ElementWiseMultiplyImpl(const Vector &x)=0
Element-wise multiplication .
T Max(T a, T b)
Definition: IpUtils.hpp:23
ipnumber Number
Type of all numbers.
Definition: IpTypes.hpp:17
Number Min() const
Returns the minimum value in the vector.
Definition: IpVector.hpp:784
SmartPtr< const VectorSpace > OwnerSpace() const
Return the owner VectorSpace.
Definition: IpVector.hpp:863
VectorSpace()
Default constructor.
Number cached_min_
Definition: IpVector.hpp:444
Number Amax() const
Computes the max-norm of this vector (based on IDAMAX)
Definition: IpVector.hpp:660
Number SumLogs() const
Returns the sum of the logs of each vector entry.
Definition: IpVector.hpp:680
Number cached_max_
Definition: IpVector.hpp:441
virtual void ElementWiseAbsImpl()=0
Take elementwise absolute values of the elements of the vector.
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:803
Number cached_sum_
Definition: IpVector.hpp:447
#define DBG_ASSERT(test)
Definition: IpDebug.hpp:27
TaggedObject::Tag amax_cache_tag_
Definition: IpVector.hpp:437
Number Asum() const
Computes the 1-norm of this vector (DASUM)
Definition: IpVector.hpp:650
virtual Number Nrm2Impl() const =0
Computes the 2-norm of this vector (DNRM2)
Index Dim() const
Dimension of the Vector.
Definition: IpVector.hpp:858
unsigned int Tag
Type for the Tag values.
virtual void SetImpl(Number alpha)=0
Set each element in the vector to the scalar alpha.
virtual void ElementWiseSelectImpl(const Vector &x)=0
Element-wise selection .
TaggedObject::Tag nrm2_cache_tag_
Definition: IpVector.hpp:431
TaggedObject::Tag min_cache_tag_
Definition: IpVector.hpp:443
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:485
Vector * MakeNew() const
Create new Vector of the same type with uninitialized data.
Definition: IpVector.hpp:550
virtual Number SumLogsImpl() const =0
Sum of logs of entries in the vector.
T Min(T a, T b)
Definition: IpUtils.hpp:53
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:434
void ElementWiseMultiply(const Vector &x)
Element-wise multiplication .
Definition: IpVector.hpp:713
Number cached_nrm2_
Definition: IpVector.hpp:432
const Index dim_
Dimension of the vectors in this vector space.
Definition: IpVector.hpp:525
Index Dim() const
Accessor function for the dimension of the vectors of this type.
Definition: IpVector.hpp:495
Number Sum() const
Returns the sum of the vector entries.
Definition: IpVector.hpp:670
void AddOneVector(Number a, const Vector &v1, Number c)
Add one vector, y = a * v1 + c * y.
Definition: IpVector.hpp:794
Number FracToBound(const Vector &delta, Number tau) const
Fraction to the boundary parameter.
Definition: IpVector.hpp:815
Number cached_amax_
Definition: IpVector.hpp:438
void ElementWiseSqrt()
Element-wise square root of the entries in the vector.
Definition: IpVector.hpp:760
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:690
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:429
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:774
void ElementWiseReciprocal()
Reciprocates the entries in the vector.
Definition: IpVector.hpp:729
bool cached_valid_
Definition: IpVector.hpp:453
virtual Number MinImpl() const =0
Min number in the vector.