Sacado Package Browser (Single Doxygen Collection) Version of the Day
Sacado_CacheFad_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_CACHEFAD_SFAD_HPP
00055 #define SACADO_CACHEFAD_SFAD_HPP
00056 
00057 #include "Sacado_CacheFad_SFadTraits.hpp"
00058 #include "Sacado_CacheFad_Expression.hpp"
00059 #include "Sacado_StaticArrayTraits.hpp"
00060 #include "Sacado_dummy_arg.hpp"
00061 
00062 namespace Sacado {
00063 
00065   namespace CacheFad {
00066 
00068     template <typename T, int Num> 
00069     struct SFadExprTag {};
00070 
00078     template <typename T, int Num> 
00079     class Expr< SFadExprTag<T,Num> > {
00080 
00081     public:
00082 
00084       typedef T value_type;
00085 
00087       typedef typename ScalarType<T>::type scalar_type;
00088 
00093 
00095       Expr() : val_( T(0.)), update_val_(true) { ss_array<T>::zero(dx_, Num); }
00096 
00098 
00101       Expr(const T & x) : val_(x), update_val_(true)  { 
00102   ss_array<T>::zero(dx_, Num); }
00103 
00105 
00108       Expr(const int sz, const T & x);
00109 
00111 
00116       Expr(const int sz, const int i, const T & x);
00117 
00119       Expr(const Expr& x);
00120 
00122       template <typename S> Expr(const Expr<S>& x);
00123 
00125       ~Expr() {}
00126 
00128 
00134       void diff(const int ith, const int n);
00135 
00137 
00141       void resize(int sz);
00142 
00144 
00148       void expand(int sz) { resize(sz); }
00149 
00151       void zero() { ss_array<T>::zero(dx_, Num); }
00152 
00154       void setUpdateValue(bool update_val) { update_val_ = update_val; }
00155 
00157       bool updateValue() const { return update_val_; }
00158 
00160       void cache() const {}
00161 
00163       template <typename S>
00164       bool isEqualTo(const Expr<S>& x) const {
00165   typedef IsEqual<value_type> IE;
00166   if (x.size() != this->size()) return false;
00167   bool eq = IE::eval(x.val(), this->val());
00168   for (int i=0; i<this->size(); i++)
00169     eq = eq && IE::eval(x.dx(i), this->dx(i));
00170   return eq;
00171       }
00172 
00174 
00179 
00181       const T& val() const { return val_;}
00182 
00184       T& val() { return val_;}
00185 
00187 
00192 
00194       int size() const { return Num;}
00195 
00200       int availableSize() const { return Num; }
00201 
00203       bool hasFastAccess() const { return true; }
00204 
00206       bool isPassive() const { return false; }
00207       
00209       void setIsConstant(bool is_const) {}
00210 
00212       const T* dx() const { return &(dx_[0]);}
00213 
00215       const T& dx(int i) const { return dx_[i]; }
00216     
00218       T& fastAccessDx(int i) { return dx_[i];}
00219 
00221       const T& fastAccessDx(int i) const { return dx_[i];}
00222     
00224 
00229 
00231       Expr< SFadExprTag<T,Num> >& operator=(const T& val);
00232 
00234       Expr< SFadExprTag<T,Num> >& 
00235       operator=(const Expr< SFadExprTag<T,Num> >& x);
00236 
00238       template <typename S> 
00239       Expr< SFadExprTag<T,Num> >& operator=(const Expr<S>& x); 
00240 
00242 
00247 
00249       Expr< SFadExprTag<T,Num> >& operator += (const T& x);
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       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       template <typename S> 
00274       Expr< SFadExprTag<T,Num> >& operator /= (const Expr<S>& x);
00275 
00277 
00278     protected:
00279 
00281       T val_;
00282 
00284       T dx_[Num];
00285 
00287       bool update_val_;
00288 
00289     }; // class Expr<SFadExprTag>
00290 
00304     template <typename ValueT, int Num>
00305     class SFad : 
00306       public Expr< SFadExprTag<ValueT,Num > > {
00307 
00308     public:
00309 
00311       typedef typename ScalarType<ValueT>::type ScalarT;
00312 
00314       template <typename T> 
00315       struct apply {
00316   typedef SFad<T,Num> type;
00317       };
00318 
00323 
00325 
00328       SFad() : 
00329   Expr< SFadExprTag< ValueT,Num > >() {}
00330 
00332 
00335       SFad(const ValueT & x) : 
00336   Expr< SFadExprTag< ValueT,Num > >(x) {}
00337 
00339 
00343       SFad(const typename dummy<ValueT,ScalarT>::type& x) : 
00344   Expr< SFadExprTag< ValueT,Num > >(ValueT(x)) {}
00345 
00347 
00350       SFad(const int sz, const ValueT & x) : 
00351   Expr< SFadExprTag< ValueT,Num > >(sz,x) {}
00352 
00354 
00359       SFad(const int sz, const int i, const ValueT & x) : 
00360   Expr< SFadExprTag< ValueT,Num > >(sz,i,x) {}
00361 
00363       SFad(const SFad& x) : 
00364   Expr< SFadExprTag< ValueT,Num > >(x) {}
00365 
00367       template <typename S> SFad(const Expr<S>& x) : 
00368   Expr< SFadExprTag< ValueT,Num > >(x) {}
00369 
00371 
00373       ~SFad() {}
00374 
00376       SFad& operator=(const ValueT& v) {
00377   Expr< SFadExprTag< ValueT,Num > >::operator=(v);
00378   return *this;
00379       }
00380 
00382 
00385       SFad& operator=(const typename dummy<ValueT,ScalarT>::type& v) {
00386   Expr< SFadExprTag< ValueT,Num > >::operator=(ValueT(v));
00387   return *this;
00388       }
00389 
00391       SFad& operator=(const SFad& x) {
00392   Expr< SFadExprTag< ValueT,Num > >::operator=(static_cast<const Expr< SFadExprTag< ValueT,Num > >&>(x));
00393   return *this;
00394       }
00395 
00397       template <typename S> SFad& operator=(const Expr<S>& x) 
00398       {
00399   Expr< SFadExprTag< ValueT,Num > >::operator=(x);
00400   return *this;
00401       }
00402 
00403     }; // class SFad<ValueT,Num>
00404 
00405   } // namespace CacheFad
00406 
00407 } // namespace Sacado
00408 
00409 #include "Sacado_CacheFad_SFadImp.hpp"
00410 #include "Sacado_CacheFad_Ops.hpp"
00411 
00412 #endif // SACADO_CACHEFAD_SFAD_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines