Sacado_Fad_SFad.hpp

Go to the documentation of this file.
00001 // $Id: Sacado_Fad_SFad.hpp,v 1.11.2.3 2007/08/14 00:19:06 etphipp Exp $ 
00002 // $Source: /space/CVS/Trilinos/packages/sacado/src/Sacado_Fad_SFad.hpp,v $ 
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_FAD_SFAD_HPP
00055 #define SACADO_FAD_SFAD_HPP
00056 
00057 #include "Sacado_Fad_SFadTraits.hpp"
00058 #include "Sacado_Fad_Expression.hpp"
00059 #include "Sacado_StaticArrayTraits.hpp"
00060 
00061 namespace Sacado {
00062 
00064   namespace Fad {
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 
00089 
00091       Expr() : val_( T(0.)) { ss_array<T>::zero(dx_, Num); }
00092 
00094 
00097       Expr(const T & x) : val_(x)  { ss_array<T>::zero(dx_, Num); }
00098 
00100 
00103       Expr(const int sz, const T & x);
00104 
00106 
00111       Expr(const int sz, const int i, const T & x);
00112 
00114       Expr(const Expr& x) : val_(x.val_) { 
00115   ss_array<T>::copy(x.dx_, dx_, Num); }
00116 
00118       template <typename S> Expr(const Expr<S>& x);
00119 
00121       ~Expr() {}
00122 
00124 
00130       void diff(const int ith, const int n);
00131 
00133 
00137       void resize(int sz);
00138 
00140       void zero() { ss_array<T>::zero(dx_, Num); }
00141 
00143 
00148 
00150       const T& val() const { return val_;}
00151 
00153       T& val() { return val_;}
00154 
00156 
00161 
00163       int size() const { return Num;}
00164 
00166       bool hasFastAccess() const { return true; }
00167 
00169       bool isPassive() const { return false; }
00170       
00172       void setIsConstant(bool is_const) {}
00173 
00175       const T* dx() const { return &(dx_[0]);}
00176 
00178       T dx(int i) const { return dx_[i]; }
00179     
00181       T& fastAccessDx(int i) { return dx_[i];}
00182 
00184       T fastAccessDx(int i) const { return dx_[i];}
00185     
00187 
00192 
00194       Expr< SFadExprTag<T,Num> >& operator=(const T& val);
00195 
00197       Expr< SFadExprTag<T,Num> >& 
00198       operator=(const Expr< SFadExprTag<T,Num> >& x);
00199 
00201       template <typename S> 
00202       Expr< SFadExprTag<T,Num> >& operator=(const Expr<S>& x); 
00203 
00205 
00210 
00212       Expr< SFadExprTag<T,Num> >& operator += (const T& x);
00213 
00215       Expr< SFadExprTag<T,Num> >& operator -= (const T& x);
00216 
00218       Expr< SFadExprTag<T,Num> >& operator *= (const T& x);
00219 
00221       Expr< SFadExprTag<T,Num> >& operator /= (const T& x);
00222 
00224       template <typename S> 
00225       Expr< SFadExprTag<T,Num> >& operator += (const Expr<S>& x);
00226 
00228       template <typename S> 
00229       Expr< SFadExprTag<T,Num> >& operator -= (const Expr<S>& x);
00230   
00232       template <typename S> 
00233       Expr< SFadExprTag<T,Num> >& operator *= (const Expr<S>& x);
00234 
00236       template <typename S> 
00237       Expr< SFadExprTag<T,Num> >& operator /= (const Expr<S>& x);
00238 
00240 
00241     protected:
00242 
00244       T val_;
00245 
00247       T dx_[Num];
00248 
00249     }; // class Expr<SFadExprTag>
00250 
00252 
00268     template <typename ValueT, int Num,
00269         typename ScalarT = typename ScalarValueType<ValueT>::type >
00270     class SFad : 
00271       public Expr< SFadExprTag<ValueT,Num > > {
00272 
00273     public:
00274 
00279 
00281 
00284       SFad() : 
00285   Expr< SFadExprTag< ValueT,Num > >() {}
00286 
00288 
00291       SFad(const ValueT & x) : 
00292   Expr< SFadExprTag< ValueT,Num > >(x) {}
00293 
00295 
00298       SFad(const ScalarT& x) : 
00299   Expr< SFadExprTag< ValueT,Num > >(ValueT(x)) {}
00300 
00302 
00305       SFad(const int sz, const ValueT & x) : 
00306   Expr< SFadExprTag< ValueT,Num > >(sz,x) {}
00307 
00309 
00314       SFad(const int sz, const int i, const ValueT & x) : 
00315   Expr< SFadExprTag< ValueT,Num > >(sz,i,x) {}
00316 
00318       SFad(const SFad& x) : 
00319   Expr< SFadExprTag< ValueT,Num > >(x) {}
00320 
00322       template <typename S> SFad(const Expr<S>& x) : 
00323   Expr< SFadExprTag< ValueT,Num > >(x) {}
00324 
00326 
00328       ~SFad() {}
00329 
00331       SFad& operator=(const ValueT& val) {
00332   Expr< SFadExprTag< ValueT,Num > >::operator=(val);
00333   return *this;
00334       }
00335 
00337       SFad& operator=(const ScalarT& val) {
00338   Expr< SFadExprTag< ValueT,Num > >::operator=(ValueT(val));
00339   return *this;
00340       }
00341 
00343       SFad& operator=(const SFad& x) {
00344   Expr< SFadExprTag< ValueT,Num > >::operator=(static_cast<const Expr< SFadExprTag< ValueT,Num > >&>(x));
00345   return *this;
00346       }
00347 
00349       template <typename S> SFad& operator=(const Expr<S>& x) 
00350       {
00351   Expr< SFadExprTag< ValueT,Num > >::operator=(x);
00352   return *this;
00353       }
00354 
00355     }; // class SFad<ValueT,Num,ScalarT>
00356 
00358 
00363     template <typename ValueT, int Num>
00364     class SFad<ValueT,Num,ValueT> : 
00365       public Expr< SFadExprTag<ValueT,Num >  >{
00366 
00367     public:
00368 
00373 
00375 
00378       SFad() : Expr< SFadExprTag< ValueT,Num > >() {}
00379 
00381 
00384       SFad(const ValueT & x) : 
00385   Expr< SFadExprTag< ValueT,Num > >(x) {}
00386 
00388 
00391       SFad(const int sz, const ValueT & x) : 
00392   Expr< SFadExprTag< ValueT,Num > >(sz,x) {}
00393 
00395 
00400       SFad(const int sz, const int i, const ValueT & x) : 
00401   Expr< SFadExprTag< ValueT,Num > >(sz,i,x) {}
00402 
00404       SFad(const SFad& x) : 
00405   Expr< SFadExprTag< ValueT,Num > >(x) {}
00406 
00408       template <typename S> SFad(const Expr<S>& x) : 
00409   Expr< SFadExprTag< ValueT,Num > >(x) {}
00410 
00412 
00414       ~SFad() {}
00415 
00417       SFad& operator=(const ValueT& val) {
00418   Expr< SFadExprTag< ValueT,Num > >::operator=(val);
00419   return *this;
00420       }
00421 
00423       SFad& operator=(const SFad& x) {
00424   Expr< SFadExprTag< ValueT,Num > >::operator=(static_cast<const Expr< SFadExprTag< ValueT,Num > >&>(x));
00425   return *this;
00426       }
00427 
00429       template <typename S> SFad& operator=(const Expr<S>& x) 
00430       {
00431   Expr< SFadExprTag< ValueT,Num > >::operator=(x);
00432   return *this;
00433       }
00434 
00435     }; // class SFad<ValueT,Num>
00436 
00437   } // namespace Fad
00438 
00439 } // namespace Sacado
00440 
00441 #include "Sacado_Fad_SFadImp.hpp"
00442 #include "Sacado_Fad_Ops.hpp"
00443 
00444 #endif // SACADO_FAD_SFAD_HPP

Generated on Tue Oct 20 12:55:05 2009 for Sacado Package Browser (Single Doxygen Collection) by doxygen 1.4.7