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 typename ScalarType<T>::type scalar_type;
00087 
00089       typedef Expr< SFadExprTag<T,Num> > base_expr_type;
00090 
00092       static const int num_args = 1;
00093 
00095       static const bool is_linear = true;
00096 
00101 
00103       Expr() : val_( T(0.)), update_val_(true) { ss_array<T>::zero(dx_, Num); }
00104 
00106 
00109       Expr(const T & x) : val_(x), update_val_(true)  { 
00110   ss_array<T>::zero(dx_, Num); }
00111 
00113 
00116       Expr(const int sz, const T & x);
00117 
00119 
00124       Expr(const int sz, const int i, const T & x);
00125 
00127       Expr(const Expr& x) : val_(x.val_), update_val_(x.update_val_) { 
00128   //ss_array<T>::copy(x.dx_, dx_, Num);
00129   for (int i=0; i<Num; i++)
00130     dx_[i] = x.dx_[i];
00131       }
00132 
00134       template <typename S> Expr(const Expr<S>& x);
00135 
00137       ~Expr() {}
00138 
00140 
00146       void diff(const int ith, const int n);
00147 
00149 
00153       void resize(int sz);
00154 
00156 
00160       void expand(int sz) { resize(sz); }
00161 
00163       void zero() { ss_array<T>::zero(dx_, Num); }
00164 
00166       void setUpdateValue(bool update_val) { update_val_ = update_val; }
00167 
00169       bool updateValue() const { return update_val_; }
00170 
00172       void cache() const {}
00173 
00175       template <typename S>
00176       bool isEqualTo(const Expr<S>& x) const {
00177   typedef IsEqual<value_type> IE;
00178   if (x.size() != this->size()) return false;
00179   bool eq = IE::eval(x.val(), this->val());
00180   for (int i=0; i<this->size(); i++)
00181     eq = eq && IE::eval(x.dx(i), this->dx(i));
00182   return eq;
00183       }
00184 
00186 
00191 
00193       const T& val() const { return val_;}
00194 
00196       T& val() { return val_;}
00197 
00199 
00204 
00206       int size() const { return Num;}
00207 
00212       int availableSize() const { return Num; }
00213 
00215       bool hasFastAccess() const { return true; }
00216 
00218       bool isPassive() const { return false; }
00219       
00221       void setIsConstant(bool is_const) {}
00222 
00224       const T* dx() const { return &(dx_[0]);}
00225 
00227       const T& dx(int i) const { return dx_[i]; }
00228     
00230       T& fastAccessDx(int i) { return dx_[i];}
00231 
00233       const T& fastAccessDx(int i) const { return dx_[i];}
00234 
00236       void computePartials(const T& bar, T partials[]) const { 
00237   partials[0] = bar; 
00238       }
00239 
00241       void getTangents(int i, T dots[]) const { 
00242   dots[0] = this->dx_[i];
00243       }
00244 
00246       template <int Arg>
00247       bool isActive() const { return true; }
00248 
00250       template <int Arg>
00251       T getTangent(int i) const { return this->dx_[i]; }
00252     
00254 
00259 
00261       Expr< SFadExprTag<T,Num> >& operator=(const T& val);
00262 
00264       Expr< SFadExprTag<T,Num> >& 
00265       operator=(const Expr< SFadExprTag<T,Num> >& x);
00266 
00268       template <typename S> 
00269       Expr< SFadExprTag<T,Num> >& operator=(const Expr<S>& x); 
00270 
00272 
00277 
00279       Expr< SFadExprTag<T,Num> >& operator += (const T& x);
00280 
00282       Expr< SFadExprTag<T,Num> >& operator -= (const T& x);
00283 
00285       Expr< SFadExprTag<T,Num> >& operator *= (const T& x);
00286 
00288       Expr< SFadExprTag<T,Num> >& operator /= (const T& x);
00289 
00291       template <typename S> 
00292       Expr< SFadExprTag<T,Num> >& operator += (const Expr<S>& x);
00293 
00295       template <typename S> 
00296       Expr< SFadExprTag<T,Num> >& operator -= (const Expr<S>& x);
00297   
00299       template <typename S> 
00300       Expr< SFadExprTag<T,Num> >& operator *= (const Expr<S>& x);
00301 
00303       template <typename S> 
00304       Expr< SFadExprTag<T,Num> >& operator /= (const Expr<S>& x);
00305 
00307 
00308     protected:
00309 
00311       T val_;
00312 
00314       T dx_[Num];
00315 
00317       bool update_val_;
00318 
00319       // Functor for mpl::for_each to compute the local accumulation
00320       // of a tangent derivative
00321       template <typename ExprT>
00322       struct LocalAccumOp {
00323   typedef typename ExprT::value_type value_type;
00324   static const int N = ExprT::num_args;
00325   const ExprT& x;
00326   mutable value_type t;
00327   value_type partials[N];
00328   int i;
00329   inline LocalAccumOp(const ExprT& x_) :
00330     x(x_) { x.computePartials(value_type(1.), partials); }
00331   template <typename ArgT>
00332   inline void operator () (ArgT arg) const {
00333     const int Arg = ArgT::value;
00334     if (x.template isActive<Arg>())
00335       t += partials[Arg] * x.template getTangent<Arg>(i);
00336   }
00337       };
00338 
00339     }; // class Expr<SFadExprTag>
00340 
00354     template <typename ValueT, int Num>
00355     class SFad : 
00356       public Expr< SFadExprTag<ValueT,Num > > {
00357 
00358     public:
00359 
00361       typedef typename ScalarType<ValueT>::type ScalarT;
00362 
00364       template <typename T> 
00365       struct apply {
00366   typedef SFad<T,Num> type;
00367       };
00368 
00373 
00375 
00378       SFad() : 
00379   Expr< SFadExprTag< ValueT,Num > >() {}
00380 
00382 
00385       SFad(const ValueT & x) : 
00386   Expr< SFadExprTag< ValueT,Num > >(x) {}
00387 
00389 
00393       SFad(const typename dummy<ValueT,ScalarT>::type& x) : 
00394   Expr< SFadExprTag< ValueT,Num > >(ValueT(x)) {}
00395 
00397 
00400       SFad(const int sz, const ValueT & x) : 
00401   Expr< SFadExprTag< ValueT,Num > >(sz,x) {}
00402 
00404 
00409       SFad(const int sz, const int i, const ValueT & x) : 
00410   Expr< SFadExprTag< ValueT,Num > >(sz,i,x) {}
00411 
00413       SFad(const SFad& x) : 
00414   Expr< SFadExprTag< ValueT,Num > >(x) {}
00415 
00417       template <typename S> SFad(const Expr<S>& x) : 
00418   Expr< SFadExprTag< ValueT,Num > >(x) {}
00419 
00421 
00423       ~SFad() {}
00424 
00426       SFad& operator=(const ValueT& v) {
00427   Expr< SFadExprTag< ValueT,Num > >::operator=(v);
00428   return *this;
00429       }
00430 
00432 
00435       SFad& operator=(const typename dummy<ValueT,ScalarT>::type& v) {
00436   Expr< SFadExprTag< ValueT,Num > >::operator=(ValueT(v));
00437   return *this;
00438       }
00439 
00441       SFad& operator=(const SFad& x) {
00442   Expr< SFadExprTag< ValueT,Num > >::operator=(static_cast<const Expr< SFadExprTag< ValueT,Num > >&>(x));
00443   return *this;
00444       }
00445 
00447       template <typename S> SFad& operator=(const Expr<S>& x) 
00448       {
00449   Expr< SFadExprTag< ValueT,Num > >::operator=(x);
00450   return *this;
00451       }
00452 
00453     }; // class SFad<ValueT,Num>
00454 
00456     template <typename T, int Num>
00457     struct ExprPromote< Expr< SFadExprTag<T,Num> >, T > {
00458       typedef Expr< SFadExprTag<T,Num> > type;
00459     };
00460     
00462     template <typename T, int Num>
00463     struct ExprPromote< T, Expr< SFadExprTag<T,Num> > > {
00464       typedef Expr< SFadExprTag<T,Num> > type;
00465     };
00466 
00467     template <typename T, int Num>
00468     std::ostream& operator << (std::ostream& os, 
00469                                const Expr< SFadExprTag<T,Num> >& x) {
00470       os << x.val() << " [";
00471       
00472       for (int i=0; i< x.size(); i++) {
00473         os << " " << x.dx(i);
00474       }
00475 
00476       os << " ]";
00477       return os;
00478     }
00479 
00480   } // namespace ELRCacheFad
00481 
00482 } // namespace Sacado
00483 
00484 #include "Sacado_ELRCacheFad_SFadImp.hpp"
00485 #include "Sacado_ELRCacheFad_Ops.hpp"
00486 
00487 #endif // SACADO_ELRCACHEFAD_SFAD_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines