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 
00092       static const bool is_linear = true;
00093 
00098 
00100       Expr() : val_( T(0.)), update_val_(true) { ss_array<T>::zero(dx_, Num); }
00101 
00103 
00106       Expr(const T & x) : val_(x), update_val_(true)  { 
00107   ss_array<T>::zero(dx_, Num); }
00108 
00110 
00113       Expr(const int sz, const T & x);
00114 
00116 
00121       Expr(const int sz, const int i, const T & x);
00122 
00124       Expr(const Expr& x) : val_(x.val_), update_val_(x.update_val_) { 
00125   //ss_array<T>::copy(x.dx_, dx_, Num);
00126   for (int i=0; i<Num; i++)
00127     dx_[i] = x.dx_[i];
00128       }
00129 
00131       template <typename S> Expr(const Expr<S>& x);
00132 
00134       ~Expr() {}
00135 
00137 
00143       void diff(const int ith, const int n);
00144 
00146 
00150       void resize(int sz);
00151 
00153 
00157       void expand(int sz) { resize(sz); }
00158 
00160       void zero() { ss_array<T>::zero(dx_, Num); }
00161 
00163       void setUpdateValue(bool update_val) { update_val_ = update_val; }
00164 
00166       bool updateValue() const { return update_val_; }
00167 
00169       template <typename S>
00170       bool isEqualTo(const Expr<S>& x) const {
00171   typedef IsEqual<value_type> IE;
00172   if (x.size() != this->size()) return false;
00173   bool eq = IE::eval(x.val(), this->val());
00174   for (int i=0; i<this->size(); i++)
00175     eq = eq && IE::eval(x.dx(i), this->dx(i));
00176   return eq;
00177       }
00178 
00180 
00185 
00187       const T& val() const { return val_;}
00188 
00190       T& val() { return val_;}
00191 
00193 
00198 
00200       int size() const { return Num;}
00201 
00206       int availableSize() const { return Num; }
00207 
00209       bool hasFastAccess() const { return true; }
00210 
00212       bool isPassive() const { return false; }
00213       
00215       void setIsConstant(bool is_const) {}
00216 
00218       const T* dx() const { return &(dx_[0]);}
00219 
00221       const T& dx(int i) const { return dx_[i]; }
00222     
00224       T& fastAccessDx(int i) { return dx_[i];}
00225 
00227       const T& fastAccessDx(int i) const { return dx_[i];}
00228 
00230       void computePartials(const T& bar, T partials[]) const { 
00231   partials[0] = bar; 
00232       }
00233 
00235       void getTangents(int i, T dots[]) const { 
00236   dots[0] = this->dx_[i];
00237       }
00238 
00240       template <int Arg>
00241       bool isActive() const { return true; }
00242 
00244       template <int Arg>
00245       const T& getTangent(int i) const { return this->dx_[i]; }
00246     
00248 
00253 
00255       Expr< SFadExprTag<T,Num> >& operator=(const T& val);
00256 
00258       Expr< SFadExprTag<T,Num> >& 
00259       operator=(const Expr< SFadExprTag<T,Num> >& x);
00260 
00262       template <typename S> 
00263       Expr< SFadExprTag<T,Num> >& operator=(const Expr<S>& x); 
00264 
00266 
00271 
00273       Expr< SFadExprTag<T,Num> >& operator += (const T& x);
00274 
00276       Expr< SFadExprTag<T,Num> >& operator -= (const T& x);
00277 
00279       Expr< SFadExprTag<T,Num> >& operator *= (const T& x);
00280 
00282       Expr< SFadExprTag<T,Num> >& operator /= (const T& x);
00283 
00285       template <typename S> 
00286       Expr< SFadExprTag<T,Num> >& operator += (const Expr<S>& x);
00287 
00289       template <typename S> 
00290       Expr< SFadExprTag<T,Num> >& operator -= (const Expr<S>& x);
00291   
00293       template <typename S> 
00294       Expr< SFadExprTag<T,Num> >& operator *= (const Expr<S>& x);
00295 
00297       template <typename S> 
00298       Expr< SFadExprTag<T,Num> >& operator /= (const Expr<S>& x);
00299 
00301 
00302     protected:
00303 
00305       T val_;
00306 
00308       T dx_[Num];
00309 
00311       bool update_val_;
00312 
00313       // Functor for mpl::for_each to compute the local accumulation
00314       // of a tangent derivative
00315       template <typename ExprT>
00316       struct LocalAccumOp {
00317   typedef typename ExprT::value_type value_type;
00318   static const int N = ExprT::num_args;
00319   const ExprT& x;
00320   mutable value_type t;
00321   value_type partials[N];
00322   int i;
00323   inline LocalAccumOp(const ExprT& x_) :
00324     x(x_) { x.computePartials(value_type(1.), partials); }
00325   inline void getTangents(int i_) { i = i_; }
00326   template <typename ArgT>
00327   inline void operator () (ArgT arg) const {
00328     const int Arg = ArgT::value;
00329     if (x.template isActive<Arg>())
00330       t += partials[Arg] * x.template getTangent<Arg>(i);
00331   }
00332       };
00333 
00334     }; // class Expr<SFadExprTag>
00335 
00346     template <typename ValueT, int Num>
00347     class SFad : 
00348       public Expr< SFadExprTag<ValueT,Num > > {
00349 
00350     public:
00351 
00353       typedef typename ScalarType<ValueT>::type ScalarT;
00354 
00356       template <typename T> 
00357       struct apply {
00358   typedef SFad<T,Num> type;
00359       };
00360 
00365 
00367 
00370       SFad() : 
00371   Expr< SFadExprTag< ValueT,Num > >() {}
00372 
00374 
00377       SFad(const ValueT & x) : 
00378   Expr< SFadExprTag< ValueT,Num > >(x) {}
00379 
00381 
00385       SFad(const typename dummy<ValueT,ScalarT>::type& x) : 
00386   Expr< SFadExprTag< ValueT,Num > >(ValueT(x)) {}
00387 
00389 
00392       SFad(const int sz, const ValueT & x) : 
00393   Expr< SFadExprTag< ValueT,Num > >(sz,x) {}
00394 
00396 
00401       SFad(const int sz, const int i, const ValueT & x) : 
00402   Expr< SFadExprTag< ValueT,Num > >(sz,i,x) {}
00403 
00405       SFad(const SFad& x) : 
00406   Expr< SFadExprTag< ValueT,Num > >(x) {}
00407 
00409       template <typename S> SFad(const Expr<S>& x) : 
00410   Expr< SFadExprTag< ValueT,Num > >(x) {}
00411 
00413 
00415       ~SFad() {}
00416 
00418       SFad& operator=(const ValueT& v) {
00419   Expr< SFadExprTag< ValueT,Num > >::operator=(v);
00420   return *this;
00421       }
00422 
00424 
00427       SFad& operator=(const typename dummy<ValueT,ScalarT>::type& v) {
00428   Expr< SFadExprTag< ValueT,Num > >::operator=(ValueT(v));
00429   return *this;
00430       }
00431 
00433       SFad& operator=(const SFad& x) {
00434   Expr< SFadExprTag< ValueT,Num > >::operator=(static_cast<const Expr< SFadExprTag< ValueT,Num > >&>(x));
00435   return *this;
00436       }
00437 
00439       template <typename S> SFad& operator=(const Expr<S>& x) 
00440       {
00441   Expr< SFadExprTag< ValueT,Num > >::operator=(x);
00442   return *this;
00443       }
00444 
00445     }; // class SFad<ValueT,Num>
00446 
00448     template <typename T, int Num>
00449     struct ExprPromote< Expr< SFadExprTag<T,Num> >, T > {
00450       typedef Expr< SFadExprTag<T,Num> > type;
00451     };
00452     
00454     template <typename T, int Num>
00455     struct ExprPromote< T, Expr< SFadExprTag<T,Num> > > {
00456       typedef Expr< SFadExprTag<T,Num> > type;
00457     };
00458 
00459     template <typename T, int Num>
00460     std::ostream& operator << (std::ostream& os, 
00461                                const Expr< SFadExprTag<T,Num> >& x) {
00462       os << x.val() << " [";
00463       
00464       for (int i=0; i< x.size(); i++) {
00465         os << " " << x.dx(i);
00466       }
00467 
00468       os << " ]";
00469       return os;
00470     }
00471 
00472   } // namespace ELRFad
00473 
00474 } // namespace Sacado
00475 
00476 #include "Sacado_ELRFad_SFadImp.hpp"
00477 #include "Sacado_ELRFad_Ops.hpp"
00478 
00479 #endif // SACADO_ELRFAD_SFAD_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines