Sacado Package Browser (Single Doxygen Collection) Version of the Day
Sacado_ELRCacheFad_GeneralFadImp.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 #include "Sacado_ConfigDefs.h"
00055 #include "Sacado_mpl_range_c.hpp"
00056 #include "Sacado_mpl_for_each.hpp"
00057 
00058 // template <typename T, typename Storage> 
00059 // template <typename S> 
00060 // inline Sacado::ELRCacheFad::GeneralFad<T,Storage>::GeneralFad(const Expr<S>& x) :
00061 //   Storage(T(0.))
00062 // {
00063 //   int sz = x.size();
00064 
00065 //   if (sz != this->size()) 
00066 //     this->resize(sz);
00067 
00068 //   if (sz) {
00069 
00070 //     // Number of arguments
00071 //     const int N = Expr<S>::num_args;
00072 
00073 //     // Array to store partials
00074 //     T partials[N];
00075 
00076 //     // Array to store tangents
00077 //     T dots[N];
00078 
00079 //     // Compute partials
00080 //     x.computePartials(T(1.), partials);
00081   
00082 //     // Compute each tangent direction
00083 //     for(int i=0; i<sz; ++i) {
00084 //       T t = T(0.);
00085 //       x.getTangents(i,dots);
00086 //       for (int j=0; j<N; j++) {
00087 //  t += partials[j]*dots[j];
00088 //       }
00089 //       this->fastAccessDx(i) = t;
00090 //     }
00091 
00092 //   }
00093   
00094 //   // Compute value
00095 //   this->val() = x.val();
00096 // }
00097 
00098 template <typename T, typename Storage> 
00099 template <typename S> 
00100 inline Sacado::ELRCacheFad::GeneralFad<T,Storage>::
00101 GeneralFad(const Expr<S>& x) :
00102   Storage(T(0.)),
00103   update_val_(x.updateValue())
00104 {
00105   x.cache();
00106 
00107   int sz = x.size();
00108 
00109   // Compute value
00110   if (update_val_)
00111     this->val() = x.val();
00112 
00113   if (sz != this->size()) 
00114     this->resize(sz);
00115 
00116   if (sz) {
00117 
00118     // Number of arguments
00119     const int N = Expr<S>::num_args;
00120 
00121     // Compute partials
00122     LocalAccumOp< Expr<S> > op(x);
00123   
00124     // Compute each tangent direction
00125     for(int i=0; i<sz; ++i) {
00126       op.t = T(0.);
00127       op.i = i;
00128 
00129       // Automatically unrolled loop that computes
00130       // for (int j=0; j<N; j++)
00131       //   op.t += op.partials[j] * x.getTangent<j>(i);
00132       Sacado::mpl::for_each< mpl::range_c< int, 0, N > > f(op);
00133 
00134       this->fastAccessDx(i) = op.t;
00135     }
00136 
00137   }
00138 }
00139 
00140 template <typename T, typename Storage> 
00141 inline void 
00142 Sacado::ELRCacheFad::GeneralFad<T,Storage>::
00143 diff(const int ith, const int n) 
00144 { 
00145   if (this->size() != n) 
00146     this->resize(n);
00147 
00148   this->zero();
00149   this->fastAccessDx(ith) = T(1.);
00150 
00151 }
00152 
00153 template <typename T, typename Storage> 
00154 inline Sacado::ELRCacheFad::GeneralFad<T,Storage>& 
00155 Sacado::ELRCacheFad::GeneralFad<T,Storage>::
00156 operator=(const T& v) 
00157 {
00158   this->val() = v;
00159 
00160   if (this->size()) {
00161     this->zero();    // We need to zero out the array for future resizes
00162     this->resize(0);
00163   }
00164 
00165   return *this;
00166 }
00167 
00168 template <typename T, typename Storage> 
00169 inline Sacado::ELRCacheFad::GeneralFad<T,Storage>& 
00170 Sacado::ELRCacheFad::GeneralFad<T,Storage>::
00171 operator=(const Sacado::ELRCacheFad::GeneralFad<T,Storage>& x) 
00172 {
00173   // Copy value & dx_
00174   Storage::operator=(x);
00175   update_val_ = x.update_val_;
00176   
00177   return *this;
00178 }
00179 
00180 template <typename T, typename Storage> 
00181 template <typename S> 
00182 inline Sacado::ELRCacheFad::GeneralFad<T,Storage>& 
00183 Sacado::ELRCacheFad::GeneralFad<T,Storage>::
00184 operator=(const Expr<S>& x) 
00185 {
00186   x.cache();
00187 
00188   int sz = x.size();
00189 
00190   if (sz != this->size()) 
00191     this->resize(sz);
00192 
00193   if (sz) {
00194 
00195     // Number of arguments
00196     const int N = Expr<S>::num_args;
00197 
00198     // Compute partials
00199     LocalAccumOp< Expr<S> > op(x);
00200   
00201     // Compute each tangent direction
00202     for(int i=0; i<sz; ++i) {
00203       op.t = T(0.);
00204       op.i = i;
00205 
00206       // Automatically unrolled loop that computes
00207       // for (int j=0; j<N; j++)
00208       //   op.t += op.partials[j] * x.getTangent<j>(i);
00209       Sacado::mpl::for_each< mpl::range_c< int, 0, N > > f(op);
00210 
00211       this->fastAccessDx(i) = op.t;
00212     }
00213 
00214   }
00215   
00216   update_val_ = x.updateValue();
00217   if (update_val_)
00218     this->val() = x.val();
00219   
00220   return *this;
00221 }
00222 
00223 template <typename T, typename Storage> 
00224 inline  Sacado::ELRCacheFad::GeneralFad<T,Storage>& 
00225 Sacado::ELRCacheFad::GeneralFad<T,Storage>::
00226 operator += (const T& v)
00227 {
00228   if (update_val_)
00229     this->val() += v;
00230 
00231   return *this;
00232 }
00233 
00234 template <typename T, typename Storage> 
00235 inline Sacado::ELRCacheFad::GeneralFad<T,Storage>& 
00236 Sacado::ELRCacheFad::GeneralFad<T,Storage>::
00237 operator -= (const T& v)
00238 {
00239   if (update_val_)
00240     this->val() -= v;
00241 
00242   return *this;
00243 }
00244 
00245 template <typename T, typename Storage> 
00246 inline Sacado::ELRCacheFad::GeneralFad<T,Storage>& 
00247 Sacado::ELRCacheFad::GeneralFad<T,Storage>::
00248 operator *= (const T& v)
00249 {
00250   int sz = this->size();
00251 
00252   if (update_val_)
00253     this->val() *= v;
00254   for (int i=0; i<sz; ++i)
00255     this->fastAccessDx(i) *= v;
00256 
00257   return *this;
00258 }
00259 
00260 template <typename T, typename Storage> 
00261 inline Sacado::ELRCacheFad::GeneralFad<T,Storage>& 
00262 Sacado::ELRCacheFad::GeneralFad<T,Storage>::
00263 operator /= (const T& v)
00264 {
00265   int sz = this->size();
00266 
00267   if (update_val_)
00268     this->val() /= v;
00269   for (int i=0; i<sz; ++i)
00270     this->fastAccessDx(i) /= v;
00271 
00272   return *this;
00273 }
00274 
00275 template <typename T, typename Storage> 
00276 inline  Sacado::ELRCacheFad::GeneralFad<T,Storage>& 
00277 Sacado::ELRCacheFad::GeneralFad<T,Storage>::
00278 operator += (const typename Sacado::dummy<value_type,scalar_type>::type& v)
00279 {
00280   if (update_val_)
00281     this->val() += v;
00282 
00283   return *this;
00284 }
00285 
00286 template <typename T, typename Storage> 
00287 inline Sacado::ELRCacheFad::GeneralFad<T,Storage>& 
00288 Sacado::ELRCacheFad::GeneralFad<T,Storage>::
00289 operator -= (const typename Sacado::dummy<value_type,scalar_type>::type& v)
00290 {
00291   if (update_val_)
00292     this->val() -= v;
00293 
00294   return *this;
00295 }
00296 
00297 template <typename T, typename Storage> 
00298 inline Sacado::ELRCacheFad::GeneralFad<T,Storage>& 
00299 Sacado::ELRCacheFad::GeneralFad<T,Storage>::
00300 operator *= (const typename Sacado::dummy<value_type,scalar_type>::type& v)
00301 {
00302   int sz = this->size();
00303 
00304   if (update_val_)
00305     this->val() *= v;
00306   for (int i=0; i<sz; ++i)
00307     this->fastAccessDx(i) *= v;
00308 
00309   return *this;
00310 }
00311 
00312 template <typename T, typename Storage> 
00313 inline Sacado::ELRCacheFad::GeneralFad<T,Storage>& 
00314 Sacado::ELRCacheFad::GeneralFad<T,Storage>::
00315 operator /= (const typename Sacado::dummy<value_type,scalar_type>::type& v)
00316 {
00317   int sz = this->size();
00318 
00319   if (update_val_)
00320     this->val() /= v;
00321   for (int i=0; i<sz; ++i)
00322     this->fastAccessDx(i) /= v;
00323 
00324   return *this;
00325 }
00326 
00327 template <typename T, typename Storage> 
00328 template <typename S> 
00329 inline Sacado::ELRCacheFad::GeneralFad<T,Storage>& 
00330 Sacado::ELRCacheFad::GeneralFad<T,Storage>::
00331 operator += (const Sacado::ELRCacheFad::Expr<S>& x)
00332 {
00333   x.cache();
00334 
00335   int xsz = x.size(), sz = this->size();
00336 
00337 #ifdef SACADO_DEBUG
00338   if ((xsz != sz) && (xsz != 0) && (sz != 0))
00339     throw "Fad Error:  Attempt to assign with incompatible sizes";
00340 #endif
00341 
00342   // Number of arguments
00343   const int N = Expr<S>::num_args;
00344 
00345   if (xsz) {
00346 
00347     // Compute partials
00348     LocalAccumOp< Expr<S> > op(x);
00349 
00350     if (sz) {
00351 
00352       // Compute each tangent direction
00353       for(int i=0; i<xsz; ++i) {
00354   op.t = T(0.);
00355   op.i = i;
00356   
00357   // Automatically unrolled loop that computes
00358   // for (int j=0; j<N; j++)
00359   //   op.t += op.partials[j] * x.getTangent<j>(i);
00360   Sacado::mpl::for_each< mpl::range_c< int, 0, N > > f(op);
00361   
00362   this->fastAccessDx(i) += op.t;
00363       }
00364 
00365     }
00366 
00367     else {
00368 
00369       this->resize(xsz);
00370 
00371       // Compute each tangent direction
00372       for(int i=0; i<xsz; ++i) {
00373   op.t = T(0.);
00374   op.i = i;
00375   
00376   // Automatically unrolled loop that computes
00377   // for (int j=0; j<N; j++)
00378   //   op.t += op.partials[j] * x.getTangent<j>(i);
00379   Sacado::mpl::for_each< mpl::range_c< int, 0, N > > f(op);
00380   
00381   this->fastAccessDx(i) = op.t;
00382       }
00383 
00384     }
00385 
00386   }
00387   
00388   update_val_ = x.updateValue();
00389   if (update_val_)
00390     this->val() += x.val();
00391 
00392   return *this;
00393 }
00394 
00395 template <typename T, typename Storage> 
00396 template <typename S> 
00397 inline Sacado::ELRCacheFad::GeneralFad<T,Storage>& 
00398 Sacado::ELRCacheFad::GeneralFad<T,Storage>::
00399 operator -= (const Sacado::ELRCacheFad::Expr<S>& x)
00400 {
00401   x.cache();
00402 
00403   int xsz = x.size(), sz = this->size();
00404 
00405 #ifdef SACADO_DEBUG
00406   if ((xsz != sz) && (xsz != 0) && (sz != 0))
00407     throw "Fad Error:  Attempt to assign with incompatible sizes";
00408 #endif
00409 
00410   // Number of arguments
00411   const int N = Expr<S>::num_args;
00412 
00413   if (xsz) {
00414 
00415     // Compute partials
00416     LocalAccumOp< Expr<S> > op(x);
00417 
00418     if (sz) {
00419 
00420       // Compute each tangent direction
00421       for(int i=0; i<xsz; ++i) {
00422   op.t = T(0.);
00423   op.i = i;
00424   
00425   // Automatically unrolled loop that computes
00426   // for (int j=0; j<N; j++)
00427   //   op.t += op.partials[j] * x.getTangent<j>(i);
00428   Sacado::mpl::for_each< mpl::range_c< int, 0, N > > f(op);
00429   
00430   this->fastAccessDx(i) -= op.t;
00431       }
00432 
00433     }
00434 
00435     else {
00436 
00437       this->resize(xsz);
00438 
00439       // Compute each tangent direction
00440       for(int i=0; i<xsz; ++i) {
00441   op.t = T(0.);
00442   op.i = i;
00443   
00444   // Automatically unrolled loop that computes
00445   // for (int j=0; j<N; j++)
00446   //   op.t += op.partials[j] * x.getTangent<j>(i);
00447   Sacado::mpl::for_each< mpl::range_c< int, 0, N > > f(op);
00448   
00449   this->fastAccessDx(i) = -op.t;
00450       }
00451 
00452     }
00453 
00454   }
00455 
00456   update_val_ = x.updateValue();
00457   if (update_val_)
00458     this->val() -= x.val();
00459 
00460   return *this;
00461 }
00462 
00463 template <typename T, typename Storage> 
00464 template <typename S> 
00465 inline Sacado::ELRCacheFad::GeneralFad<T,Storage>& 
00466 Sacado::ELRCacheFad::GeneralFad<T,Storage>::
00467 operator *= (const Sacado::ELRCacheFad::Expr<S>& x)
00468 {
00469   x.cache();
00470 
00471   int xsz = x.size(), sz = this->size();
00472   update_val_ = x.updateValue();
00473   T xval = x.val();
00474 
00475 #ifdef SACADO_DEBUG
00476   if ((xsz != sz) && (xsz != 0) && (sz != 0))
00477     throw "Fad Error:  Attempt to assign with incompatible sizes";
00478 #endif
00479 
00480   // Number of arguments
00481   const int N = Expr<S>::num_args;
00482 
00483   if (xsz) {
00484 
00485     // Compute partials
00486     LocalAccumOp< Expr<S> > op(x);
00487 
00488     if (sz) {
00489 
00490       // Compute each tangent direction
00491       for(int i=0; i<xsz; ++i) {
00492   op.t = T(0.);
00493   op.i = i;
00494   
00495   // Automatically unrolled loop that computes
00496   // for (int j=0; j<N; j++)
00497   //   op.t += op.partials[j] * x.getTangent<j>(i);
00498   Sacado::mpl::for_each< mpl::range_c< int, 0, N > > f(op);
00499   
00500   this->fastAccessDx(i) = this->val() * op.t + this->fastAccessDx(i) * xval;
00501       }
00502 
00503     }
00504 
00505     else {
00506 
00507       this->resize(xsz);
00508 
00509       // Compute each tangent direction
00510       for(int i=0; i<xsz; ++i) {
00511   op.t = T(0.);
00512   op.i = i;
00513   
00514   // Automatically unrolled loop that computes
00515   // for (int j=0; j<N; j++)
00516   //   op.t += op.partials[j] * x.getTangent<j>(i);
00517   Sacado::mpl::for_each< mpl::range_c< int, 0, N > > f(op);
00518   
00519   this->fastAccessDx(i) = this->val() * op.t;
00520       }
00521 
00522     }
00523 
00524   }
00525 
00526   else {
00527 
00528     if (sz) {
00529       for (int i=0; i<sz; ++i)
00530   this->fastAccessDx(i) *= xval;
00531     }
00532 
00533   }
00534 
00535   if (update_val_)
00536     this->val() *= xval;
00537 
00538   return *this;
00539 }
00540 
00541 template <typename T, typename Storage>
00542 template <typename S> 
00543 inline Sacado::ELRCacheFad::GeneralFad<T,Storage>& 
00544 Sacado::ELRCacheFad::GeneralFad<T,Storage>::
00545 operator /= (const Sacado::ELRCacheFad::Expr<S>& x)
00546 {
00547   x.cache();
00548 
00549   int xsz = x.size(), sz = this->size();
00550   update_val_ = x.updateValue();
00551   T xval = x.val();
00552 
00553 #ifdef SACADO_DEBUG
00554   if ((xsz != sz) && (xsz != 0) && (sz != 0))
00555     throw "Fad Error:  Attempt to assign with incompatible sizes";
00556 #endif
00557 
00558   // Number of arguments
00559   const int N = Expr<S>::num_args;
00560 
00561   if (xsz) {
00562 
00563     // Compute partials
00564     LocalAccumOp< Expr<S> > op(x);
00565 
00566     T xval2 = xval*xval;
00567 
00568     if (sz) {
00569 
00570       // Compute each tangent direction
00571       for(int i=0; i<xsz; ++i) {
00572   op.t = T(0.);
00573   op.i = i;
00574   
00575   // Automatically unrolled loop that computes
00576   // for (int j=0; j<N; j++)
00577   //   op.t += op.partials[j] * x.getTangent<j>(i);
00578   Sacado::mpl::for_each< mpl::range_c< int, 0, N > > f(op);
00579   
00580   this->fastAccessDx(i) = (this->fastAccessDx(i) * xval - this->val() * op.t) / xval2;
00581       }
00582 
00583     }
00584 
00585     else {
00586 
00587       this->resize(xsz);
00588 
00589       // Compute each tangent direction
00590       for(int i=0; i<xsz; ++i) {
00591   op.t = T(0.);
00592   op.i = i;
00593   
00594   // Automatically unrolled loop that computes
00595   // for (int j=0; j<N; j++)
00596   //   op.t += op.partials[j] * x.getTangent<j>(i);
00597   Sacado::mpl::for_each< mpl::range_c< int, 0, N > > f(op);
00598   
00599   this->fastAccessDx(i) = -this->val() * op.t / xval2;
00600       }
00601 
00602     }
00603 
00604   }
00605 
00606   else {
00607 
00608     if (sz) {
00609       for (int i=0; i<sz; ++i)
00610   this->fastAccessDx(i) /= xval;
00611     }
00612 
00613   }
00614 
00615   if (update_val_)
00616     this->val() /= xval;
00617 
00618   return *this;
00619 }
00620 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines