- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# /home/coin/SVN-release/Ipopt-3.7.1/Ipopt/src/LinAlg/IpDenseVector.hpp

Go to the documentation of this file.

    00001 // Copyright (C) 2004, 2009 International Business Machines and others.
    00002 // All Rights Reserved.
    00003 // This code is published under the Common Public License.
    00004 //
    00005 // $Id: IpDenseVector.hpp 1492 2009-07-07 02:11:04Z andreasw $
    00006 //
    00007 // Authors:  Carl Laird, Andreas Waechter     IBM    2004-08-13
    00008
    00009 #ifndef __IPDENSEVECTOR_HPP__
    00010 #define __IPDENSEVECTOR_HPP__
    00011
    00012 #include "IpUtils.hpp"
    00013 #include "IpVector.hpp"
    00014 #include <map>
    00015
    00016 namespace Ipopt
    00017 {
    00018
    00019   /* forward declarations */
    00020   class DenseVectorSpace;
    00021
    00024   DECLARE_STD_EXCEPTION(METADATA_ERROR);
    00026
    00040   class DenseVector : public Vector
    00041   {
    00042   public:
    00043
    00048     DenseVector(const DenseVectorSpace* owner_space);
    00049
    00052     virtual ~DenseVector();
    00054
    00058     SmartPtr<DenseVector> MakeNewDenseVector() const;
    00059
    00061     void SetValues(const Number *x);
    00062
    00068     inline Number* Values();
    00069
    00078     inline const Number* Values() const;
    00079
    00083     const Number* ExpandedValues() const;
    00084
    00087     inline Number* ExpandedValues()
    00088     {
    00089       return Values();
    00090     }
    00091
    00094     bool IsHomogeneous() const
    00095     {
    00096       return homogeneous_;
    00097     }
    00098
    00100     Number Scalar() const
    00101     {
    00102       DBG_ASSERT(homogeneous_);
    00103       return scalar_;
    00104     }
    00106
    00112     void CopyToPos(Index Pos, const Vector& x);
    00116     void CopyFromPos(Index Pos, const Vector& x);
    00118
    00119   protected:
    00123     virtual void CopyImpl(const Vector& x);
    00124
    00126     virtual void ScalImpl(Number alpha);
    00127
    00129     virtual void AxpyImpl(Number alpha, const Vector &x);
    00130
    00132     virtual Number DotImpl(const Vector &x) const;
    00133
    00135     virtual Number Nrm2Impl() const;
    00136
    00138     virtual Number AsumImpl() const;
    00139
    00141     virtual Number AmaxImpl() const;
    00142
    00144     virtual void SetImpl(Number value);
    00145
    00147     virtual void ElementWiseDivideImpl(const Vector& x);
    00148
    00150     virtual void ElementWiseMultiplyImpl(const Vector& x);
    00151
    00153     virtual void ElementWiseMaxImpl(const Vector& x);
    00154
    00156     virtual void ElementWiseMinImpl(const Vector& x);
    00157
    00159     virtual void ElementWiseReciprocalImpl();
    00160
    00162     virtual void ElementWiseAbsImpl();
    00163
    00165     virtual void ElementWiseSqrtImpl();
    00166
    00168     virtual void ElementWiseSgnImpl();
    00169
    00171     virtual void AddScalarImpl(Number scalar);
    00172
    00174     virtual Number MaxImpl() const;
    00175
    00177     virtual Number MinImpl() const;
    00178
    00180     virtual Number SumImpl() const;
    00181
    00183     virtual Number SumLogsImpl() const;
    00184
    00189     void AddTwoVectorsImpl(Number a, const Vector& v1,
    00190                            Number b, const Vector& v2, Number c);
    00192     Number FracToBoundImpl(const Vector& delta, Number tau) const;
    00194     void AddVectorQuotientImpl(Number a, const Vector& z, const Vector& s,
    00195                                Number c);
    00197
    00200     /* Print the entire vector with padding */
    00201     virtual void PrintImpl(const Journalist& jnlst,
    00202                            EJournalLevel level,
    00203                            EJournalCategory category,
    00204                            const std::string& name,
    00205                            Index indent,
    00206                            const std::string& prefix) const
    00207     {
    00208       PrintImplOffset(jnlst, level, category, name, indent, prefix, 1);
    00209     }
    00210     /* Print the entire vector with padding, and start counting with
    00211        an offset. */
    00212     void PrintImplOffset(const Journalist& jnlst,
    00213                          EJournalLevel level,
    00214                          EJournalCategory category,
    00215                          const std::string& name,
    00216                          Index indent,
    00217                          const std::string& prefix,
    00218                          Index offset) const;
    00220     friend class ParVector;
    00221
    00222   private:
    00232     DenseVector();
    00233
    00235     DenseVector(const DenseVector&);
    00236
    00238     void operator=(const DenseVector&);
    00240
    00244     const DenseVectorSpace* owner_space_;
    00245
    00247     Number* values_;
    00248
    00250     mutable Number* expanded_values_;
    00251
    00254     Number* values_allocated();
    00255
    00258     bool initialized_;
    00259
    00264     bool homogeneous_;
    00265
    00268     Number scalar_;
    00269
    00272     void set_values_from_scalar();
    00273   };
    00274
    00278   typedef std::map<std::string, std::vector<std::string> > StringMetaDataMapType;
    00279   typedef std::map<std::string, std::vector<Index> > IntegerMetaDataMapType;
    00280   typedef std::map<std::string, std::vector<Number> > NumericMetaDataMapType;
    00281
    00284   class DenseVectorSpace : public VectorSpace
    00285   {
    00286   public:
    00292     DenseVectorSpace(Index dim)
    00293         :
    00294         VectorSpace(dim)
    00295     {}
    00296
    00298     ~DenseVectorSpace()
    00299     {}
    00301
    00303     DenseVector* MakeNewDenseVector() const
    00304     {
    00305       return new DenseVector(this);
    00306     }
    00307
    00311     virtual Vector* MakeNew() const
    00312     {
    00313       return MakeNewDenseVector();
    00314     }
    00315
    00322     Number* AllocateInternalStorage() const;
    00323
    00325     void FreeInternalStorage(Number* values) const;
    00327
    00332     bool HasStringMetaData(const std::string tag) const;
    00333
    00335     bool HasIntegerMetaData(const std::string tag) const;
    00336
    00338     bool HasNumericMetaData(const std::string tag) const;
    00339
    00341     const std::vector<std::string>& GetStringMetaData(const std::string& tag) const;
    00342
    00344     const std::vector<Index>& GetIntegerMetaData(const std::string& tag) const;
    00345
    00347     const std::vector<Number>& GetNumericMetaData(const std::string& tag) const;
    00348
    00350     void SetStringMetaData(std::string tag, std::vector<std::string> meta_data);
    00351
    00353     void SetIntegerMetaData(std::string tag, std::vector<Index> meta_data);
    00354
    00356     void SetNumericMetaData(std::string tag, std::vector<Number> meta_data);
    00358
    00359   private:
    00360     // variables to store vector meta data
    00361     StringMetaDataMapType string_meta_data_;
    00362     IntegerMetaDataMapType integer_meta_data_;
    00363     NumericMetaDataMapType numeric_meta_data_;
    00364
    00365   };
    00366
    00367   // inline functions
    00368   inline Number* DenseVector::Values()
    00369   {
    00370     // Here we assume that every time someone requests this direct raw
    00371     // pointer, the data is going to change and the Tag for this
    00372     // vector has to be updated.
    00373
    00374     if (initialized_ && homogeneous_) {
    00375       // If currently the vector is a homogeneous vector, set all elements
    00376       // explicitly to this value
    00377       set_values_from_scalar();
    00378     }
    00379     ObjectChanged();
    00380     initialized_= true;
    00381     homogeneous_ = false;
    00382     return values_allocated();
    00383   }
    00384
    00385   inline const Number* DenseVector::Values() const
    00386   {
    00387     DBG_ASSERT(initialized_ && (Dim()==0 || values_));
    00388     return values_;
    00389   }
    00390
    00391   inline Number* DenseVector::values_allocated()
    00392   {
    00393     if (values_==NULL) {
    00394       values_ = owner_space_->AllocateInternalStorage();
    00395     }
    00396     return values_;
    00397   }
    00398
    00399   inline
    00400   Number* DenseVectorSpace::AllocateInternalStorage() const
    00401   {
    00402     if (Dim()>0) {
    00403       return new Number[Dim()];
    00404     }
    00405     else {
    00406       return NULL;
    00407     }
    00408   }
    00409
    00410   inline
    00411   void DenseVectorSpace::FreeInternalStorage(Number* values) const
    00412   {
    00413     delete [] values;
    00414   }
    00415
    00416   inline
    00417   SmartPtr<DenseVector> DenseVector::MakeNewDenseVector() const
    00418   {
    00419     return owner_space_->MakeNewDenseVector();
    00420   }
    00421
    00422   inline
    00423   bool DenseVectorSpace::HasStringMetaData(const std::string tag) const
    00424   {
    00425     StringMetaDataMapType::const_iterator iter;
    00426     iter = string_meta_data_.find(tag);
    00427
    00428     if (iter != string_meta_data_.end()) {
    00429       return true;
    00430     }
    00431
    00432     return false;
    00433   }
    00434
    00435   inline
    00436   bool DenseVectorSpace::HasIntegerMetaData(const std::string tag) const
    00437   {
    00438     IntegerMetaDataMapType::const_iterator iter;
    00439     iter = integer_meta_data_.find(tag);
    00440
    00441     if (iter != integer_meta_data_.end()) {
    00442       return true;
    00443     }
    00444
    00445     return false;
    00446   }
    00447
    00448   inline
    00449   bool DenseVectorSpace::HasNumericMetaData(const std::string tag) const
    00450   {
    00451     NumericMetaDataMapType::const_iterator iter;
    00452     iter = numeric_meta_data_.find(tag);
    00453
    00454     if (iter != numeric_meta_data_.end()) {
    00455       return true;
    00456     }
    00457
    00458     return false;
    00459   }
    00460
    00461   inline
    00462   const std::vector<std::string>& DenseVectorSpace::GetStringMetaData(const std::string& tag) const
    00463   {
    00464     DBG_ASSERT(HasStringMetaData(tag));
    00465     StringMetaDataMapType::const_iterator iter;
    00466     iter = string_meta_data_.find(tag);
    00467     return iter->second;
    00468   }
    00469
    00470   inline
    00471   const std::vector<Index>& DenseVectorSpace::GetIntegerMetaData(const std::string& tag) const
    00472   {
    00473     DBG_ASSERT(HasIntegerMetaData(tag));
    00474     IntegerMetaDataMapType::const_iterator iter;
    00475     iter = integer_meta_data_.find(tag);
    00476     return iter->second;
    00477   }
    00478
    00479   inline
    00480   const std::vector<Number>& DenseVectorSpace::GetNumericMetaData(const std::string& tag) const
    00481   {
    00482     DBG_ASSERT(HasNumericMetaData(tag));
    00483     NumericMetaDataMapType::const_iterator iter;
    00484     iter = numeric_meta_data_.find(tag);
    00485     return iter->second;
    00486   }
    00487
    00488   inline
    00489   void DenseVectorSpace::SetStringMetaData(std::string tag, std::vector<std::string> meta_data)
    00490   {
    00491     string_meta_data_[tag] = meta_data;
    00492   }
    00493
    00494   inline
    00495   void DenseVectorSpace::SetIntegerMetaData(std::string tag, std::vector<Index> meta_data)
    00496   {
    00497     integer_meta_data_[tag] = meta_data;
    00498   }
    00499
    00500   inline
    00501   void DenseVectorSpace::SetNumericMetaData(std::string tag, std::vector<Number> meta_data)
    00502   {
    00503     numeric_meta_data_[tag] = meta_data;
    00504   }
    00505 } // namespace Ipopt
    00506 #endif