Sacado Package Browser (Single Doxygen Collection) Version of the Day
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.)), 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 
00164 
00166       const T& val() const { return val_;}
00167 
00169       T& val() { return val_;}
00170 
00172 
00177 
00179       int size() const { return Num;}
00180 
00185       int availableSize() const { return Num; }
00186 
00188       bool hasFastAccess() const { return true; }
00189 
00191       bool isPassive() const { return false; }
00192       
00194       void setIsConstant(bool is_const) {}
00195 
00197       const T* dx() const { return &(dx_[0]);}
00198 
00200       const T& dx(int i) const { return dx_[i]; }
00201     
00203       T& fastAccessDx(int i) { return dx_[i];}
00204 
00206       const T& fastAccessDx(int i) const { return dx_[i];}
00207 
00209       void computePartials(const T& bar, T partials[]) const { 
00210   partials[0] = bar; 
00211       }
00212 
00214       void getTangents(int i, T dots[]) const { 
00215   dots[0] = this->dx_[i];
00216       }
00217 
00219       template <int Arg>
00220       bool isActive() const { return true; }
00221 
00223       template <int Arg>
00224       T getTangent(int i) const { return this->dx_[i]; }
00225     
00227 
00232 
00234       Expr< SFadExprTag<T,Num> >& operator=(const T& val);
00235 
00237       Expr< SFadExprTag<T,Num> >& 
00238       operator=(const Expr< SFadExprTag<T,Num> >& x);
00239 
00241       template <typename S> 
00242       Expr< SFadExprTag<T,Num> >& operator=(const Expr<S>& x); 
00243 
00245 
00250 
00252       Expr< SFadExprTag<T,Num> >& operator += (const T& x);
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       template <typename S> 
00265       Expr< SFadExprTag<T,Num> >& operator += (const Expr<S>& x);
00266 
00268       template <typename S> 
00269       Expr< SFadExprTag<T,Num> >& operator -= (const Expr<S>& x);
00270   
00272       template <typename S> 
00273       Expr< SFadExprTag<T,Num> >& operator *= (const Expr<S>& x);
00274 
00276       template <typename S> 
00277       Expr< SFadExprTag<T,Num> >& operator /= (const Expr<S>& x);
00278 
00280 
00281     protected:
00282 
00284       T val_;
00285 
00287       T dx_[Num];
00288 
00290       bool update_val_;
00291 
00292       // Functor for mpl::for_each to compute the local accumulation
00293       // of a tangent derivative
00294       template <typename ExprT>
00295       struct LocalAccumOp {
00296   typedef typename ExprT::value_type value_type;
00297   static const int N = ExprT::num_args;
00298   const ExprT& x;
00299   mutable value_type t;
00300   value_type partials[N];
00301   int i;
00302   inline LocalAccumOp(const ExprT& x_) :
00303     x(x_) { x.computePartials(value_type(1.), partials); }
00304   template <typename ArgT>
00305   inline void operator () (ArgT arg) const {
00306     const int Arg = ArgT::value;
00307     if (x.template isActive<Arg>())
00308       t += partials[Arg] * x.template getTangent<Arg>(i);
00309   }
00310       };
00311 
00312     }; // class Expr<SFadExprTag>
00313 
00324     template <typename ValueT, int Num>
00325     class SFad : 
00326       public Expr< SFadExprTag<ValueT,Num > > {
00327 
00328     public:
00329 
00331       typedef typename ScalarType<ValueT>::type ScalarT;
00332 
00334       template <typename T> 
00335       struct apply {
00336   typedef SFad<T,Num> type;
00337       };
00338 
00343 
00345 
00348       SFad() : 
00349   Expr< SFadExprTag< ValueT,Num > >() {}
00350 
00352 
00355       SFad(const ValueT & x) : 
00356   Expr< SFadExprTag< ValueT,Num > >(x) {}
00357 
00359 
00363       SFad(const typename dummy<ValueT,ScalarT>::type& x) : 
00364   Expr< SFadExprTag< ValueT,Num > >(ValueT(x)) {}
00365 
00367 
00370       SFad(const int sz, const ValueT & x) : 
00371   Expr< SFadExprTag< ValueT,Num > >(sz,x) {}
00372 
00374 
00379       SFad(const int sz, const int i, const ValueT & x) : 
00380   Expr< SFadExprTag< ValueT,Num > >(sz,i,x) {}
00381 
00383       SFad(const SFad& x) : 
00384   Expr< SFadExprTag< ValueT,Num > >(x) {}
00385 
00387       template <typename S> SFad(const Expr<S>& x) : 
00388   Expr< SFadExprTag< ValueT,Num > >(x) {}
00389 
00391 
00393       ~SFad() {}
00394 
00396       SFad& operator=(const ValueT& v) {
00397   Expr< SFadExprTag< ValueT,Num > >::operator=(v);
00398   return *this;
00399       }
00400 
00402 
00405       SFad& operator=(const typename dummy<ValueT,ScalarT>::type& v) {
00406   Expr< SFadExprTag< ValueT,Num > >::operator=(ValueT(v));
00407   return *this;
00408       }
00409 
00411       SFad& operator=(const SFad& x) {
00412   Expr< SFadExprTag< ValueT,Num > >::operator=(static_cast<const Expr< SFadExprTag< ValueT,Num > >&>(x));
00413   return *this;
00414       }
00415 
00417       template <typename S> SFad& operator=(const Expr<S>& x) 
00418       {
00419   Expr< SFadExprTag< ValueT,Num > >::operator=(x);
00420   return *this;
00421       }
00422 
00423     }; // class SFad<ValueT,Num>
00424 
00426     template <typename T, int Num>
00427     struct ExprPromote< Expr< SFadExprTag<T,Num> >, T > {
00428       typedef Expr< SFadExprTag<T,Num> > type;
00429     };
00430     
00432     template <typename T, int Num>
00433     struct ExprPromote< T, Expr< SFadExprTag<T,Num> > > {
00434       typedef Expr< SFadExprTag<T,Num> > type;
00435     };
00436 
00437     template <typename T, int Num>
00438     std::ostream& operator << (std::ostream& os, 
00439                                const Expr< SFadExprTag<T,Num> >& x) {
00440       os << x.val() << " [";
00441       
00442       for (int i=0; i< x.size(); i++) {
00443         os << " " << x.dx(i);
00444       }
00445 
00446       os << " ]";
00447       return os;
00448     }
00449 
00450   } // namespace ELRFad
00451 
00452 } // namespace Sacado
00453 
00454 #include "Sacado_ELRFad_SFadImp.hpp"
00455 #include "Sacado_ELRFad_Ops.hpp"
00456 
00457 #endif // SACADO_ELRFAD_SFAD_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines