SundanceEvalVector.hpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                              Sundance
00005 //                 Copyright (2005) Sandia Corporation
00006 // 
00007 // Copyright (year first published) Sandia Corporation.  Under the terms 
00008 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government 
00009 // retains certain rights in this software.
00010 // 
00011 // This library is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Lesser General Public License as
00013 // published by the Free Software Foundation; either version 2.1 of the
00014 // License, or (at your option) any later version.
00015 //  
00016 // This library is distributed in the hope that it will be useful, but
00017 // WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 // Lesser General Public License for more details.
00020 //                                                                                 
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License along with this library; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00024 // USA                                                                                
00025 // Questions? Contact Kevin Long (krlong@sandia.gov), 
00026 // Sandia National Laboratories, Livermore, California, USA
00027 // 
00028 // ************************************************************************
00029 /* @HEADER@ */
00030 
00031 #ifndef SUNDANCE_EVALVECTOR_H
00032 #define SUNDANCE_EVALVECTOR_H
00033 
00034 #include "SundanceDefs.hpp"
00035 #include "Teuchos_RefCountPtr.hpp"
00036 #include "Teuchos_Array.hpp"
00037 #include "SundanceObjectWithVerbosity.hpp"
00038 #include "SundanceUnaryFunctor.hpp"
00039 #include "SundanceNoncopyable.hpp"
00040 
00041 
00042 namespace Sundance
00043 {
00044 using namespace Teuchos;
00045 class TempStack;
00046 /**
00047  *
00048  */
00049 class EvalVector : public Noncopyable,
00050                    public ObjectWithClassVerbosity<EvalVector>
00051 {
00052   friend class EvalManager;
00053   friend class TempStack;
00054 
00055 private:
00056 
00057   /** */
00058   EvalVector(TempStack* s);
00059 
00060   /** */
00061   EvalVector(TempStack* s, const RCP<Array<double> >& data,
00062     const std::string& str);
00063 
00064 
00065 public:
00066   /** 
00067    * EvalVector has a nontrivial destructor. Upon destruction, 
00068    * the vector's underlying data object is not destroyed, but rather
00069    * is put back on the stack of temporary vectors. 
00070    */
00071   ~EvalVector();
00072 
00073   /** \name Mathematical operations */
00074   //@{
00075 
00076   /** */
00077   void add_SV(const double& alpha, 
00078     const EvalVector* B) ;
00079 
00080   /**
00081    * Perform the operation 
00082    * \f[ 
00083    * this = this + alpha*B*C
00084    * \f]
00085    * which shows up in the chain rule expansion of a second derivative.
00086    * 
00087    */
00088   void add_SVV(const double& alpha,
00089     const EvalVector* B,
00090     const EvalVector* C) ;
00091 
00092   /** */
00093   void add_V(const EvalVector* A) ;
00094 
00095   /** */
00096   void add_S(const double& alpha);
00097 
00098   /**
00099    * Perform the operation 
00100    * \f[ 
00101    * this = this + A*B
00102    * \f]
00103    * which shows up in the chain rule expansion of a second derivative.
00104    * 
00105    */
00106   void add_VV(const EvalVector* A,
00107     const EvalVector* B) ;
00108 
00109 
00110   /**  
00111    * Perform a scaled addition with another vector,
00112    * \f[ 
00113    * this = \alpha this + \beta C
00114    * \f]
00115    * The operation is done in-place, overwriting the old values of the
00116    * vector. 
00117    */
00118   void multiply_S_add_SV(const double& alpha, 
00119     const double& beta,
00120     const EvalVector* C) ;
00121 
00122   /** Scale and add a constant to this vector. 
00123    * The operation is done in-place, overwriting the old values of
00124    * the vector. Each element x[i] is updated as:
00125    * \f[
00126    * this = alpha * this + beta
00127    * \f]
00128    */
00129   void multiply_S_add_S(const double& alpha,
00130     const double& beta) ;
00131 
00132   /**
00133    * Perform the operation 
00134    * \f[ 
00135    * this = this*A + B*C*D
00136    * \f]
00137    * which shows up in the chain rule expansion of a second derivative.
00138    * 
00139    */
00140   void multiply_V_add_VVV(const EvalVector* A,
00141     const EvalVector* B,
00142     const EvalVector* C,
00143     const EvalVector* D) ;
00144 
00145 
00146   /**
00147    * Perform the operation 
00148    * \f[ 
00149    * this = this*A + beta*C*D
00150    * \f]
00151    * which shows up in the chain rule expansion of a second derivative.
00152    * 
00153    */
00154   void multiply_V_add_SVV(const EvalVector* A,
00155     const double& beta,
00156     const EvalVector* C,
00157     const EvalVector* D) ;
00158 
00159   /**
00160    * Perform the operation 
00161    * \f[ 
00162    * this = this*A + beta*C
00163    * \f]
00164    * which shows up in the chain rule expansion of a second derivative.
00165    * 
00166    */
00167   void multiply_V_add_SV(const EvalVector* A,
00168     const double& beta,
00169     const EvalVector* C) ;
00170 
00171   /**
00172    * Perform the operation 
00173    * \f[ 
00174    * this = this*A*B
00175    * \f]
00176    * which shows up in the chain rule expansion of a second derivative.
00177    * 
00178    */
00179   void multiply_VV(const EvalVector* A,
00180     const EvalVector* B) ;
00181 
00182   /**
00183    * Perform the operation 
00184    * \f[ 
00185    * this = this*alpha*B
00186    * \f]
00187    * which shows up in the chain rule expansion of a second derivative.
00188    * 
00189    */
00190   void multiply_SV(const double& alpha,
00191     const EvalVector* B) ;
00192 
00193   /**
00194    * Perform the operation 
00195    * \f[ 
00196    * this = this*A
00197    * \f]
00198    * which shows up in the chain rule expansion of a second derivative.
00199    * 
00200    */
00201   void multiply_V(const EvalVector* A) ;
00202 
00203   /**
00204    * Perform the operation 
00205    * \f[ 
00206    * this = this*alpha
00207    * \f]
00208    * which shows up in the chain rule expansion of a second derivative.
00209    * 
00210    */
00211   void multiply_S(const double& alpha) ;
00212 
00213   /**
00214    *
00215    */
00216   void setTo_S_add_SVV(const double& alpha,
00217     const double& beta,
00218     const EvalVector* C,
00219     const EvalVector* D);
00220 
00221   /**
00222    *
00223    */
00224   void setTo_S_add_VV(const double& alpha,
00225     const EvalVector* B,
00226     const EvalVector* C);
00227 
00228   /**
00229    *
00230    */
00231   void setTo_S_add_SV(const double& alpha,
00232     const double& beta,
00233     const EvalVector* C);
00234 
00235   /** 
00236    *
00237    */
00238   void setTo_S_add_V(const double& alpha,
00239     const EvalVector* B);
00240 
00241 
00242   /**
00243    *
00244    */
00245   void setTo_V(const EvalVector* A);
00246 
00247   /**
00248    *
00249    */
00250   void setTo_VV(const EvalVector* A,
00251     const EvalVector* B);
00252 
00253   /**
00254    *
00255    */
00256   void setTo_SV(const double& alpha,
00257     const EvalVector* B);
00258 
00259   /**
00260    *
00261    */
00262   void setTo_SVV(const double& alpha,
00263     const EvalVector* B,
00264     const EvalVector* C);
00265 
00266       
00267 
00268 
00269   /**
00270    * Set every element to a constant value
00271    */
00272   void setToConstant(const double& alpha) ;
00273 
00274   /** 
00275    * Apply a unary function
00276    */
00277   void applyUnaryOperator(const UnaryFunctor* func, 
00278     Array<RCP<EvalVector> >& opDerivs);
00279       
00280       
00281   /** */
00282   RCP<EvalVector> clone() const ;
00283 
00284   /** */
00285   void resize(int n);
00286 
00287   /** */
00288   int length() const {return data_->size();}
00289       
00290   /** */
00291   void print(std::ostream& os) const ;
00292 
00293   /** */
00294   const double * start() const {return &((*data_)[0]);}
00295 
00296   /** */
00297   double * start() {return &((*data_)[0]);}
00298 
00299   const std::string& str() const {return str_;}
00300 
00301   void setString(const std::string& str) {str_ = str;}
00302 
00303   inline static bool& shadowOps() {static bool rtn = false; return rtn;}
00304 
00305   bool isValid() const {return data_.get() != 0 && s_ != 0;}
00306   //@}
00307 
00308       
00309 
00310   inline static double& totalFlops() {static double rtn = 0; return rtn;}
00311 
00312 private:
00313 
00314   inline static void addFlops(const double& flops) {totalFlops() += flops;}
00315 
00316   mutable TempStack* s_;
00317 
00318   RCP<Array<double> > data_;
00319 
00320   std::string str_;
00321 
00322 };
00323 
00324 
00325 
00326 }
00327 
00328 namespace std
00329 {
00330 inline ostream& operator<<(std::ostream& os, 
00331   const Sundance::EvalVector& vec)
00332 {
00333   vec.print(os);
00334   return os;
00335 }
00336 }
00337 
00338                   
00339 #endif

Site Contact