Sacado_CacheFad_Expression.hpp

Go to the documentation of this file.
00001 // $Id: Sacado_CacheFad_Expression.hpp,v 1.6 2007/02/22 22:09:33 dmgay Exp $ 
00002 // $Source: /space/CVS/Trilinos/packages/sacado/src/Sacado_CacheFad_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_CACHEFAD_EXPRESSION_HPP
00055 #define SACADO_CACHEFAD_EXPRESSION_HPP
00056 
00057 #include "Sacado_Traits.hpp"
00058 
00059 namespace Sacado {
00060 
00061   namespace CacheFad {
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       value_type val() const { return expr_.val();}
00087 
00089       value_type dx(int i) const { return expr_.dx(i);}
00090 
00092       value_type fastAccessDx(int i) const { return expr_.fastAccessDx(i);}
00093       
00094     protected:
00095 
00097       Expr() {}
00098 
00100       ExprT expr_;
00101 
00102     }; // class Expr
00103 
00105 
00108     template <typename ConstT> 
00109     class ConstExpr {
00110 
00111     public:
00112 
00114       typedef ConstT value_type;
00115 
00117       ConstExpr(const ConstT& constant) : constant_(constant) {}
00118 
00120       int size() const { return 0; }
00121       
00123       bool hasFastAccess() const { return 1; }
00124 
00126       value_type val() const { return constant_; }
00127 
00129       value_type dx(int i) const { return value_type(0); }
00130       
00132       value_type fastAccessDx(int i) const { return value_type(0); }
00133 
00134     protected:
00135       
00137       const ConstT& constant_;
00138 
00139     }; // class ConstExpr
00140 
00142 
00151     template <typename ExprT, template<typename> class Op> 
00152     class UnaryExpr {
00153 
00154     public:
00155 
00157       typedef typename ExprT::value_type value_type;
00158 
00160       UnaryExpr(const ExprT& expr) : expr_(expr), op_(expr) {}
00161 
00163       int size() const { return expr_.size(); }
00164       
00166       bool hasFastAccess() const { return expr_.hasFastAccess(); }
00167 
00169       value_type val() const { return op_.computeValue(expr_); }
00170 
00172       value_type dx(int i) const { return op_.computeDx(i,expr_); }
00173       
00175       value_type fastAccessDx(int i) const { 
00176   return op_.computeFastAccessDx(i,expr_); 
00177       }
00178 
00179     protected:
00180       
00182       const ExprT& expr_;
00183 
00185       Op<ExprT> op_;
00186 
00187     }; // class UnaryExpr
00188 
00189     // The Sun compiler has difficulty with class partial specialization and
00190     // template templates, which the original BinaryExpr classes below use.
00191     // However the only significant difference between the specializations
00192     // of BinaryExpr for constant arguments is removing the reference
00193     // for the corresponding data member.  The type functions below allow
00194     // us to do this without specializing BinaryExpr.  We could also 
00195     // remove the template templates, but that drastically increases
00196     // compile times due to the increased length of template argument lists.
00197     template <typename T> struct ExprConstRef {
00198       typedef const T& type;
00199     };
00200     template <typename T> struct ExprConstRef< ConstExpr<T> > {
00201       typedef const ConstExpr<T> type;
00202     };
00203 
00205 
00215     template <typename ExprT1, typename ExprT2, 
00216         template<typename,typename> class Op> 
00217     class BinaryExpr {
00218     
00219     public:
00220 
00222       typedef typename ExprT1::value_type value_type_1;
00223 
00225       typedef typename ExprT2::value_type value_type_2;
00226 
00228       typedef typename Sacado::Promote<value_type_1,
00229                value_type_2>::type value_type;
00230 
00232       BinaryExpr(const ExprT1& expr1, const ExprT2& expr2) : 
00233   expr1_(expr1), expr2_(expr2), op_(expr1,expr2) {}
00234 
00236       int size() const {
00237   int sz1 = expr1_.size(), sz2 = expr2_.size(); 
00238   return sz1 > sz2 ? sz1 : sz2;
00239       }
00240       
00242       bool hasFastAccess() const { 
00243   return expr1_.hasFastAccess() && expr2_.hasFastAccess();}
00244 
00246       value_type val() const { 
00247   return op_.computeValue(expr1_,expr2_); }
00248 
00250       value_type dx(int i) const { 
00251   return op_.computeDx(i,expr1_,expr2_); }
00252       
00254       value_type fastAccessDx(int i) const { 
00255   return op_.computeFastAccessDx(i,expr1_,expr2_); 
00256       }
00257 
00258     protected:
00259       
00261       typename ExprConstRef<ExprT1>::type expr1_;
00262 
00264       typename ExprConstRef<ExprT2>::type expr2_;
00265 
00267       Op<ExprT1,ExprT2> op_;
00268 
00269     }; // class BinaryExpr
00270 
00271   } // namespace CacheFad
00272 
00273 } // namespace Sacado
00274 
00275 #endif // SACADO_CACHEFAD_EXPRESSION_HPP

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