SundanceEvalVector.hpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                             Sundance
00005 //                 Copyright 2011 Sandia Corporation
00006 // 
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Kevin Long (kevin.long@ttu.edu)
00038 // 
00039 
00040 /* @HEADER@ */
00041 
00042 #ifndef SUNDANCE_EVALVECTOR_H
00043 #define SUNDANCE_EVALVECTOR_H
00044 
00045 #include "SundanceDefs.hpp"
00046 #include "Teuchos_RefCountPtr.hpp"
00047 #include "Teuchos_Array.hpp"
00048 #include "SundanceObjectWithVerbosity.hpp"
00049 #include "SundanceUnaryFunctor.hpp"
00050 #include "SundanceNoncopyable.hpp"
00051 
00052 
00053 namespace Sundance
00054 {
00055 using namespace Teuchos;
00056 class TempStack;
00057 /**
00058  *
00059  */
00060 class EvalVector : public Noncopyable,
00061                    public ObjectWithClassVerbosity<EvalVector>
00062 {
00063   friend class EvalManager;
00064   friend class TempStack;
00065 
00066 private:
00067 
00068   /** */
00069   EvalVector(TempStack* s);
00070 
00071   /** */
00072   EvalVector(TempStack* s, const RCP<Array<double> >& data,
00073     const std::string& str);
00074 
00075 
00076 public:
00077   /** 
00078    * EvalVector has a nontrivial destructor. Upon destruction, 
00079    * the vector's underlying data object is not destroyed, but rather
00080    * is put back on the stack of temporary vectors. 
00081    */
00082   ~EvalVector();
00083 
00084   /** \name Mathematical operations */
00085   //@{
00086 
00087   /** */
00088   void add_SV(const double& alpha, 
00089     const EvalVector* B) ;
00090 
00091   /**
00092    * Perform the operation 
00093    * \f[ 
00094    * this = this + alpha*B*C
00095    * \f]
00096    * which shows up in the chain rule expansion of a second derivative.
00097    * 
00098    */
00099   void add_SVV(const double& alpha,
00100     const EvalVector* B,
00101     const EvalVector* C) ;
00102 
00103   /** */
00104   void add_V(const EvalVector* A) ;
00105 
00106   /** */
00107   void add_S(const double& alpha);
00108 
00109   /**
00110    * Perform the operation 
00111    * \f[ 
00112    * this = this + A*B
00113    * \f]
00114    * which shows up in the chain rule expansion of a second derivative.
00115    * 
00116    */
00117   void add_VV(const EvalVector* A,
00118     const EvalVector* B) ;
00119 
00120 
00121   /**  
00122    * Perform a scaled addition with another vector,
00123    * \f[ 
00124    * this = \alpha this + \beta C
00125    * \f]
00126    * The operation is done in-place, overwriting the old values of the
00127    * vector. 
00128    */
00129   void multiply_S_add_SV(const double& alpha, 
00130     const double& beta,
00131     const EvalVector* C) ;
00132 
00133   /** Scale and add a constant to this vector. 
00134    * The operation is done in-place, overwriting the old values of
00135    * the vector. Each element x[i] is updated as:
00136    * \f[
00137    * this = alpha * this + beta
00138    * \f]
00139    */
00140   void multiply_S_add_S(const double& alpha,
00141     const double& beta) ;
00142 
00143   /**
00144    * Perform the operation 
00145    * \f[ 
00146    * this = this*A + B*C*D
00147    * \f]
00148    * which shows up in the chain rule expansion of a second derivative.
00149    * 
00150    */
00151   void multiply_V_add_VVV(const EvalVector* A,
00152     const EvalVector* B,
00153     const EvalVector* C,
00154     const EvalVector* D) ;
00155 
00156 
00157   /**
00158    * Perform the operation 
00159    * \f[ 
00160    * this = this*A + beta*C*D
00161    * \f]
00162    * which shows up in the chain rule expansion of a second derivative.
00163    * 
00164    */
00165   void multiply_V_add_SVV(const EvalVector* A,
00166     const double& beta,
00167     const EvalVector* C,
00168     const EvalVector* D) ;
00169 
00170   /**
00171    * Perform the operation 
00172    * \f[ 
00173    * this = this*A + beta*C
00174    * \f]
00175    * which shows up in the chain rule expansion of a second derivative.
00176    * 
00177    */
00178   void multiply_V_add_SV(const EvalVector* A,
00179     const double& beta,
00180     const EvalVector* C) ;
00181 
00182   /**
00183    * Perform the operation 
00184    * \f[ 
00185    * this = this*A*B
00186    * \f]
00187    * which shows up in the chain rule expansion of a second derivative.
00188    * 
00189    */
00190   void multiply_VV(const EvalVector* A,
00191     const EvalVector* B) ;
00192 
00193   /**
00194    * Perform the operation 
00195    * \f[ 
00196    * this = this*alpha*B
00197    * \f]
00198    * which shows up in the chain rule expansion of a second derivative.
00199    * 
00200    */
00201   void multiply_SV(const double& alpha,
00202     const EvalVector* B) ;
00203 
00204   /**
00205    * Perform the operation 
00206    * \f[ 
00207    * this = this*A
00208    * \f]
00209    * which shows up in the chain rule expansion of a second derivative.
00210    * 
00211    */
00212   void multiply_V(const EvalVector* A) ;
00213 
00214   /**
00215    * Perform the operation 
00216    * \f[ 
00217    * this = this*alpha
00218    * \f]
00219    * which shows up in the chain rule expansion of a second derivative.
00220    * 
00221    */
00222   void multiply_S(const double& alpha) ;
00223 
00224   /**
00225    *
00226    */
00227   void setTo_S_add_SVV(const double& alpha,
00228     const double& beta,
00229     const EvalVector* C,
00230     const EvalVector* D);
00231 
00232   /**
00233    *
00234    */
00235   void setTo_S_add_VV(const double& alpha,
00236     const EvalVector* B,
00237     const EvalVector* C);
00238 
00239   /**
00240    *
00241    */
00242   void setTo_S_add_SV(const double& alpha,
00243     const double& beta,
00244     const EvalVector* C);
00245 
00246   /** 
00247    *
00248    */
00249   void setTo_S_add_V(const double& alpha,
00250     const EvalVector* B);
00251 
00252 
00253   /**
00254    *
00255    */
00256   void setTo_V(const EvalVector* A);
00257 
00258   /**
00259    *
00260    */
00261   void setTo_VV(const EvalVector* A,
00262     const EvalVector* B);
00263 
00264   /**
00265    *
00266    */
00267   void setTo_SV(const double& alpha,
00268     const EvalVector* B);
00269 
00270   /**
00271    *
00272    */
00273   void setTo_SVV(const double& alpha,
00274     const EvalVector* B,
00275     const EvalVector* C);
00276 
00277       
00278 
00279 
00280   /**
00281    * Set every element to a constant value
00282    */
00283   void setToConstant(const double& alpha) ;
00284 
00285   /** 
00286    * Apply a unary function
00287    */
00288   void applyUnaryOperator(const UnaryFunctor* func, 
00289     Array<RCP<EvalVector> >& opDerivs);
00290       
00291       
00292   /** */
00293   RCP<EvalVector> clone() const ;
00294 
00295   /** */
00296   void resize(int n);
00297 
00298   /** */
00299   int length() const {return data_->size();}
00300       
00301   /** */
00302   void print(std::ostream& os) const ;
00303 
00304   /** */
00305   const double * start() const {return &((*data_)[0]);}
00306 
00307   /** */
00308   double * start() {return &((*data_)[0]);}
00309 
00310   const std::string& str() const {return str_;}
00311 
00312   void setString(const std::string& str) {str_ = str;}
00313 
00314   inline static bool& shadowOps() {static bool rtn = false; return rtn;}
00315 
00316   bool isValid() const {return data_.get() != 0 && s_ != 0;}
00317   //@}
00318 
00319       
00320 
00321   inline static double& totalFlops() {static double rtn = 0; return rtn;}
00322 
00323 private:
00324 
00325   inline static void addFlops(const double& flops) {totalFlops() += flops;}
00326 
00327   mutable TempStack* s_;
00328 
00329   RCP<Array<double> > data_;
00330 
00331   std::string str_;
00332 
00333 };
00334 
00335 
00336 
00337 }
00338 
00339 namespace std
00340 {
00341 inline ostream& operator<<(std::ostream& os, 
00342   const Sundance::EvalVector& vec)
00343 {
00344   vec.print(os);
00345   return os;
00346 }
00347 }
00348 
00349                   
00350 #endif

Site Contact