Sacado_ELRFad_SFad.hpp

Go to the documentation of this file.
00001 // $Id$ 
00002 // $Source$ 
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 #include "Sacado_dummy_arg.hpp"
00061 
00062 namespace Sacado {
00063 
00064   namespace ELRFad {
00065 
00067     template <typename T, int Num> 
00068     struct SFadExprTag {};
00069 
00077     template <typename T, int Num> 
00078     class Expr< SFadExprTag<T,Num> > {
00079 
00080     public:
00081 
00083       typedef T value_type;
00084 
00086       typedef Expr< SFadExprTag<T,Num> > base_expr_type;
00087 
00089       static const int num_args = 1;
00090 
00095 
00097       Expr() : val_( T(0.)) { ss_array<T>::zero(dx_, Num); }
00098 
00100 
00103       Expr(const T & x) : val_(x)  { ss_array<T>::zero(dx_, Num); }
00104 
00106 
00109       Expr(const int sz, const T & x);
00110 
00112 
00117       Expr(const int sz, const int i, const T & x);
00118 
00120       Expr(const Expr& x) : val_(x.val_) { 
00121   //ss_array<T>::copy(x.dx_, dx_, Num);
00122   for (int i=0; i<Num; i++)
00123     dx_[i] = x.dx_[i];
00124       }
00125 
00127       template <typename S> Expr(const Expr<S>& x);
00128 
00130       ~Expr() {}
00131 
00133 
00139       void diff(const int ith, const int n);
00140 
00142 
00146       void resize(int sz);
00147 
00149       void zero() { ss_array<T>::zero(dx_, Num); }
00150 
00152 
00157 
00159       const T& val() const { return val_;}
00160 
00162       T& val() { return val_;}
00163 
00165 
00170 
00172       int size() const { return Num;}
00173 
00178       int availableSize() const { return Num; }
00179 
00181       bool hasFastAccess() const { return true; }
00182 
00184       bool isPassive() const { return false; }
00185       
00187       void setIsConstant(bool is_const) {}
00188 
00190       const T* dx() const { return &(dx_[0]);}
00191 
00193       const T& dx(int i) const { return dx_[i]; }
00194     
00196       T& fastAccessDx(int i) { return dx_[i];}
00197 
00199       const T& fastAccessDx(int i) const { return dx_[i];}
00200 
00202       void computePartials(const T& bar, T partials[]) const { 
00203   partials[0] = bar; 
00204       }
00205 
00207       void getTangents(int i, T dots[]) const { 
00208   dots[0] = this->dx_[i];
00209       }
00210 
00212       template <int Arg>
00213       bool isActive() const { return true; }
00214 
00216       template <int Arg>
00217       T getTangent(int i) const { return this->dx_[i]; }
00218     
00220 
00225 
00227       Expr< SFadExprTag<T,Num> >& operator=(const T& val);
00228 
00230       Expr< SFadExprTag<T,Num> >& 
00231       operator=(const Expr< SFadExprTag<T,Num> >& x);
00232 
00234       template <typename S> 
00235       Expr< SFadExprTag<T,Num> >& operator=(const Expr<S>& x); 
00236 
00238 
00243 
00245       Expr< SFadExprTag<T,Num> >& operator += (const T& x);
00246 
00248       Expr< SFadExprTag<T,Num> >& operator -= (const T& x);
00249 
00251       Expr< SFadExprTag<T,Num> >& operator *= (const T& x);
00252 
00254       Expr< SFadExprTag<T,Num> >& operator /= (const T& x);
00255 
00257       template <typename S> 
00258       Expr< SFadExprTag<T,Num> >& operator += (const Expr<S>& x);
00259 
00261       template <typename S> 
00262       Expr< SFadExprTag<T,Num> >& operator -= (const Expr<S>& x);
00263   
00265       template <typename S> 
00266       Expr< SFadExprTag<T,Num> >& operator *= (const Expr<S>& x);
00267 
00269       template <typename S> 
00270       Expr< SFadExprTag<T,Num> >& operator /= (const Expr<S>& x);
00271 
00273 
00274     protected:
00275 
00277       T val_;
00278 
00280       T dx_[Num];
00281 
00282       // Functor for mpl::for_each to compute the local accumulation
00283       // of a tangent derivative
00284       template <typename ExprT>
00285       struct LocalAccumOp {
00286   typedef typename ExprT::value_type value_type;
00287   static const int N = ExprT::num_args;
00288   const ExprT& x;
00289   mutable value_type t;
00290   value_type partials[N];
00291   int i;
00292   inline LocalAccumOp(const ExprT& x_) :
00293     x(x_) { x.computePartials(value_type(1.), partials); }
00294   template <typename ArgT>
00295   inline void operator () (ArgT arg) const {
00296     const int Arg = ArgT::value;
00297     if (x.template isActive<Arg>())
00298       t += partials[Arg] * x.template getTangent<Arg>(i);
00299   }
00300       };
00301 
00302     }; // class Expr<SFadExprTag>
00303 
00314     template <typename ValueT, int Num>
00315     class SFad : 
00316       public Expr< SFadExprTag<ValueT,Num > > {
00317 
00318     public:
00319 
00321       typedef typename ScalarType<ValueT>::type ScalarT;
00322 
00324       template <typename T> 
00325       struct apply {
00326   typedef SFad<T,Num> type;
00327       };
00328 
00333 
00335 
00338       SFad() : 
00339   Expr< SFadExprTag< ValueT,Num > >() {}
00340 
00342 
00345       SFad(const ValueT & x) : 
00346   Expr< SFadExprTag< ValueT,Num > >(x) {}
00347 
00349 
00353       SFad(const typename dummy<ValueT,ScalarT>::type& x) : 
00354   Expr< SFadExprTag< ValueT,Num > >(ValueT(x)) {}
00355 
00357 
00360       SFad(const int sz, const ValueT & x) : 
00361   Expr< SFadExprTag< ValueT,Num > >(sz,x) {}
00362 
00364 
00369       SFad(const int sz, const int i, const ValueT & x) : 
00370   Expr< SFadExprTag< ValueT,Num > >(sz,i,x) {}
00371 
00373       SFad(const SFad& x) : 
00374   Expr< SFadExprTag< ValueT,Num > >(x) {}
00375 
00377       template <typename S> SFad(const Expr<S>& x) : 
00378   Expr< SFadExprTag< ValueT,Num > >(x) {}
00379 
00381 
00383       ~SFad() {}
00384 
00386       SFad& operator=(const ValueT& v) {
00387   Expr< SFadExprTag< ValueT,Num > >::operator=(v);
00388   return *this;
00389       }
00390 
00392 
00395       SFad& operator=(const typename dummy<ValueT,ScalarT>::type& v) {
00396   Expr< SFadExprTag< ValueT,Num > >::operator=(ValueT(v));
00397   return *this;
00398       }
00399 
00401       SFad& operator=(const SFad& x) {
00402   Expr< SFadExprTag< ValueT,Num > >::operator=(static_cast<const Expr< SFadExprTag< ValueT,Num > >&>(x));
00403   return *this;
00404       }
00405 
00407       template <typename S> SFad& operator=(const Expr<S>& x) 
00408       {
00409   Expr< SFadExprTag< ValueT,Num > >::operator=(x);
00410   return *this;
00411       }
00412 
00413     }; // class SFad<ValueT,Num>
00414 
00416     template <typename T, int Num>
00417     struct ExprPromote< Expr< SFadExprTag<T,Num> >, T > {
00418       typedef Expr< SFadExprTag<T,Num> > type;
00419     };
00420     
00422     template <typename T, int Num>
00423     struct ExprPromote< T, Expr< SFadExprTag<T,Num> > > {
00424       typedef Expr< SFadExprTag<T,Num> > type;
00425     };
00426 
00427     template <typename T, int Num>
00428     std::ostream& operator << (std::ostream& os, 
00429                                const Expr< SFadExprTag<T,Num> >& x) {
00430       os << x.val() << " [";
00431       
00432       for (int i=0; i< x.size(); i++) {
00433         os << " " << x.dx(i);
00434       }
00435 
00436       os << " ]";
00437       return os;
00438     }
00439 
00440   } // namespace ELRFad
00441 
00442 } // namespace Sacado
00443 
00444 #include "Sacado_ELRFad_SFadImp.hpp"
00445 #include "Sacado_ELRFad_Ops.hpp"
00446 
00447 #endif // SACADO_ELRFAD_SFAD_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Wed Apr 13 10:19:31 2011 for Sacado Package Browser (Single Doxygen Collection) by  doxygen 1.6.3