|
Sacado Package Browser (Single Doxygen Collection) Version of the Day
|
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_ELRCACHEFAD_GENERALFAD_HPP 00055 #define SACADO_ELRCACHEFAD_GENERALFAD_HPP 00056 00057 #include "Sacado_ELRCacheFad_Expression.hpp" 00058 #include "Sacado_dummy_arg.hpp" 00059 00060 namespace Sacado { 00061 00063 namespace ELRCacheFad { 00064 00066 00071 template <typename T, typename Storage> 00072 class GeneralFad : public Storage { 00073 00074 public: 00075 00077 typedef T value_type; 00078 00080 typedef typename ScalarType<T>::type scalar_type; 00081 00086 00088 GeneralFad() : Storage(T(0.)), update_val_(true) {} 00089 00091 00094 GeneralFad(const T & x) : Storage(x), update_val_(true) {} 00095 00097 00100 GeneralFad(const int sz, const T & x) : 00101 Storage(sz, x), update_val_(true) {} 00102 00104 00109 GeneralFad(const int sz, const int i, const T & x) : 00110 Storage(sz, x), update_val_(true) { 00111 this->fastAccessDx(i)=1.; 00112 } 00113 00115 GeneralFad(const GeneralFad& x) : 00116 Storage(x), update_val_(x.update_val_) {} 00117 00119 template <typename S> GeneralFad(const Expr<S>& x); 00120 00122 ~GeneralFad() {} 00123 00125 00131 void diff(const int ith, const int n); 00132 00134 void setUpdateValue(bool update_val) { update_val_ = update_val; } 00135 00137 bool updateValue() const { return update_val_; } 00138 00140 void cache() const {} 00141 00143 template <typename S> 00144 bool isEqualTo(const Expr<S>& x) const { 00145 typedef IsEqual<value_type> IE; 00146 if (x.size() != this->size()) return false; 00147 bool eq = IE::eval(x.val(), this->val()); 00148 for (int i=0; i<this->size(); i++) 00149 eq = eq && IE::eval(x.dx(i), this->dx(i)); 00150 return eq; 00151 } 00152 00154 00159 00164 int availableSize() const { return this->length(); } 00165 00167 bool hasFastAccess() const { return this->size()!=0;} 00168 00170 bool isPassive() const { return this->size()==0;} 00171 00173 void setIsConstant(bool is_const) { 00174 if (is_const && this->size()!=0) 00175 this->resize(0); 00176 } 00177 00179 00184 00186 GeneralFad& operator=(const T& val); 00187 00189 GeneralFad& 00190 operator=(const GeneralFad& x); 00191 00193 template <typename S> 00194 GeneralFad& operator=(const Expr<S>& x); 00195 00197 00202 00204 GeneralFad& operator += (const T& x); 00205 00207 GeneralFad& operator -= (const T& x); 00208 00210 GeneralFad& operator *= (const T& x); 00211 00213 GeneralFad& operator /= (const T& x); 00214 00216 00220 GeneralFad& operator += (const typename dummy<value_type,scalar_type>::type& x); 00221 00223 00227 GeneralFad& operator -= (const typename dummy<value_type,scalar_type>::type& x); 00228 00230 00234 GeneralFad& operator *= (const typename dummy<value_type,scalar_type>::type& x); 00235 00237 00241 GeneralFad& operator /= (const typename dummy<value_type,scalar_type>::type& x); 00242 00244 template <typename S> 00245 GeneralFad& operator += (const Expr<S>& x); 00246 00248 template <typename S> 00249 GeneralFad& operator -= (const Expr<S>& x); 00250 00252 template <typename S> 00253 GeneralFad& operator *= (const Expr<S>& x); 00254 00256 template <typename S> 00257 GeneralFad& operator /= (const Expr<S>& x); 00258 00260 00261 protected: 00262 00264 bool update_val_; 00265 00266 // Functor for mpl::for_each to compute the local accumulation 00267 // of a tangent derivative 00268 template <typename ExprT> 00269 struct FastLocalAccumOp { 00270 typedef typename ExprT::value_type value_type; 00271 static const int N = ExprT::num_args; 00272 const ExprT& x; 00273 mutable value_type t; 00274 value_type partials[N]; 00275 const typename ExprT::base_expr_type* args[N]; 00276 int i; 00277 inline FastLocalAccumOp(const ExprT& x_) : x(x_) { 00278 x.computePartials(value_type(1.), partials); 00279 for (int j=0; j<N; j++) 00280 args[j] = &(x.getArg(j)); 00281 } 00282 template <typename ArgT> 00283 inline void operator () (ArgT arg) const { 00284 const int Arg = ArgT::value; 00285 t += partials[Arg] * args[Arg]->fastAccessDx(i); 00286 } 00287 }; 00288 00289 template <typename ExprT> 00290 struct SlowLocalAccumOp : FastLocalAccumOp<ExprT> { 00291 inline SlowLocalAccumOp(const ExprT& x_) : 00292 FastLocalAccumOp<ExprT>(x_) {} 00293 template <typename ArgT> 00294 inline void operator () (ArgT arg) const { 00295 const int Arg = ArgT::value; 00296 if (this->x.template isActive<Arg>()) 00297 this->t += this->partials[Arg] * this->args[Arg]->fastAccessDx(this->i); 00298 } 00299 }; 00300 00301 }; // class GeneralFad 00302 00303 00304 template <typename T, typename Storage> 00305 std::ostream& operator << (std::ostream& os, 00306 const GeneralFad<T,Storage>& x) { 00307 os << x.val() << " ["; 00308 00309 for (int i=0; i< x.size(); i++) { 00310 os << " " << x.dx(i); 00311 } 00312 00313 os << " ]"; 00314 return os; 00315 } 00316 00317 } // namespace ELRCacheFad 00318 00319 } // namespace Sacado 00320 00321 #include "Sacado_ELRCacheFad_GeneralFadImp.hpp" 00322 00323 #endif // SACADO_ELRCACHEFAD_GENERALFAD_HPP
1.7.4