Sacado_ELRFad_SFad.hpp

Go to the documentation of this file.
00001 // $Id: Sacado_ELRFad_SFad.hpp,v 1.2 2007/11/14 00:18:18 etphipp Exp $ 
00002 // $Source: /space/CVS/Trilinos/packages/sacado/src/Sacado_ELRFad_SFad.hpp,v $ 
00003 // @HEADER
00004 // ***********************************************************************
00005 // 
00006 //                           Sacado Package
00007 //                 Copyright (2006) Sandia Corporation
00008 // 
00009 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00010 // the U.S. Government retains certain rights in this software.
00011 // 
00012 // This library is free software; you can redistribute it and/or modify
00013 // it under the terms of the GNU Lesser General Public License as
00014 // published by the Free Software Foundation; either version 2.1 of the
00015 // License, or (at your option) any later version.
00016 //  
00017 // This library is distributed in the hope that it will be useful, but
00018 // WITHOUT ANY WARRANTY; without even the implied warranty of
00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020 // Lesser General Public License for more details.
00021 //  
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License along with this library; if not, write to the Free Software
00024 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00025 // USA
00026 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
00027 // (etphipp@sandia.gov).
00028 // 
00029 // ***********************************************************************
00030 //
00031 // The forward-mode AD classes in Sacado are a derivative work of the
00032 // expression template classes in the Fad package by Nicolas Di Cesare.  
00033 // The following banner is included in the original Fad source code:
00034 //
00035 // ************ DO NOT REMOVE THIS BANNER ****************
00036 //
00037 //  Nicolas Di Cesare <Nicolas.Dicesare@ann.jussieu.fr>
00038 //  http://www.ann.jussieu.fr/~dicesare
00039 //
00040 //            CEMRACS 98 : C++ courses, 
00041 //         templates : new C++ techniques 
00042 //            for scientific computing 
00043 // 
00044 //********************************************************
00045 //
00046 //  A short implementation ( not all operators and 
00047 //  functions are overloaded ) of 1st order Automatic
00048 //  Differentiation in forward mode (FAD) using
00049 //  EXPRESSION TEMPLATES.
00050 //
00051 //********************************************************
00052 // @HEADER
00053 
00054 #ifndef SACADO_ELRFAD_SFAD_HPP
00055 #define SACADO_ELRFAD_SFAD_HPP
00056 
00057 #include "Sacado_ELRFad_SFadTraits.hpp"
00058 #include "Sacado_ELRFad_Expression.hpp"
00059 #include "Sacado_StaticArrayTraits.hpp"
00060 
00061 namespace Sacado {
00062 
00063   namespace ELRFad {
00064 
00066     template <typename T, int Num> 
00067     struct SFadExprTag {};
00068 
00076     template <typename T, int Num> 
00077     class Expr< SFadExprTag<T,Num> > {
00078 
00079     public:
00080 
00082       typedef T value_type;
00083 
00085       typedef Expr< SFadExprTag<T,Num> > base_expr_type;
00086 
00088       static const int num_args = 1;
00089 
00094 
00096       Expr() : val_( T(0.)) { ss_array<T>::zero(dx_, Num); }
00097 
00099 
00102       Expr(const T & x) : val_(x)  { ss_array<T>::zero(dx_, Num); }
00103 
00105 
00108       Expr(const int sz, const T & x);
00109 
00111 
00116       Expr(const int sz, const int i, const T & x);
00117 
00119       Expr(const Expr& x) : val_(x.val_) { 
00120   //ss_array<T>::copy(x.dx_, dx_, Num);
00121   for (int i=0; i<Num; i++)
00122     dx_[i] = x.dx_[i];
00123       }
00124 
00126       template <typename S> Expr(const Expr<S>& x);
00127 
00129       ~Expr() {}
00130 
00132 
00138       void diff(const int ith, const int n);
00139 
00141 
00145       void resize(int sz);
00146 
00148       void zero() { ss_array<T>::zero(dx_, Num); }
00149 
00151 
00156 
00158       const T& val() const { return val_;}
00159 
00161       T& val() { return val_;}
00162 
00164 
00169 
00171       int size() const { return Num;}
00172 
00174       bool hasFastAccess() const { return true; }
00175 
00177       bool isPassive() const { return false; }
00178       
00180       void setIsConstant(bool is_const) {}
00181 
00183       const T* dx() const { return &(dx_[0]);}
00184 
00186       const T& dx(int i) const { return dx_[i]; }
00187     
00189       T& fastAccessDx(int i) { return dx_[i];}
00190 
00192       const T& fastAccessDx(int i) const { return dx_[i];}
00193 
00195       void computePartials(const T& bar, T partials[]) const { 
00196   partials[0] = bar; 
00197       }
00198 
00200       void getTangents(int i, T dots[]) const { 
00201   dots[0] = this->dx_[i];
00202       }
00203 
00205       template <int Arg>
00206       bool isActive() const { return true; }
00207 
00209       template <int Arg>
00210       T getTangent(int i) const { return this->dx_[i]; }
00211     
00213 
00218 
00220       Expr< SFadExprTag<T,Num> >& operator=(const T& val);
00221 
00223       Expr< SFadExprTag<T,Num> >& 
00224       operator=(const Expr< SFadExprTag<T,Num> >& x);
00225 
00227       template <typename S> 
00228       Expr< SFadExprTag<T,Num> >& operator=(const Expr<S>& x); 
00229 
00231 
00236 
00238       Expr< SFadExprTag<T,Num> >& operator += (const T& x);
00239 
00241       Expr< SFadExprTag<T,Num> >& operator -= (const T& x);
00242 
00244       Expr< SFadExprTag<T,Num> >& operator *= (const T& x);
00245 
00247       Expr< SFadExprTag<T,Num> >& operator /= (const T& x);
00248 
00250       template <typename S> 
00251       Expr< SFadExprTag<T,Num> >& operator += (const Expr<S>& x);
00252 
00254       template <typename S> 
00255       Expr< SFadExprTag<T,Num> >& operator -= (const Expr<S>& x);
00256   
00258       template <typename S> 
00259       Expr< SFadExprTag<T,Num> >& operator *= (const Expr<S>& x);
00260 
00262       template <typename S> 
00263       Expr< SFadExprTag<T,Num> >& operator /= (const Expr<S>& x);
00264 
00266 
00267     protected:
00268 
00270       T val_;
00271 
00273       T dx_[Num];
00274 
00275       // Functor for mpl::for_each to compute the local accumulation
00276       // of a tangent derivative
00277       template <typename ExprT>
00278       struct LocalAccumOp {
00279   typedef typename ExprT::value_type value_type;
00280   static const int N = ExprT::num_args;
00281   const ExprT& x;
00282   mutable value_type t;
00283   value_type partials[N];
00284   int i;
00285   inline LocalAccumOp(const ExprT& x_) :
00286     x(x_) { x.computePartials(value_type(1.), partials); }
00287   template <typename ArgT>
00288   inline void operator () (ArgT arg) const {
00289     const int Arg = ArgT::value;
00290     if (x.template isActive<Arg>())
00291       t += partials[Arg] * x.template getTangent<Arg>(i);
00292   }
00293       };
00294 
00295     }; // class Expr<SFadExprTag>
00296 
00298 
00314     template <typename ValueT, int Num,
00315         typename ScalarT = typename ScalarValueType<ValueT>::type >
00316     class SFad : 
00317       public Expr< SFadExprTag<ValueT,Num > > {
00318 
00319     public:
00320 
00325 
00327 
00330       SFad() : 
00331   Expr< SFadExprTag< ValueT,Num > >() {}
00332 
00334 
00337       SFad(const ValueT & x) : 
00338   Expr< SFadExprTag< ValueT,Num > >(x) {}
00339 
00341 
00344       SFad(const ScalarT& x) : 
00345   Expr< SFadExprTag< ValueT,Num > >(ValueT(x)) {}
00346 
00348 
00351       SFad(const int sz, const ValueT & x) : 
00352   Expr< SFadExprTag< ValueT,Num > >(sz,x) {}
00353 
00355 
00360       SFad(const int sz, const int i, const ValueT & x) : 
00361   Expr< SFadExprTag< ValueT,Num > >(sz,i,x) {}
00362 
00364       SFad(const SFad& x) : 
00365   Expr< SFadExprTag< ValueT,Num > >(x) {}
00366 
00368       template <typename S> SFad(const Expr<S>& x) : 
00369   Expr< SFadExprTag< ValueT,Num > >(x) {}
00370 
00372 
00374       ~SFad() {}
00375 
00377       SFad& operator=(const ValueT& val) {
00378   Expr< SFadExprTag< ValueT,Num > >::operator=(val);
00379   return *this;
00380       }
00381 
00383       SFad& operator=(const ScalarT& val) {
00384   Expr< SFadExprTag< ValueT,Num > >::operator=(ValueT(val));
00385   return *this;
00386       }
00387 
00389       SFad& operator=(const SFad& x) {
00390   Expr< SFadExprTag< ValueT,Num > >::operator=(static_cast<const Expr< SFadExprTag< ValueT,Num > >&>(x));
00391   return *this;
00392       }
00393 
00395       template <typename S> SFad& operator=(const Expr<S>& x) 
00396       {
00397   Expr< SFadExprTag< ValueT,Num > >::operator=(x);
00398   return *this;
00399       }
00400 
00401     }; // class SFad<ValueT,Num,ScalarT>
00402 
00404 
00409     template <typename ValueT, int Num>
00410     class SFad<ValueT,Num,ValueT> : 
00411       public Expr< SFadExprTag<ValueT,Num >  >{
00412 
00413     public:
00414 
00419 
00421 
00424       SFad() : Expr< SFadExprTag< ValueT,Num > >() {}
00425 
00427 
00430       SFad(const ValueT & x) : 
00431   Expr< SFadExprTag< ValueT,Num > >(x) {}
00432 
00434 
00437       SFad(const int sz, const ValueT & x) : 
00438   Expr< SFadExprTag< ValueT,Num > >(sz,x) {}
00439 
00441 
00446       SFad(const int sz, const int i, const ValueT & x) : 
00447   Expr< SFadExprTag< ValueT,Num > >(sz,i,x) {}
00448 
00450       SFad(const SFad& x) : 
00451   Expr< SFadExprTag< ValueT,Num > >(x) {}
00452 
00454       template <typename S> SFad(const Expr<S>& x) : 
00455   Expr< SFadExprTag< ValueT,Num > >(x) {}
00456 
00458 
00460       ~SFad() {}
00461 
00463       SFad& operator=(const ValueT& val) {
00464   Expr< SFadExprTag< ValueT,Num > >::operator=(val);
00465   return *this;
00466       }
00467 
00469       SFad& operator=(const SFad& x) {
00470   Expr< SFadExprTag< ValueT,Num > >::operator=(static_cast<const Expr< SFadExprTag< ValueT,Num > >&>(x));
00471   return *this;
00472       }
00473 
00475       template <typename S> SFad& operator=(const Expr<S>& x) 
00476       {
00477   Expr< SFadExprTag< ValueT,Num > >::operator=(x);
00478   return *this;
00479       }
00480 
00481     }; // class SFad<ValueT,Num>
00482 
00484     template <typename T, int Num>
00485     struct ExprPromote< Expr< SFadExprTag<T,Num> >, T > {
00486       typedef Expr< SFadExprTag<T,Num> > type;
00487     };
00488     
00490     template <typename T, int Num>
00491     struct ExprPromote< T, Expr< SFadExprTag<T,Num> > > {
00492       typedef Expr< SFadExprTag<T,Num> > type;
00493     };
00494 
00495     template <typename T, int Num>
00496     std::ostream& operator << (std::ostream& os, 
00497              const Expr< SFadExprTag<T,Num> >& x) {
00498       os << x.val() << "  [";
00499       
00500       for (int i=0; i< x.size(); i++) {
00501   os << " " << x.dx(i);
00502       }
00503 
00504       os << " ]\n";
00505       return os;
00506     }
00507 
00508   } // namespace ELRFad
00509 
00510 } // namespace Sacado
00511 
00512 #include "Sacado_ELRFad_SFadImp.hpp"
00513 #include "Sacado_ELRFad_Ops.hpp"
00514 
00515 #endif // SACADO_ELRFAD_SFAD_HPP

Generated on Wed May 12 21:59:04 2010 for Sacado Package Browser (Single Doxygen Collection) by  doxygen 1.4.7