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       void zero() { ss_array<T>::zero(dx_, Num); }
00145 
00147       void setUpdateValue(bool update_val) { update_val_ = update_val; }
00148 
00150       bool updateValue() const { return update_val_; }
00151 
00153       void cache() const {}
00154 
00156 
00161 
00163       const T& val() const { return val_;}
00164 
00166       T& val() { return val_;}
00167 
00169 
00174 
00176       int size() const { return Num;}
00177 
00182       int availableSize() const { return Num; }
00183 
00185       bool hasFastAccess() const { return true; }
00186 
00188       bool isPassive() const { return false; }
00189       
00191       void setIsConstant(bool is_const) {}
00192 
00194       const T* dx() const { return &(dx_[0]);}
00195 
00197       const T& dx(int i) const { return dx_[i]; }
00198     
00200       T& fastAccessDx(int i) { return dx_[i];}
00201 
00203       const T& fastAccessDx(int i) const { return dx_[i];}
00204     
00206 
00211 
00213       Expr< SFadExprTag<T,Num> >& operator=(const T& val);
00214 
00216       Expr< SFadExprTag<T,Num> >& 
00217       operator=(const Expr< SFadExprTag<T,Num> >& x);
00218 
00220       template <typename S> 
00221       Expr< SFadExprTag<T,Num> >& operator=(const Expr<S>& x); 
00222 
00224 
00229 
00231       Expr< SFadExprTag<T,Num> >& operator += (const T& x);
00232 
00234       Expr< SFadExprTag<T,Num> >& operator -= (const T& x);
00235 
00237       Expr< SFadExprTag<T,Num> >& operator *= (const T& x);
00238 
00240       Expr< SFadExprTag<T,Num> >& operator /= (const T& x);
00241 
00243       template <typename S> 
00244       Expr< SFadExprTag<T,Num> >& operator += (const Expr<S>& x);
00245 
00247       template <typename S> 
00248       Expr< SFadExprTag<T,Num> >& operator -= (const Expr<S>& x);
00249   
00251       template <typename S> 
00252       Expr< SFadExprTag<T,Num> >& operator *= (const Expr<S>& x);
00253 
00255       template <typename S> 
00256       Expr< SFadExprTag<T,Num> >& operator /= (const Expr<S>& x);
00257 
00259 
00260     protected:
00261 
00263       T val_;
00264 
00266       T dx_[Num];
00267 
00269       bool update_val_;
00270 
00271     }; // class Expr<SFadExprTag>
00272 
00286     template <typename ValueT, int Num>
00287     class SFad : 
00288       public Expr< SFadExprTag<ValueT,Num > > {
00289 
00290     public:
00291 
00293       typedef typename ScalarType<ValueT>::type ScalarT;
00294 
00296       template <typename T> 
00297       struct apply {
00298   typedef SFad<T,Num> type;
00299       };
00300 
00305 
00307 
00310       SFad() : 
00311   Expr< SFadExprTag< ValueT,Num > >() {}
00312 
00314 
00317       SFad(const ValueT & x) : 
00318   Expr< SFadExprTag< ValueT,Num > >(x) {}
00319 
00321 
00325       SFad(const typename dummy<ValueT,ScalarT>::type& x) : 
00326   Expr< SFadExprTag< ValueT,Num > >(ValueT(x)) {}
00327 
00329 
00332       SFad(const int sz, const ValueT & x) : 
00333   Expr< SFadExprTag< ValueT,Num > >(sz,x) {}
00334 
00336 
00341       SFad(const int sz, const int i, const ValueT & x) : 
00342   Expr< SFadExprTag< ValueT,Num > >(sz,i,x) {}
00343 
00345       SFad(const SFad& x) : 
00346   Expr< SFadExprTag< ValueT,Num > >(x) {}
00347 
00349       template <typename S> SFad(const Expr<S>& x) : 
00350   Expr< SFadExprTag< ValueT,Num > >(x) {}
00351 
00353 
00355       ~SFad() {}
00356 
00358       SFad& operator=(const ValueT& v) {
00359   Expr< SFadExprTag< ValueT,Num > >::operator=(v);
00360   return *this;
00361       }
00362 
00364 
00367       SFad& operator=(const typename dummy<ValueT,ScalarT>::type& v) {
00368   Expr< SFadExprTag< ValueT,Num > >::operator=(ValueT(v));
00369   return *this;
00370       }
00371 
00373       SFad& operator=(const SFad& x) {
00374   Expr< SFadExprTag< ValueT,Num > >::operator=(static_cast<const Expr< SFadExprTag< ValueT,Num > >&>(x));
00375   return *this;
00376       }
00377 
00379       template <typename S> SFad& operator=(const Expr<S>& x) 
00380       {
00381   Expr< SFadExprTag< ValueT,Num > >::operator=(x);
00382   return *this;
00383       }
00384 
00385     }; // class SFad<ValueT,Num>
00386 
00387   } // namespace CacheFad
00388 
00389 } // namespace Sacado
00390 
00391 #include "Sacado_CacheFad_SFadImp.hpp"
00392 #include "Sacado_CacheFad_Ops.hpp"
00393 
00394 #endif // SACADO_CACHEFAD_SFAD_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines