Sacado Package Browser (Single Doxygen Collection) Version of the Day
Sacado_Tay_Taylor.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 terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00010 // license for use of this work by or on behalf of the U.S. Government.
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_TAYLOR_HPP
00033 #define SACADO_TAY_TAYLOR_HPP
00034 
00035 #include "Sacado_Handle.hpp"
00036 #include <cmath>
00037 #include <algorithm>  // for std::min and std::max
00038 #include <ostream>  // for std::ostream
00039 #include "Sacado_dummy_arg.hpp"
00040 
00041 namespace Sacado {
00042 
00044   namespace Tay {
00045 
00047 
00051     template <typename T> 
00052     class Taylor {
00053     public:
00054 
00056       template <typename U> 
00057       struct apply {
00058   typedef Taylor<U> type;
00059       };
00060 
00062       typedef T value_type;
00063 
00065       typedef typename ScalarType<value_type>::type scalar_type;
00066 
00068       Taylor();
00069 
00071 
00074       Taylor(const T& x);
00075 
00077 
00081       Taylor(const typename dummy<value_type,scalar_type>::type& x);
00082 
00084 
00087       Taylor(unsigned int d, const T & x);
00088 
00090 
00093       Taylor(unsigned int d);
00094 
00096       Taylor(const Taylor& x);
00097 
00099       ~Taylor();
00100 
00102 
00106       void resize(unsigned int d, bool keep_coeffs);
00107 
00109 
00112       void reserve(unsigned int d);
00113 
00115 
00124       void copyForWrite() { th.makeOwnCopy(); }
00125 
00127       bool isEqualTo(const Taylor& x) const {
00128   typedef IsEqual<value_type> IE;
00129   if (x.degree() != this->degree()) return false;
00130   bool eq = true;
00131   for (unsigned int i=0; i<=this->degree(); i++)
00132     eq = eq && IE::eval(x.coeff(i), this->coeff(i));
00133   return eq;
00134       }
00135 
00140 
00142       Taylor<T>& operator=(const T& val);
00143 
00145 
00149       Taylor<T>& operator=(const typename dummy<value_type,scalar_type>::type& val);
00150 
00152       Taylor<T>& operator=(const Taylor<T>& x);
00153 
00155 
00160 
00162       const T& val() const { return th->coeff_[0];}
00163 
00165       T& val() { return th->coeff_[0];}
00166 
00168 
00173 
00175       unsigned int degree() const { return th->deg_;}
00176 
00178       bool hasFastAccess(unsigned int d) const { return th->deg_>=d;}
00179 
00181       const T* coeff() const { return th->coeff_;}
00182 
00184       T* coeff() { return th->coeff_;}
00185 
00187       T coeff(unsigned int i) const { 
00188   T tmp= i<=th->deg_ ? th->coeff_[i]:T(0.); return tmp;}
00189     
00191       T& fastAccessCoeff(unsigned int i) { return th->coeff_[i];}
00192 
00194       const T& fastAccessCoeff(unsigned int i) const { return th->coeff_[i];}
00195     
00197 
00202 
00204       Taylor<T> operator + () const;
00205 
00207       Taylor<T> operator - () const;
00208 
00210       Taylor<T>& operator += (const T& x);
00211 
00213       Taylor<T>& operator -= (const T& x);
00214 
00216       Taylor<T>& operator *= (const T& x);
00217 
00219       Taylor<T>& operator /= (const T& x);
00220 
00222       Taylor<T>& operator += (const Taylor<T>& x);
00223 
00225       Taylor<T>& operator -= (const Taylor<T>& x);
00226   
00228       Taylor<T>& operator *= (const Taylor<T>& x);
00229 
00231       Taylor<T>& operator /= (const Taylor<T>& x);
00232 
00234 
00235     protected:
00236 
00238       unsigned int length() const { return th->len_; }
00239 
00241       void resizeCoeffs(unsigned int len);
00242 
00243     protected:
00244 
00245       struct TaylorData {
00246 
00248   T* coeff_;
00249 
00251   unsigned int deg_;
00252 
00254   unsigned int len_;
00255 
00257   TaylorData();
00258 
00260   TaylorData(const T& x);
00261 
00263   TaylorData(unsigned int d, const T & x);
00264 
00266   TaylorData(unsigned int d);
00267 
00269   TaylorData(unsigned int d, unsigned int l);
00270 
00272   TaylorData(const TaylorData& x);
00273 
00275   ~TaylorData();
00276 
00278   TaylorData& operator=(const TaylorData& x);
00279 
00280       };
00281 
00282       Sacado::Handle<TaylorData> th;
00283 
00284     }; // class Taylor
00285 
00286     // Operations
00287     template <typename T> Taylor<T> operator+(const Taylor<T>& a,
00288                 const Taylor<T>& b);
00289     template <typename T> Taylor<T> operator+(const T& a, 
00290                 const Taylor<T>& b);
00291     template <typename T> Taylor<T> operator+(const Taylor<T>& a, 
00292                 const T& b);
00293     template <typename T> Taylor<T> operator-(const Taylor<T>& a, 
00294                 const Taylor<T>& b);
00295     template <typename T> Taylor<T> operator-(const T& a, 
00296                 const Taylor<T>& b);
00297     template <typename T> Taylor<T> operator-(const Taylor<T>& a, 
00298                 const T& b);
00299     template <typename T> Taylor<T> operator*(const Taylor<T>& a, 
00300                 const Taylor<T>& b);
00301     template <typename T> Taylor<T> operator*(const T& a, 
00302                 const Taylor<T>& b);
00303     template <typename T> Taylor<T> operator*(const Taylor<T>& a, 
00304                 const T& b);
00305     template <typename T> Taylor<T> operator/(const Taylor<T>& a, 
00306                 const Taylor<T>& b);
00307     template <typename T> Taylor<T> operator/(const T& a, 
00308                 const Taylor<T>& b);
00309     template <typename T> Taylor<T> operator/(const Taylor<T>& a, 
00310                 const T& b);
00311     template <typename T> Taylor<T> exp(const Taylor<T>& a);
00312     template <typename T> Taylor<T> log(const Taylor<T>& a);
00313     template <typename T> Taylor<T> log10(const Taylor<T>& a);
00314     template <typename T> Taylor<T> sqrt(const Taylor<T>& a);
00315     template <typename T> Taylor<T> pow(const Taylor<T>& a, 
00316           const Taylor<T>& b);
00317     template <typename T> Taylor<T> pow(const T& a,
00318           const Taylor<T>& b);
00319     template <typename T> Taylor<T> pow(const Taylor<T>& a,
00320           const T& b);
00321     template <typename T> void sincos(const Taylor<T>& a,
00322               Taylor<T>& s, Taylor<T>& c);
00323     template <typename T> Taylor<T> cos(const Taylor<T>& a);
00324     template <typename T> Taylor<T> sin(const Taylor<T>& a);
00325     template <typename T> Taylor<T> tan(const Taylor<T>& a);
00326     template <typename T> void sinhcosh(const Taylor<T>& a,
00327           Taylor<T>& s, Taylor<T>& c);
00328     template <typename T> Taylor<T> cosh(const Taylor<T>& a);
00329     template <typename T> Taylor<T> sinh(const Taylor<T>& a);
00330     template <typename T> Taylor<T> tanh(const Taylor<T>& a);
00331     template <typename T> Taylor<T> quad(const T& c0,
00332            const Taylor<T>& a,
00333            const Taylor<T>& b);
00334     template <typename T> Taylor<T> acos(const Taylor<T>& a);
00335     template <typename T> Taylor<T> asin(const Taylor<T>& a);
00336     template <typename T> Taylor<T> atan(const Taylor<T>& a);
00337     template <typename T> Taylor<T> atan2(const Taylor<T>& a,
00338             const Taylor<T>& b);
00339     template <typename T> Taylor<T> atan2(const T& a,
00340             const Taylor<T>& b);
00341     template <typename T> Taylor<T> atan2(const Taylor<T>& a,
00342             const T& b);
00343     template <typename T> Taylor<T> acosh(const Taylor<T>& a);
00344     template <typename T> Taylor<T> asinh(const Taylor<T>& a);
00345     template <typename T> Taylor<T> atanh(const Taylor<T>& a);
00346     template <typename T> Taylor<T> abs(const Taylor<T>& a);
00347     template <typename T> Taylor<T> fabs(const Taylor<T>& a);
00348     template <typename T> Taylor<T> max(const Taylor<T>& a,
00349           const Taylor<T>& b);
00350     template <typename T> Taylor<T> max(const T& a,
00351           const Taylor<T>& b);
00352     template <typename T> Taylor<T> max(const Taylor<T>& a,
00353           const T& b);
00354     template <typename T> Taylor<T> min(const Taylor<T>& a,
00355           const Taylor<T>& b);
00356     template <typename T> Taylor<T> min(const T& a,
00357           const Taylor<T>& b);
00358     template <typename T> Taylor<T> min(const Taylor<T>& a,
00359           const T& b);
00360     template <typename T> bool operator==(const Taylor<T>& a,
00361             const Taylor<T>& b);
00362     template <typename T> bool operator==(const T& a,
00363             const Taylor<T>& b);
00364     template <typename T> bool operator==(const Taylor<T>& a,
00365             const T& b);
00366     template <typename T> bool operator!=(const Taylor<T>& a,
00367             const Taylor<T>& b);
00368     template <typename T> bool operator!=(const T& a,
00369             const Taylor<T>& b);
00370     template <typename T> bool operator!=(const Taylor<T>& a,
00371             const T& b);
00372     template <typename T> bool operator<=(const Taylor<T>& a,
00373             const Taylor<T>& b);
00374     template <typename T> bool operator<=(const T& a,
00375             const Taylor<T>& b);
00376     template <typename T> bool operator<=(const Taylor<T>& a,
00377             const T& b);
00378     template <typename T> bool operator>=(const Taylor<T>& a,
00379             const Taylor<T>& b);
00380     template <typename T> bool operator>=(const T& a,
00381             const Taylor<T>& b);
00382     template <typename T> bool operator>=(const Taylor<T>& a,
00383             const T& b);
00384     template <typename T> bool operator<(const Taylor<T>& a,
00385            const Taylor<T>& b);
00386     template <typename T> bool operator<(const T& a,
00387            const Taylor<T>& b);
00388     template <typename T> bool operator<(const Taylor<T>& a,
00389            const T& b);
00390     template <typename T> bool operator>(const Taylor<T>& a,
00391            const Taylor<T>& b);
00392     template <typename T> bool operator>(const T& a,
00393            const Taylor<T>& b);
00394     template <typename T> bool operator>(const Taylor<T>& a,
00395            const T& b);
00396     template <typename T> std::ostream& operator << (std::ostream& os,
00397                  const Taylor<T>& a);
00398 
00399   } // namespace Tay
00400 
00401 } // namespace Sacado
00402 
00403 #include "Sacado_Tay_TaylorTraits.hpp"
00404 #include "Sacado_Tay_TaylorImp.hpp"
00405 
00406 #endif // SACADO_TAY_TAYLOR_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines