Sacado Package Browser (Single Doxygen Collection) Version of the Day
Sacado_ELRCacheFad_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_ELRCACHEFAD_SFAD_HPP
00055 #define SACADO_ELRCACHEFAD_SFAD_HPP
00056 
00057 #include "Sacado_ELRCacheFad_SFadTraits.hpp"
00058 #include "Sacado_ELRCacheFad_Expression.hpp"
00059 #include "Sacado_StaticArrayTraits.hpp"
00060 #include "Sacado_dummy_arg.hpp"
00061 
00062 namespace Sacado {
00063 
00064   namespace ELRCacheFad {
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.)), update_val_(true) { ss_array<T>::zero(dx_, Num); }
00098 
00100 
00103       Expr(const T & x) : val_(x), update_val_(true)  { 
00104   ss_array<T>::zero(dx_, Num); }
00105 
00107 
00110       Expr(const int sz, const T & x);
00111 
00113 
00118       Expr(const int sz, const int i, const T & x);
00119 
00121       Expr(const Expr& x) : val_(x.val_), update_val_(x.update_val_) { 
00122   //ss_array<T>::copy(x.dx_, dx_, Num);
00123   for (int i=0; i<Num; i++)
00124     dx_[i] = x.dx_[i];
00125       }
00126 
00128       template <typename S> Expr(const Expr<S>& x);
00129 
00131       ~Expr() {}
00132 
00134 
00140       void diff(const int ith, const int n);
00141 
00143 
00147       void resize(int sz);
00148 
00150       void zero() { ss_array<T>::zero(dx_, Num); }
00151 
00153       void setUpdateValue(bool update_val) { update_val_ = update_val; }
00154 
00156       bool updateValue() const { return update_val_; }
00157 
00159       void cache() const {}
00160 
00162 
00167 
00169       const T& val() const { return val_;}
00170 
00172       T& val() { return val_;}
00173 
00175 
00180 
00182       int size() const { return Num;}
00183 
00188       int availableSize() const { return Num; }
00189 
00191       bool hasFastAccess() const { return true; }
00192 
00194       bool isPassive() const { return false; }
00195       
00197       void setIsConstant(bool is_const) {}
00198 
00200       const T* dx() const { return &(dx_[0]);}
00201 
00203       const T& dx(int i) const { return dx_[i]; }
00204     
00206       T& fastAccessDx(int i) { return dx_[i];}
00207 
00209       const T& fastAccessDx(int i) const { return dx_[i];}
00210 
00212       void computePartials(const T& bar, T partials[]) const { 
00213   partials[0] = bar; 
00214       }
00215 
00217       void getTangents(int i, T dots[]) const { 
00218   dots[0] = this->dx_[i];
00219       }
00220 
00222       template <int Arg>
00223       bool isActive() const { return true; }
00224 
00226       template <int Arg>
00227       T getTangent(int i) const { return this->dx_[i]; }
00228     
00230 
00235 
00237       Expr< SFadExprTag<T,Num> >& operator=(const T& val);
00238 
00240       Expr< SFadExprTag<T,Num> >& 
00241       operator=(const Expr< SFadExprTag<T,Num> >& x);
00242 
00244       template <typename S> 
00245       Expr< SFadExprTag<T,Num> >& operator=(const Expr<S>& x); 
00246 
00248 
00253 
00255       Expr< SFadExprTag<T,Num> >& operator += (const T& x);
00256 
00258       Expr< SFadExprTag<T,Num> >& operator -= (const T& x);
00259 
00261       Expr< SFadExprTag<T,Num> >& operator *= (const T& x);
00262 
00264       Expr< SFadExprTag<T,Num> >& operator /= (const T& x);
00265 
00267       template <typename S> 
00268       Expr< SFadExprTag<T,Num> >& operator += (const Expr<S>& x);
00269 
00271       template <typename S> 
00272       Expr< SFadExprTag<T,Num> >& operator -= (const Expr<S>& x);
00273   
00275       template <typename S> 
00276       Expr< SFadExprTag<T,Num> >& operator *= (const Expr<S>& x);
00277 
00279       template <typename S> 
00280       Expr< SFadExprTag<T,Num> >& operator /= (const Expr<S>& x);
00281 
00283 
00284     protected:
00285 
00287       T val_;
00288 
00290       T dx_[Num];
00291 
00293       bool update_val_;
00294 
00295       // Functor for mpl::for_each to compute the local accumulation
00296       // of a tangent derivative
00297       template <typename ExprT>
00298       struct LocalAccumOp {
00299   typedef typename ExprT::value_type value_type;
00300   static const int N = ExprT::num_args;
00301   const ExprT& x;
00302   mutable value_type t;
00303   value_type partials[N];
00304   int i;
00305   inline LocalAccumOp(const ExprT& x_) :
00306     x(x_) { x.computePartials(value_type(1.), partials); }
00307   template <typename ArgT>
00308   inline void operator () (ArgT arg) const {
00309     const int Arg = ArgT::value;
00310     if (x.template isActive<Arg>())
00311       t += partials[Arg] * x.template getTangent<Arg>(i);
00312   }
00313       };
00314 
00315     }; // class Expr<SFadExprTag>
00316 
00330     template <typename ValueT, int Num>
00331     class SFad : 
00332       public Expr< SFadExprTag<ValueT,Num > > {
00333 
00334     public:
00335 
00337       typedef typename ScalarType<ValueT>::type ScalarT;
00338 
00340       template <typename T> 
00341       struct apply {
00342   typedef SFad<T,Num> type;
00343       };
00344 
00349 
00351 
00354       SFad() : 
00355   Expr< SFadExprTag< ValueT,Num > >() {}
00356 
00358 
00361       SFad(const ValueT & x) : 
00362   Expr< SFadExprTag< ValueT,Num > >(x) {}
00363 
00365 
00369       SFad(const typename dummy<ValueT,ScalarT>::type& x) : 
00370   Expr< SFadExprTag< ValueT,Num > >(ValueT(x)) {}
00371 
00373 
00376       SFad(const int sz, const ValueT & x) : 
00377   Expr< SFadExprTag< ValueT,Num > >(sz,x) {}
00378 
00380 
00385       SFad(const int sz, const int i, const ValueT & x) : 
00386   Expr< SFadExprTag< ValueT,Num > >(sz,i,x) {}
00387 
00389       SFad(const SFad& x) : 
00390   Expr< SFadExprTag< ValueT,Num > >(x) {}
00391 
00393       template <typename S> SFad(const Expr<S>& x) : 
00394   Expr< SFadExprTag< ValueT,Num > >(x) {}
00395 
00397 
00399       ~SFad() {}
00400 
00402       SFad& operator=(const ValueT& v) {
00403   Expr< SFadExprTag< ValueT,Num > >::operator=(v);
00404   return *this;
00405       }
00406 
00408 
00411       SFad& operator=(const typename dummy<ValueT,ScalarT>::type& v) {
00412   Expr< SFadExprTag< ValueT,Num > >::operator=(ValueT(v));
00413   return *this;
00414       }
00415 
00417       SFad& operator=(const SFad& x) {
00418   Expr< SFadExprTag< ValueT,Num > >::operator=(static_cast<const Expr< SFadExprTag< ValueT,Num > >&>(x));
00419   return *this;
00420       }
00421 
00423       template <typename S> SFad& operator=(const Expr<S>& x) 
00424       {
00425   Expr< SFadExprTag< ValueT,Num > >::operator=(x);
00426   return *this;
00427       }
00428 
00429     }; // class SFad<ValueT,Num>
00430 
00432     template <typename T, int Num>
00433     struct ExprPromote< Expr< SFadExprTag<T,Num> >, T > {
00434       typedef Expr< SFadExprTag<T,Num> > type;
00435     };
00436     
00438     template <typename T, int Num>
00439     struct ExprPromote< T, Expr< SFadExprTag<T,Num> > > {
00440       typedef Expr< SFadExprTag<T,Num> > type;
00441     };
00442 
00443     template <typename T, int Num>
00444     std::ostream& operator << (std::ostream& os, 
00445                                const Expr< SFadExprTag<T,Num> >& x) {
00446       os << x.val() << " [";
00447       
00448       for (int i=0; i< x.size(); i++) {
00449         os << " " << x.dx(i);
00450       }
00451 
00452       os << " ]";
00453       return os;
00454     }
00455 
00456   } // namespace ELRCacheFad
00457 
00458 } // namespace Sacado
00459 
00460 #include "Sacado_ELRCacheFad_SFadImp.hpp"
00461 #include "Sacado_ELRCacheFad_Ops.hpp"
00462 
00463 #endif // SACADO_ELRCACHEFAD_SFAD_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines