IpDenseVector.hpp

Go to the documentation of this file.
00001 // Copyright (C) 2004, 2006 International Business Machines and others.
00002 // All Rights Reserved.
00003 // This code is published under the Common Public License.
00004 //
00005 // $Id: IpDenseVector.hpp 759 2006-07-07 03:07:08Z 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 
00015 namespace Ipopt
00016 {
00017 
00018   /* forward declarations */
00019   class DenseVectorSpace;
00020 
00023   class DenseVector : public Vector
00024   {
00025   public:
00026 
00031     DenseVector(const DenseVectorSpace* owner_space);
00032 
00035     virtual ~DenseVector();
00037 
00041     SmartPtr<DenseVector> MakeNewDenseVector() const;
00042 
00044     void SetValues(const Number *x);
00045 
00051     inline Number* Values();
00052 
00058     inline const Number* Values() const;
00059 
00062     bool IsHomogeneous() const
00063     {
00064       return homogeneous_;
00065     }
00066 
00068     Number Scalar() const
00069     {
00070       DBG_ASSERT(homogeneous_);
00071       return scalar_;
00072     }
00074 
00080     void CopyToPos(Index Pos, const Vector& x);
00084     void CopyFromPos(Index Pos, Vector& x) const;
00086 
00087   protected:
00091     virtual void CopyImpl(const Vector& x);
00092 
00094     virtual void ScalImpl(Number alpha);
00095 
00097     virtual void AxpyImpl(Number alpha, const Vector &x);
00098 
00100     virtual Number DotImpl(const Vector &x) const;
00101 
00103     virtual Number Nrm2Impl() const;
00104 
00106     virtual Number AsumImpl() const;
00107 
00109     virtual Number AmaxImpl() const;
00110 
00112     virtual void SetImpl(Number value);
00113 
00115     virtual void ElementWiseDivideImpl(const Vector& x);
00116 
00118     virtual void ElementWiseMultiplyImpl(const Vector& x);
00119 
00121     virtual void ElementWiseMaxImpl(const Vector& x);
00122 
00124     virtual void ElementWiseMinImpl(const Vector& x);
00125 
00127     virtual void ElementWiseReciprocalImpl();
00128 
00130     virtual void ElementWiseAbsImpl();
00131 
00133     virtual void ElementWiseSqrtImpl();
00134 
00136     virtual void ElementWiseSgnImpl();
00137 
00139     virtual void AddScalarImpl(Number scalar);
00140 
00142     virtual Number MaxImpl() const;
00143 
00145     virtual Number MinImpl() const;
00146 
00148     virtual Number SumImpl() const;
00149 
00151     virtual Number SumLogsImpl() const;
00152 
00157     void AddTwoVectorsImpl(Number a, const Vector& v1,
00158                            Number b, const Vector& v2, Number c);
00160     Number FracToBoundImpl(const Vector& delta, Number tau) const;
00162     void AddVectorQuotientImpl(Number a, const Vector& z, const Vector& s,
00163                                Number c);
00165 
00168     /* Print the entire vector with padding */
00169     virtual void PrintImpl(const Journalist& jnlst,
00170                            EJournalLevel level,
00171                            EJournalCategory category,
00172                            const std::string& name,
00173                            Index indent,
00174                            const std::string& prefix) const;
00176 
00177   private:
00187     DenseVector();
00188 
00190     DenseVector(const DenseVector&);
00191 
00193     void operator=(const DenseVector&);
00195 
00199     const DenseVectorSpace* owner_space_;
00200 
00202     Number* values_;
00203 
00206     Number* values_allocated();
00207 
00210     bool initialized_;
00211 
00216     bool homogeneous_;
00217 
00220     Number scalar_;
00221 
00224     void set_values_from_scalar();
00225   };
00226 
00229   class DenseVectorSpace : public VectorSpace
00230   {
00231   public:
00237     DenseVectorSpace(Index dim)
00238         :
00239         VectorSpace(dim)
00240     {}
00241 
00243     ~DenseVectorSpace()
00244     {}
00246 
00248     DenseVector* MakeNewDenseVector() const
00249     {
00250       return new DenseVector(this);
00251     }
00252 
00256     virtual Vector* MakeNew() const
00257     {
00258       return MakeNewDenseVector();
00259     }
00260 
00267     Number* AllocateInternalStorage() const;
00268 
00270     void FreeInternalStorage(Number* values) const;
00272   };
00273 
00274   // inline functions
00275   inline Number* DenseVector::Values()
00276   {
00277     // Here we assume that every time someone requests this direct raw
00278     // pointer, the data is going to change and the Tag for this
00279     // vector has to be updated.
00280 
00281     if (initialized_ && homogeneous_) {
00282       // If currently the vector is a homogeneous vector, set all elements
00283       // explicitly to this value
00284       set_values_from_scalar();
00285     }
00286     ObjectChanged();
00287     initialized_= true;
00288     homogeneous_ = false;
00289     return values_allocated();
00290   }
00291 
00292   inline const Number* DenseVector::Values() const
00293   {
00294     DBG_ASSERT(initialized_ && (Dim()==0 || values_));
00295     return values_;
00296   }
00297 
00298   inline Number* DenseVector::values_allocated()
00299   {
00300     if (values_==NULL) {
00301       values_ = owner_space_->AllocateInternalStorage();
00302     }
00303     return values_;
00304   }
00305 
00306   inline
00307   Number* DenseVectorSpace::AllocateInternalStorage() const
00308   {
00309     if (Dim()>0) {
00310       return new Number[Dim()];
00311     }
00312     else {
00313       return NULL;
00314     }
00315   }
00316 
00317   inline
00318   void DenseVectorSpace::FreeInternalStorage(Number* values) const
00319   {
00320     delete [] values;
00321   }
00322 
00323   inline
00324   SmartPtr<DenseVector> DenseVector::MakeNewDenseVector() const
00325   {
00326     return owner_space_->MakeNewDenseVector();
00327   }
00328 
00329 } // namespace Ipopt
00330 #endif

Generated on Fri Sep 26 07:44:11 2008 for SimTKcore by  doxygen 1.5.6