Ipopt Documentation  
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
IpDenseVector.hpp
Go to the documentation of this file.
1 // Copyright (C) 2004, 2009 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 __IPDENSEVECTOR_HPP__
8 #define __IPDENSEVECTOR_HPP__
9 
10 #include "IpUtils.hpp"
11 #include "IpVector.hpp"
12 #include <map>
13 
14 namespace Ipopt
15 {
16 
17 /* forward declarations */
18 class DenseVectorSpace;
19 
22 DECLARE_STD_EXCEPTION(METADATA_ERROR);
24 
41 {
42 public:
48  const DenseVectorSpace* owner_space
49  );
50 
53  virtual ~DenseVector();
55 
59  SmartPtr<DenseVector> MakeNewDenseVector() const;
60 
62  void SetValues(
63  const Number* x
64  );
65 
71  inline Number* Values();
72 
84  inline const Number* Values() const;
85 
90  const Number* ExpandedValues() const;
91 
96  {
97  return Values();
98  }
99 
103  bool IsHomogeneous() const
104  {
105  return homogeneous_;
106  }
107 
109  Number Scalar() const
110  {
111  DBG_ASSERT(homogeneous_);
112  return scalar_;
113  }
115 
123  void CopyToPos(
124  Index Pos,
125  const Vector& x
126  );
127 
133  void CopyFromPos(
134  Index Pos,
135  const Vector& x
136  );
138 
139 protected:
142  virtual void CopyImpl(
143  const Vector& x
144  );
145 
146  virtual void ScalImpl(
147  Number alpha
148  );
149 
150  virtual void AxpyImpl(
151  Number alpha,
152  const Vector& x
153  );
154 
155  virtual Number DotImpl(
156  const Vector& x
157  ) const;
158 
159  virtual Number Nrm2Impl() const;
160 
161  virtual Number AsumImpl() const;
162 
163  virtual Number AmaxImpl() const;
164 
165  virtual void SetImpl(
166  Number value
167  );
168 
169  virtual void ElementWiseDivideImpl(
170  const Vector& x
171  );
172 
173  virtual void ElementWiseMultiplyImpl(
174  const Vector& x
175  );
176 
177  virtual void ElementWiseMaxImpl(
178  const Vector& x
179  );
180 
181  virtual void ElementWiseMinImpl(
182  const Vector& x
183  );
184 
185  virtual void ElementWiseReciprocalImpl();
186 
187  virtual void ElementWiseAbsImpl();
188 
189  virtual void ElementWiseSqrtImpl();
190 
191  virtual void ElementWiseSgnImpl();
192 
193  virtual void AddScalarImpl(
194  Number scalar
195  );
196 
197  virtual Number MaxImpl() const;
198 
199  virtual Number MinImpl() const;
200 
201  virtual Number SumImpl() const;
202 
203  virtual Number SumLogsImpl() const;
205 
212  void AddTwoVectorsImpl(
213  Number a,
214  const Vector& v1,
215  Number b,
216  const Vector& v2,
217  Number c
218  );
219 
221  Number FracToBoundImpl(
222  const Vector& delta,
223  Number tau
224  ) const;
225 
227  void AddVectorQuotientImpl(
228  Number a,
229  const Vector& z,
230  const Vector& s,
231  Number c
232  );
234 
237  virtual void PrintImpl(
238  const Journalist& jnlst,
239  EJournalLevel level,
240  EJournalCategory category,
241  const std::string& name,
242  Index indent,
243  const std::string& prefix
244  ) const
245  {
246  PrintImplOffset(jnlst, level, category, name, indent, prefix, 1);
247  }
248 
249  /* Print the entire vector with padding, and start counting with an offset. */
250  void PrintImplOffset(
251  const Journalist& jnlst,
252  EJournalLevel level,
253  EJournalCategory category,
254  const std::string& name,
255  Index indent,
256  const std::string& prefix,
257  Index offset
258  ) const;
260 
261  friend class ParVector;
262 
263 private:
273  DenseVector();
274 
276  DenseVector(
277  const DenseVector&
278  );
279 
281  void operator=(
282  const DenseVector&
283  );
285 
288 
291 
294 
296  inline Number* values_allocated();
297 
303 
311 
314 
318  void set_values_from_scalar();
319 };
320 
323 typedef std::map<std::string, std::vector<std::string> > StringMetaDataMapType;
324 typedef std::map<std::string, std::vector<Index> > IntegerMetaDataMapType;
325 typedef std::map<std::string, std::vector<Number> > NumericMetaDataMapType;
327 
330 {
331 public:
336  Index dim
337  )
338  : VectorSpace(dim)
339  { }
340 
343  { }
345 
348  {
349  return new DenseVector(this);
350  }
351 
352  virtual Vector* MakeNew() const
353  {
354  return MakeNewDenseVector();
355  }
356 
364  inline Number* AllocateInternalStorage() const;
365 
367  inline void FreeInternalStorage(
368  Number* values
369  ) const;
371 
376  inline
377  bool HasStringMetaData(
378  const std::string tag
379  ) const;
380 
382  inline
383  bool HasIntegerMetaData(
384  const std::string tag
385  ) const;
386 
388  inline
389  bool HasNumericMetaData(
390  const std::string tag
391  ) const;
392 
394  inline const std::vector<std::string>& GetStringMetaData(
395  const std::string& tag
396  ) const;
397 
399  inline const std::vector<Index>& GetIntegerMetaData(
400  const std::string& tag
401  ) const;
402 
404  inline const std::vector<Number>& GetNumericMetaData(
405  const std::string& tag
406  ) const;
407 
409  inline void SetStringMetaData(
410  std::string tag,
411  std::vector<std::string> meta_data
412  );
413 
415  inline void SetIntegerMetaData(
416  std::string tag,
417  std::vector<Index> meta_data
418  );
419 
421  inline void SetNumericMetaData(
422  std::string tag,
423  std::vector<Number> meta_data
424  );
425 
427  inline const StringMetaDataMapType& GetStringMetaData() const;
428 
430  inline const IntegerMetaDataMapType& GetIntegerMetaData() const;
431 
433  inline const NumericMetaDataMapType& GetNumericMetaData() const;
435 
436 private:
437  // variables to store vector meta data
441 };
442 
443 // inline functions
445 {
446  // Here we assume that every time someone requests this direct raw
447  // pointer, the data is going to change and the Tag for this
448  // vector has to be updated.
449 
450  if( initialized_ && homogeneous_ )
451  {
452  // If currently the vector is a homogeneous vector, set all elements
453  // explicitly to this value
455  }
456  ObjectChanged();
457  initialized_ = true;
458  homogeneous_ = false;
459  return values_allocated();
460 }
461 
462 inline const Number* DenseVector::Values() const
463 {
464  DBG_ASSERT(initialized_ && (Dim() == 0 || values_));
465  return values_;
466 }
467 
469 {
470  if( values_ == NULL )
471  {
473  }
474  return values_;
475 }
476 
478 {
479  if( Dim() > 0 )
480  {
481  return new Number[Dim()];
482  }
483  else
484  {
485  return NULL;
486  }
487 }
488 
489 inline
491  Number* values
492 ) const
493 {
494  delete[] values;
495 }
496 
498 {
500 }
501 
502 inline
504  const std::string tag
505 ) const
506 {
507  StringMetaDataMapType::const_iterator iter;
508  iter = string_meta_data_.find(tag);
509 
510  if( iter != string_meta_data_.end() )
511  {
512  return true;
513  }
514 
515  return false;
516 }
517 
518 inline
520  const std::string tag
521 ) const
522 {
523  IntegerMetaDataMapType::const_iterator iter;
524  iter = integer_meta_data_.find(tag);
525 
526  if( iter != integer_meta_data_.end() )
527  {
528  return true;
529  }
530 
531  return false;
532 }
533 
534 inline
536  const std::string tag
537 ) const
538 {
539  NumericMetaDataMapType::const_iterator iter;
540  iter = numeric_meta_data_.find(tag);
541 
542  if( iter != numeric_meta_data_.end() )
543  {
544  return true;
545  }
546 
547  return false;
548 }
549 
550 inline const std::vector<std::string>& DenseVectorSpace::GetStringMetaData(
551  const std::string& tag
552 ) const
553 {
555  StringMetaDataMapType::const_iterator iter;
556  iter = string_meta_data_.find(tag);
557  return iter->second;
558 }
559 
560 inline const std::vector<Index>& DenseVectorSpace::GetIntegerMetaData(
561  const std::string& tag
562 ) const
563 {
565  IntegerMetaDataMapType::const_iterator iter;
566  iter = integer_meta_data_.find(tag);
567  return iter->second;
568 }
569 
570 inline const std::vector<Number>& DenseVectorSpace::GetNumericMetaData(
571  const std::string& tag
572 ) const
573 {
575  NumericMetaDataMapType::const_iterator iter;
576  iter = numeric_meta_data_.find(tag);
577  return iter->second;
578 }
579 
581  std::string tag,
582  std::vector<std::string> meta_data
583 )
584 {
585  string_meta_data_[tag] = meta_data;
586 }
587 
589  std::string tag,
590  std::vector<Index> meta_data
591 )
592 {
593  integer_meta_data_[tag] = meta_data;
594 }
595 
597  std::string tag,
598  std::vector<Number> meta_data
599 )
600 {
601  numeric_meta_data_[tag] = meta_data;
602 }
603 
605 {
606  return string_meta_data_;
607 }
608 
610 {
611  return integer_meta_data_;
612 }
613 
615 {
616  return numeric_meta_data_;
617 }
618 
619 } // namespace Ipopt
620 #endif
bool initialized_
Flag for Initialization.
std::map< std::string, std::vector< Number > > NumericMetaDataMapType
void set_values_from_scalar()
Auxiliary method for setting explicitly all elements in values_ to the current scalar value...
Number * AllocateInternalStorage() const
Allocate internal storage for the DenseVector.
void FreeInternalStorage(Number *values) const
Deallocate internal storage for the DenseVector.
bool IsHomogeneous() const
Indicates if the vector is homogeneous (i.e., all entries have the value Scalar().
DenseVector * MakeNewDenseVector() const
Method for creating a new vector of this specific type.
DECLARE_STD_EXCEPTION(FATAL_ERROR_IN_LINEAR_SOLVER)
void SetStringMetaData(std::string tag, std::vector< std::string > meta_data)
Set meta data of type std::string by tag.
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
std::map< std::string, std::vector< Index > > IntegerMetaDataMapType
Dense Vector Implementation.
const NumericMetaDataMapType & GetNumericMetaData() const
Get map of meta data of type Number.
virtual void PrintImpl(const Journalist &jnlst, EJournalLevel level, EJournalCategory category, const std::string &name, Index indent, const std::string &prefix) const
Print the entire vector.
EJournalLevel
Print Level Enum.
bool homogeneous_
Flag indicating whether the vector is currently homogeneous (that is, all elements have the same valu...
This vectors space is the vector space for DenseVector.
StringMetaDataMapType string_meta_data_
Index Dim() const
Accessor function for the dimension of the vectors of this type.
Definition: IpVector.hpp:481
void SetNumericMetaData(std::string tag, std::vector< Number > meta_data)
Set meta data of type Number by tag.
~DenseVectorSpace()
Destructor.
void SetIntegerMetaData(std::string tag, std::vector< Index > meta_data)
Set meta data of type Index by tag.
Template class for Smart Pointers.
Definition: IpSmartPtr.hpp:171
Number * values_
Dense Number array of vector values.
virtual Vector * MakeNew() const
Pure virtual method for creating a new Vector of the corresponding type.
VectorSpace base class, corresponding to the Vector base class.
Definition: IpVector.hpp:458
bool HasStringMetaData(const std::string tag) const
Check if string meta exists for tag.
NumericMetaDataMapType numeric_meta_data_
DenseVectorSpace(Index dim)
Constructor, requires dimension of all vector for this VectorSpace.
bool HasNumericMetaData(const std::string tag) const
Check if Numeric meta exists for tag.
const DenseVectorSpace * owner_space_
Copy of the owner_space ptr as a DenseVectorSpace instead of a VectorSpace.
int Index
Type of all indices of vectors, matrices etc.
Definition: IpTypes.hpp:17
#define DBG_ASSERT(test)
Definition: IpDebug.hpp:28
const IntegerMetaDataMapType & GetIntegerMetaData() const
Get map of meta data of type Number.
Number * values_allocated()
Get the internal values array, making sure that memory has been allocated.
Number * ExpandedValues()
This is the same as Values, but we add it here so that ExpandedValues can also be used for the non-co...
#define IPOPTLIB_EXPORT
Number Scalar() const
Scalar value of all entries in a homogeneous vector.
bool HasIntegerMetaData(const std::string tag) const
Check if Integer meta exists for tag.
Class responsible for all message output.
const StringMetaDataMapType & GetStringMetaData() const
Get map of meta data of type Number.
Index Dim() const
Dimension of the Vector.
Definition: IpVector.hpp:836
SmartPtr< DenseVector > MakeNewDenseVector() const
Create a new DenseVector from same VectorSpace.
Number scalar_
Homogeneous value of all elements if the vector is currently homogeneous.
Number * expanded_values_
Dense Number array pointer that is used for ExpandedValues.
EJournalCategory
Category Selection Enum.
IntegerMetaDataMapType integer_meta_data_
std::map< std::string, std::vector< std::string > > StringMetaDataMapType
Number * Values()
Obtain pointer to the internal Number array with vector elements with the intention to change the vec...