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 // $Id: IpDenseVector.hpp 1861 2010-12-21 21:34:47Z andreasw $
6 //
7 // Authors: Carl Laird, Andreas Waechter IBM 2004-08-13
8 
9 #ifndef __IPDENSEVECTOR_HPP__
10 #define __IPDENSEVECTOR_HPP__
11 
12 #include "IpUtils.hpp"
13 #include "IpVector.hpp"
14 #include <map>
15 
16 namespace Ipopt
17 {
18 
19  /* forward declarations */
20  class DenseVectorSpace;
21 
24  DECLARE_STD_EXCEPTION(METADATA_ERROR);
26 
40  class DenseVector : public Vector
41  {
42  public:
43 
48  DenseVector(const DenseVectorSpace* owner_space);
49 
52  virtual ~DenseVector();
54 
59 
61  void SetValues(const Number *x);
62 
68  inline Number* Values();
69 
78  inline const Number* Values() const;
79 
83  const Number* ExpandedValues() const;
84 
88  {
89  return Values();
90  }
91 
94  bool IsHomogeneous() const
95  {
96  return homogeneous_;
97  }
98 
100  Number Scalar() const
101  {
103  return scalar_;
104  }
106 
112  void CopyToPos(Index Pos, const Vector& x);
116  void CopyFromPos(Index Pos, const Vector& x);
118 
119  protected:
123  virtual void CopyImpl(const Vector& x);
124 
126  virtual void ScalImpl(Number alpha);
127 
129  virtual void AxpyImpl(Number alpha, const Vector &x);
130 
132  virtual Number DotImpl(const Vector &x) const;
133 
135  virtual Number Nrm2Impl() const;
136 
138  virtual Number AsumImpl() const;
139 
141  virtual Number AmaxImpl() const;
142 
144  virtual void SetImpl(Number value);
145 
147  virtual void ElementWiseDivideImpl(const Vector& x);
148 
150  virtual void ElementWiseMultiplyImpl(const Vector& x);
151 
153  virtual void ElementWiseMaxImpl(const Vector& x);
154 
156  virtual void ElementWiseMinImpl(const Vector& x);
157 
159  virtual void ElementWiseReciprocalImpl();
160 
162  virtual void ElementWiseAbsImpl();
163 
165  virtual void ElementWiseSqrtImpl();
166 
168  virtual void ElementWiseSgnImpl();
169 
171  virtual void AddScalarImpl(Number scalar);
172 
174  virtual Number MaxImpl() const;
175 
177  virtual Number MinImpl() const;
178 
180  virtual Number SumImpl() const;
181 
183  virtual Number SumLogsImpl() const;
184 
189  void AddTwoVectorsImpl(Number a, const Vector& v1,
190  Number b, const Vector& v2, Number c);
192  Number FracToBoundImpl(const Vector& delta, Number tau) const;
194  void AddVectorQuotientImpl(Number a, const Vector& z, const Vector& s,
195  Number c);
197 
200  /* Print the entire vector with padding */
201  virtual void PrintImpl(const Journalist& jnlst,
202  EJournalLevel level,
203  EJournalCategory category,
204  const std::string& name,
205  Index indent,
206  const std::string& prefix) const
207  {
208  PrintImplOffset(jnlst, level, category, name, indent, prefix, 1);
209  }
210  /* Print the entire vector with padding, and start counting with
211  an offset. */
212  void PrintImplOffset(const Journalist& jnlst,
213  EJournalLevel level,
214  EJournalCategory category,
215  const std::string& name,
216  Index indent,
217  const std::string& prefix,
218  Index offset) const;
220  friend class ParVector;
221 
222  private:
232  DenseVector();
233 
235  DenseVector(const DenseVector&);
236 
238  void operator=(const DenseVector&);
240 
245 
248 
251 
255 
259 
265 
269 
272  void set_values_from_scalar();
273  };
274 
278  typedef std::map<std::string, std::vector<std::string> > StringMetaDataMapType;
279  typedef std::map<std::string, std::vector<Index> > IntegerMetaDataMapType;
280  typedef std::map<std::string, std::vector<Number> > NumericMetaDataMapType;
281 
285  {
286  public:
293  :
294  VectorSpace(dim)
295  {}
296 
299  {}
301 
304  {
305  return new DenseVector(this);
306  }
307 
311  virtual Vector* MakeNew() const
312  {
313  return MakeNewDenseVector();
314  }
315 
323 
325  void FreeInternalStorage(Number* values) const;
327 
332  bool HasStringMetaData(const std::string tag) const;
333 
335  bool HasIntegerMetaData(const std::string tag) const;
336 
338  bool HasNumericMetaData(const std::string tag) const;
339 
341  const std::vector<std::string>& GetStringMetaData(const std::string& tag) const;
342 
344  const std::vector<Index>& GetIntegerMetaData(const std::string& tag) const;
345 
347  const std::vector<Number>& GetNumericMetaData(const std::string& tag) const;
348 
350  void SetStringMetaData(std::string tag, std::vector<std::string> meta_data);
351 
353  void SetIntegerMetaData(std::string tag, std::vector<Index> meta_data);
354 
356  void SetNumericMetaData(std::string tag, std::vector<Number> meta_data);
357 
360 
363 
367 
368  private:
369  // variables to store vector meta data
373 
374  };
375 
376  // inline functions
378  {
379  // Here we assume that every time someone requests this direct raw
380  // pointer, the data is going to change and the Tag for this
381  // vector has to be updated.
382 
383  if (initialized_ && homogeneous_) {
384  // If currently the vector is a homogeneous vector, set all elements
385  // explicitly to this value
387  }
388  ObjectChanged();
389  initialized_= true;
390  homogeneous_ = false;
391  return values_allocated();
392  }
393 
394  inline const Number* DenseVector::Values() const
395  {
396  DBG_ASSERT(initialized_ && (Dim()==0 || values_));
397  return values_;
398  }
399 
401  {
402  if (values_==NULL) {
404  }
405  return values_;
406  }
407 
408  inline
410  {
411  if (Dim()>0) {
412  return new Number[Dim()];
413  }
414  else {
415  return NULL;
416  }
417  }
418 
419  inline
421  {
422  delete [] values;
423  }
424 
425  inline
427  {
429  }
430 
431  inline
432  bool DenseVectorSpace::HasStringMetaData(const std::string tag) const
433  {
434  StringMetaDataMapType::const_iterator iter;
435  iter = string_meta_data_.find(tag);
436 
437  if (iter != string_meta_data_.end()) {
438  return true;
439  }
440 
441  return false;
442  }
443 
444  inline
445  bool DenseVectorSpace::HasIntegerMetaData(const std::string tag) const
446  {
447  IntegerMetaDataMapType::const_iterator iter;
448  iter = integer_meta_data_.find(tag);
449 
450  if (iter != integer_meta_data_.end()) {
451  return true;
452  }
453 
454  return false;
455  }
456 
457  inline
458  bool DenseVectorSpace::HasNumericMetaData(const std::string tag) const
459  {
460  NumericMetaDataMapType::const_iterator iter;
461  iter = numeric_meta_data_.find(tag);
462 
463  if (iter != numeric_meta_data_.end()) {
464  return true;
465  }
466 
467  return false;
468  }
469 
470  inline
471  const std::vector<std::string>& DenseVectorSpace::GetStringMetaData(const std::string& tag) const
472  {
474  StringMetaDataMapType::const_iterator iter;
475  iter = string_meta_data_.find(tag);
476  return iter->second;
477  }
478 
479  inline
480  const std::vector<Index>& DenseVectorSpace::GetIntegerMetaData(const std::string& tag) const
481  {
483  IntegerMetaDataMapType::const_iterator iter;
484  iter = integer_meta_data_.find(tag);
485  return iter->second;
486  }
487 
488  inline
489  const std::vector<Number>& DenseVectorSpace::GetNumericMetaData(const std::string& tag) const
490  {
492  NumericMetaDataMapType::const_iterator iter;
493  iter = numeric_meta_data_.find(tag);
494  return iter->second;
495  }
496 
497  inline
498  void DenseVectorSpace::SetStringMetaData(std::string tag, std::vector<std::string> meta_data)
499  {
500  string_meta_data_[tag] = meta_data;
501  }
502 
503  inline
504  void DenseVectorSpace::SetIntegerMetaData(std::string tag, std::vector<Index> meta_data)
505  {
506  integer_meta_data_[tag] = meta_data;
507  }
508 
509  inline
510  void DenseVectorSpace::SetNumericMetaData(std::string tag, std::vector<Number> meta_data)
511  {
512  numeric_meta_data_[tag] = meta_data;
513  }
514 
515  inline
517  {
518  return string_meta_data_;
519  }
520 
521  inline
523  {
524  return integer_meta_data_;
525  }
526 
527  inline
529  {
530  return numeric_meta_data_;
531  }
532 
533 } // namespace Ipopt
534 #endif