Sacado_Fad_Expression.hpp

Go to the documentation of this file.
00001 // $Id: Sacado_Fad_Expression.hpp,v 1.8 2007/02/22 22:09:33 dmgay Exp $ 
00002 // $Source: /space/CVS/Trilinos/packages/sacado/src/Sacado_Fad_Expression.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_EXPRESSION_HPP
00055 #define SACADO_FAD_EXPRESSION_HPP
00056 
00057 #include "Sacado_Traits.hpp"
00058 
00059 namespace Sacado {
00060 
00061   namespace Fad {
00062 
00064 
00068     template <typename ExprT> 
00069     class Expr {
00070 
00071     public:
00072 
00074       typedef typename ExprT::value_type value_type;
00075 
00077       explicit Expr(const ExprT& expr) : expr_(expr) {}
00078 
00080       int size() const {return expr_.size();}
00081       
00083       bool hasFastAccess() const { return expr_.hasFastAccess();}
00084 
00086       bool isPassive() const { return expr_.isPassive();}
00087 
00089       value_type val() const { return expr_.val();}
00090 
00092       value_type dx(int i) const { return expr_.dx(i);}
00093 
00095       value_type fastAccessDx(int i) const { return expr_.fastAccessDx(i);}
00096       
00097     protected:
00098 
00100       Expr() {}
00101 
00103       ExprT expr_;
00104 
00105     }; // class Expr
00106 
00108 
00111     template <typename ConstT> 
00112     class ConstExpr {
00113 
00114     public:
00115 
00117       typedef ConstT value_type;
00118 
00120       ConstExpr(const ConstT& constant) : constant_(constant) {}
00121 
00123       int size() const { return 0; }
00124       
00126       bool hasFastAccess() const { return true; }
00127 
00129       bool isPassive() const { return true; }
00130 
00132       value_type val() const { return constant_; }
00133 
00135       value_type dx(int i) const { return value_type(0); }
00136       
00138       value_type fastAccessDx(int i) const { return value_type(0); }
00139 
00140     protected:
00141       
00143       const ConstT& constant_;
00144 
00145     }; // class ConstExpr
00146 
00148 
00154     template <typename ExprT, template<typename> class Op> 
00155     class UnaryExpr {
00156 
00157     public:
00158 
00160       typedef typename ExprT::value_type value_type;
00161       typedef Op<ExprT> OpT;
00162 
00164       UnaryExpr(const ExprT& expr) : expr_(expr) {}
00165 
00167       int size() const { return expr_.size(); }
00168       
00170       bool hasFastAccess() const { return expr_.hasFastAccess(); }
00171 
00173       bool isPassive() const { return expr_.isPassive();}
00174 
00176       value_type val() const { return OpT::computeValue(expr_); }
00177 
00179       value_type dx(int i) const { return OpT::computeDx(i,expr_); }
00180       
00182       value_type fastAccessDx(int i) const { 
00183   return OpT::computeFastAccessDx(i,expr_); 
00184       }
00185 
00186     protected:
00187       
00189       const ExprT& expr_;
00190 
00191     }; // class UnaryExpr
00192 
00193     // The Sun compiler has difficulty with class partial specialization and
00194     // template templates, which the original BinaryExpr classes below use.
00195     // However the only significant difference between the specializations
00196     // of BinaryExpr for constant arguments is removing the reference
00197     // for the corresponding data member.  The type functions below allow
00198     // us to do this without specializing BinaryExpr.  We could also 
00199     // remove the template templates, but that drastically increases
00200     // compile times due to the increased length of template argument lists.
00201     template <typename T> struct ExprConstRef {
00202       typedef const T& type;
00203     };
00204     template <typename T> struct ExprConstRef< ConstExpr<T> > {
00205       typedef const ConstExpr<T> type;
00206     };
00207 
00209 
00216     template <typename ExprT1, typename ExprT2, 
00217         template<typename,typename> class Op> 
00218     class BinaryExpr {
00219     
00220     public:
00221 
00223       typedef typename ExprT1::value_type value_type_1;
00224 
00226       typedef typename ExprT2::value_type value_type_2;
00227 
00229       typedef typename Sacado::Promote<value_type_1,
00230                value_type_2>::type value_type;
00231 
00232       typedef Op<ExprT1,ExprT2> OpT;
00233 
00235       BinaryExpr(const ExprT1& expr1, const ExprT2& expr2) : 
00236   expr1_(expr1), expr2_(expr2) {}
00237 
00239       int size() const {
00240   int sz1 = expr1_.size(), sz2 = expr2_.size(); 
00241   return sz1 > sz2 ? sz1 : sz2;
00242       }
00243       
00245       bool hasFastAccess() const { 
00246   return expr1_.hasFastAccess() && expr2_.hasFastAccess();}
00247 
00249       bool isPassive() const { 
00250   return expr1_.isPassive() && expr2_.isPassive();}
00251 
00253       value_type val() const { 
00254   return OpT::computeValue(expr1_,expr2_); }
00255 
00257       value_type dx(int i) const { 
00258   return OpT::computeDx(i,expr1_,expr2_); }
00259       
00261       value_type fastAccessDx(int i) const { 
00262   return OpT::computeFastAccessDx(i,expr1_,expr2_); 
00263       }
00264 
00265     protected:
00266       
00268       typename ExprConstRef<ExprT1>::type expr1_;
00269 
00271       typename ExprConstRef<ExprT2>::type expr2_;
00272 
00273     }; // class BinaryExpr
00274 
00275   } // namespace Fad
00276 
00277 } // namespace Sacado
00278 
00279 #endif // SACADO_FAD_EXPRESSION_HPP

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