|
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 // @HEADER 00031 00032 #ifndef SACADO_TAY_CACHETAYLOREXPR_HPP 00033 #define SACADO_TAY_CACHETAYLOREXPR_HPP 00034 00035 #include "Sacado_Traits.hpp" 00036 00037 namespace Sacado { 00038 00039 namespace Tay { 00040 00042 00046 template <typename ExprT> 00047 class Expr { 00048 00049 public: 00050 00052 typedef typename ExprT::value_type value_type; 00053 00055 explicit Expr(const ExprT& expr) : expr_(expr) {} 00056 00058 void allocateCache(unsigned int d) const { expr_.allocateCache(d); } 00059 00061 unsigned int degree() const {return expr_.degree();} 00062 00064 bool hasFastAccess(unsigned int d) const { 00065 return expr_.hasFastAccess(d);} 00066 00068 value_type coeff(unsigned int i) const { return expr_.coeff(i);} 00069 00071 value_type fastAccessCoeff(unsigned int i) const { return 00072 expr_.fastAccessCoeff(i);} 00073 00074 protected: 00075 00077 Expr() {} 00078 00080 ExprT expr_; 00081 00082 }; // class Expr 00083 00085 00088 template <typename ConstT> 00089 class ConstExpr { 00090 00091 public: 00092 00094 typedef ConstT value_type; 00095 00097 ConstExpr(const ConstT& constant) : constant_(constant) {} 00098 00100 void allocateCache(unsigned int d) const {} 00101 00103 unsigned int degree() const { return 0; } 00104 00106 bool hasFastAccess(unsigned int d) const { return 1; } 00107 00108 value_type value() const { return constant_; } 00109 00111 value_type coeff(unsigned int i) const { 00112 return i==0 ? constant_ : value_type(0); } 00113 00115 value_type fastAccessCoeff(unsigned int i) const { 00116 return i==0 ? constant_ : value_type(0); } 00117 00118 protected: 00119 00121 ConstT constant_; 00122 00123 }; // class ConstExpr 00124 00126 00132 template <typename ExprT, template<typename> class Op> 00133 class UnaryExpr { 00134 00135 public: 00136 00138 typedef typename ExprT::value_type value_type; 00139 00141 UnaryExpr(const ExprT& expr) : expr_(expr), op_(expr) {} 00142 00144 void allocateCache(unsigned int d) const { 00145 expr_.allocateCache(d); 00146 op_.allocateCache(d); 00147 } 00148 00150 unsigned int degree() const {return expr_.degree();} 00151 00153 bool hasFastAccess(unsigned int d) const { 00154 return expr_.hasFastAccess(d); } 00155 00157 value_type coeff(unsigned int i) const { 00158 return op_.computeCoeff(i,expr_); } 00159 00161 value_type fastAccessCoeff(unsigned int i) const { 00162 return op_.computeFastAccessCoeff(i,expr_); 00163 } 00164 00165 protected: 00166 00168 ExprT expr_; 00169 00171 Op<ExprT> op_; 00172 00173 }; // class UnaryExpr 00174 00176 00183 template <typename ExprT1, typename ExprT2, 00184 template<typename,typename> class Op> 00185 class BinaryExpr { 00186 00187 public: 00188 00190 typedef typename ExprT1::value_type value_type_1; 00191 00193 typedef typename ExprT2::value_type value_type_2; 00194 00196 typedef typename Sacado::Promote<value_type_1, 00197 value_type_2>::type value_type; 00198 00200 BinaryExpr(const ExprT1& expr1, const ExprT2& expr2) : 00201 expr1_(expr1), expr2_(expr2), op_(expr1,expr2) {} 00202 00204 void allocateCache(unsigned int d) const { 00205 expr1_.allocateCache(d); 00206 expr2_.allocateCache(d); 00207 op_.allocateCache(d); 00208 } 00209 00211 unsigned int degree() const { 00212 unsigned int d1 = expr1_.degree(), d2 = expr2_.degree(); 00213 return d1 > d2 ? d1 : d2; 00214 } 00215 00217 bool hasFastAccess(unsigned int d) const { 00218 return expr1_.hasFastAccess(d) && expr2_.hasFastAccess(d);} 00219 00221 value_type coeff(unsigned int i) const { 00222 return op_.computeCoeff(i,expr1_,expr2_); } 00223 00225 value_type fastAccessCoeff(unsigned int i) const { 00226 return op_.computeFastAccessCoeff(i,expr1_,expr2_); 00227 } 00228 00229 protected: 00230 00232 ExprT1 expr1_; 00233 00235 ExprT2 expr2_; 00236 00238 Op<ExprT1,ExprT2> op_; 00239 00240 }; // class BinaryExpr 00241 00243 00250 template <typename ExprT2, template<typename,typename> class Op> 00251 class BinaryExpr<ConstExpr<typename ExprT2::value_type>, ExprT2, Op> { 00252 00253 public: 00254 00256 typedef ConstExpr<typename ExprT2::value_type> ExprT1; 00257 00259 typedef typename ExprT2::value_type value_type; 00260 00262 BinaryExpr(const ExprT1& expr1, const ExprT2& expr2) : 00263 expr1_(expr1), expr2_(expr2), op_(expr1,expr2) {} 00264 00266 void allocateCache(unsigned int d) const { 00267 expr2_.allocateCache(d); 00268 op_.allocateCache(d); 00269 } 00270 00272 unsigned int degree() const { 00273 return expr2_.degree(); 00274 } 00275 00277 bool hasFastAccess(unsigned int d) const { 00278 return expr2_.hasFastAccess(d);} 00279 00281 value_type coeff(unsigned int i) const { 00282 return op_.computeCoeff(i,expr1_,expr2_); } 00283 00285 value_type fastAccessCoeff(unsigned int i) const { 00286 return op_.computeFastAccessCoeff(i,expr1_,expr2_); 00287 } 00288 00289 protected: 00290 00292 ExprT1 expr1_; 00293 00295 ExprT2 expr2_; 00296 00298 Op<ExprT1,ExprT2> op_; 00299 00300 }; // class BinaryExpr 00301 00303 00310 template <typename ExprT1, template<typename,typename> class Op> 00311 class BinaryExpr<ExprT1,ConstExpr<typename ExprT1::value_type>, Op> { 00312 00313 public: 00314 00316 typedef ConstExpr<typename ExprT1::value_type> ExprT2; 00317 00319 typedef typename ExprT1::value_type value_type; 00320 00322 BinaryExpr(const ExprT1& expr1, const ExprT2& expr2) : 00323 expr1_(expr1), expr2_(expr2), op_(expr1,expr2) {} 00324 00326 void allocateCache(unsigned int d) const { 00327 expr1_.allocateCache(d); 00328 op_.allocateCache(d); 00329 } 00330 00332 unsigned int degree() const { 00333 return expr1_.degree(); 00334 } 00335 00337 bool hasFastAccess(unsigned int d) const { 00338 return expr1_.hasFastAccess(d);} 00339 00341 value_type coeff(unsigned int i) const { 00342 return op_.computeCoeff(i,expr1_,expr2_); } 00343 00345 value_type fastAccessCoeff(unsigned int i) const { 00346 return op_.computeFastAccessCoeff(i,expr1_,expr2_); 00347 } 00348 00349 protected: 00350 00352 ExprT1 expr1_; 00353 00355 ExprT2 expr2_; 00356 00358 Op<ExprT1,ExprT2> op_; 00359 00360 }; // class BinaryExpr 00361 00362 } // namespace Tay 00363 00364 } // namespace Sacado 00365 00366 #endif // SACADO_TAY_CACHETAYLOREXPR_HPP
1.7.4