/build/buildd/coinor-ipopt-3.10.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 Eclipse Public License.
00004 //
00005 // $Id: IpDenseVector.hpp 1861 2010-12-21 21:34:47Z 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);
00357 
00359     const StringMetaDataMapType& GetStringMetaData() const;
00360 
00362     const IntegerMetaDataMapType& GetIntegerMetaData() const;
00363 
00365     const NumericMetaDataMapType& GetNumericMetaData() const;
00367 
00368   private:
00369     // variables to store vector meta data
00370     StringMetaDataMapType string_meta_data_;
00371     IntegerMetaDataMapType integer_meta_data_;
00372     NumericMetaDataMapType numeric_meta_data_;
00373 
00374   };
00375 
00376   // inline functions
00377   inline Number* DenseVector::Values()
00378   {
00379     // Here we assume that every time someone requests this direct raw
00380     // pointer, the data is going to change and the Tag for this
00381     // vector has to be updated.
00382 
00383     if (initialized_ && homogeneous_) {
00384       // If currently the vector is a homogeneous vector, set all elements
00385       // explicitly to this value
00386       set_values_from_scalar();
00387     }
00388     ObjectChanged();
00389     initialized_= true;
00390     homogeneous_ = false;
00391     return values_allocated();
00392   }
00393 
00394   inline const Number* DenseVector::Values() const
00395   {
00396     DBG_ASSERT(initialized_ && (Dim()==0 || values_));
00397     return values_;
00398   }
00399 
00400   inline Number* DenseVector::values_allocated()
00401   {
00402     if (values_==NULL) {
00403       values_ = owner_space_->AllocateInternalStorage();
00404     }
00405     return values_;
00406   }
00407 
00408   inline
00409   Number* DenseVectorSpace::AllocateInternalStorage() const
00410   {
00411     if (Dim()>0) {
00412       return new Number[Dim()];
00413     }
00414     else {
00415       return NULL;
00416     }
00417   }
00418 
00419   inline
00420   void DenseVectorSpace::FreeInternalStorage(Number* values) const
00421   {
00422     delete [] values;
00423   }
00424 
00425   inline
00426   SmartPtr<DenseVector> DenseVector::MakeNewDenseVector() const
00427   {
00428     return owner_space_->MakeNewDenseVector();
00429   }
00430 
00431   inline
00432   bool DenseVectorSpace::HasStringMetaData(const std::string tag) const
00433   {
00434     StringMetaDataMapType::const_iterator iter;
00435     iter = string_meta_data_.find(tag);
00436 
00437     if (iter != string_meta_data_.end()) {
00438       return true;
00439     }
00440 
00441     return false;
00442   }
00443 
00444   inline
00445   bool DenseVectorSpace::HasIntegerMetaData(const std::string tag) const
00446   {
00447     IntegerMetaDataMapType::const_iterator iter;
00448     iter = integer_meta_data_.find(tag);
00449 
00450     if (iter != integer_meta_data_.end()) {
00451       return true;
00452     }
00453 
00454     return false;
00455   }
00456 
00457   inline
00458   bool DenseVectorSpace::HasNumericMetaData(const std::string tag) const
00459   {
00460     NumericMetaDataMapType::const_iterator iter;
00461     iter = numeric_meta_data_.find(tag);
00462 
00463     if (iter != numeric_meta_data_.end()) {
00464       return true;
00465     }
00466 
00467     return false;
00468   }
00469 
00470   inline
00471   const std::vector<std::string>& DenseVectorSpace::GetStringMetaData(const std::string& tag) const
00472   {
00473     DBG_ASSERT(HasStringMetaData(tag));
00474     StringMetaDataMapType::const_iterator iter;
00475     iter = string_meta_data_.find(tag);
00476     return iter->second;
00477   }
00478 
00479   inline
00480   const std::vector<Index>& DenseVectorSpace::GetIntegerMetaData(const std::string& tag) const
00481   {
00482     DBG_ASSERT(HasIntegerMetaData(tag));
00483     IntegerMetaDataMapType::const_iterator iter;
00484     iter = integer_meta_data_.find(tag);
00485     return iter->second;
00486   }
00487 
00488   inline
00489   const std::vector<Number>& DenseVectorSpace::GetNumericMetaData(const std::string& tag) const
00490   {
00491     DBG_ASSERT(HasNumericMetaData(tag));
00492     NumericMetaDataMapType::const_iterator iter;
00493     iter = numeric_meta_data_.find(tag);
00494     return iter->second;
00495   }
00496 
00497   inline
00498   void DenseVectorSpace::SetStringMetaData(std::string tag, std::vector<std::string> meta_data)
00499   {
00500     string_meta_data_[tag] = meta_data;
00501   }
00502 
00503   inline
00504   void DenseVectorSpace::SetIntegerMetaData(std::string tag, std::vector<Index> meta_data)
00505   {
00506     integer_meta_data_[tag] = meta_data;
00507   }
00508 
00509   inline
00510   void DenseVectorSpace::SetNumericMetaData(std::string tag, std::vector<Number> meta_data)
00511   {
00512     numeric_meta_data_[tag] = meta_data;
00513   }
00514 
00515   inline
00516   const StringMetaDataMapType& DenseVectorSpace::GetStringMetaData() const
00517   {
00518     return string_meta_data_;
00519   }
00520 
00521   inline
00522   const IntegerMetaDataMapType& DenseVectorSpace::GetIntegerMetaData() const
00523   {
00524     return integer_meta_data_;
00525   }
00526 
00527   inline
00528   const NumericMetaDataMapType& DenseVectorSpace::GetNumericMetaData() const
00529   {
00530     return numeric_meta_data_;
00531   }
00532 
00533 } // namespace Ipopt
00534 #endif