Sacado_PCE_OrthogPolyTraits.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_PCE_ORTHOGPOLYTRAITS_HPP
00033 #define SACADO_PCE_ORTHOGPOLYTRAITS_HPP
00034 
00035 #include "Sacado_Traits.hpp"
00036 
00037 // Forward declarations
00038 namespace Sacado {
00039   namespace PCE {
00040     template <typename T> class OrthogPoly;
00041   }
00042 }
00043 
00044 namespace Sacado {
00045 
00047   template <typename T>
00048   class Promote< PCE::OrthogPoly<T>, PCE::OrthogPoly<T> > {
00049   public:
00050 
00051     typedef PCE::OrthogPoly<T> type;
00052   };
00053 
00055   template <typename L, typename R>
00056   class Promote< PCE::OrthogPoly<L>, R > {
00057   public:
00058 
00059     typedef typename ValueType< PCE::OrthogPoly<L> >::type value_type_l;
00060     typedef typename ValueType<R>::type value_type_r;
00061     typedef typename Promote<value_type_l,value_type_r>::type value_type;
00062 
00063     typedef PCE::OrthogPoly<value_type> type;
00064   };
00065 
00067   template <typename L, typename R>
00068   class Promote< L, PCE::OrthogPoly<R> > {
00069   public:
00070 
00071     typedef typename ValueType<L>::type value_type_l;
00072     typedef typename ValueType< PCE::OrthogPoly<R> >::type value_type_r;
00073     typedef typename Promote<value_type_l,value_type_r>::type value_type;
00074 
00075     typedef PCE::OrthogPoly<value_type> type;
00076   };
00077 
00079   template <typename T>
00080   struct ScalarType< PCE::OrthogPoly<T> > {
00081     typedef typename ScalarType<typename PCE::OrthogPoly<T>::value_type>::type type;
00082   };
00083 
00085   template <typename T>
00086   struct ValueType< PCE::OrthogPoly<T> > {
00087     typedef typename PCE::OrthogPoly<T>::value_type type;
00088   };
00089 
00091   template <typename T>
00092   struct IsADType< PCE::OrthogPoly<T> > {
00093     static const bool value = true;
00094   };
00095 
00097   template <typename T>
00098   struct IsScalarType< PCE::OrthogPoly<T> > {
00099     static const bool value = false;
00100   };
00101 
00103   template <typename T>
00104   struct Value< PCE::OrthogPoly<T> > {
00105     typedef typename ValueType< PCE::OrthogPoly<T> >::type value_type;
00106     static const value_type& eval(const PCE::OrthogPoly<T>& x) { 
00107       return x.val(); }
00108   };
00109 
00111   template <typename T>
00112   struct ScalarValue< PCE::OrthogPoly<T> > {
00113     typedef typename ValueType< PCE::OrthogPoly<T> >::type value_type;
00114     typedef typename ScalarType< PCE::OrthogPoly<T> >::type scalar_type;
00115     static const scalar_type& eval(const PCE::OrthogPoly<T>& x) { 
00116       return ScalarValue<value_type>::eval(x.val()); }
00117   };
00118 
00120   template <typename T>
00121   struct StringName< PCE::OrthogPoly<T> > {
00122     static std::string eval() { 
00123       return std::string("Sacado::PCE::OrthogPoly< ") + 
00124   StringName<T>::eval() + " >"; }
00125   };
00126 
00127 } // namespace Sacado
00128 
00129 // Define Teuchos traits classes
00130 #ifdef HAVE_SACADO_TEUCHOS
00131 #include "Teuchos_PromotionTraits.hpp"
00132 #include "Teuchos_ScalarTraits.hpp"
00133 #include "Teuchos_TestForException.hpp"
00134 #include "Sacado_mpl_apply.hpp"
00135 
00136 namespace Teuchos {
00137 
00139   template <typename T>
00140   struct PromotionTraits< Sacado::PCE::OrthogPoly<T>, 
00141         Sacado::PCE::OrthogPoly<T> > {
00142     typedef typename Sacado::Promote< Sacado::PCE::OrthogPoly<T>,
00143               Sacado::PCE::OrthogPoly<T> >::type
00144     promote;
00145   };
00146 
00148   template <typename T, typename R>
00149   struct PromotionTraits< Sacado::PCE::OrthogPoly<T>, R > {
00150     typedef typename Sacado::Promote< Sacado::PCE::OrthogPoly<T>, R >::type 
00151     promote;
00152   };
00153 
00155   template <typename L, typename T>
00156   struct PromotionTraits< L, Sacado::PCE::OrthogPoly<T> > {
00157   public:
00158     typedef typename Sacado::Promote< L, Sacado::PCE::OrthogPoly<T> >::type 
00159     promote;
00160   };
00161 
00163   template <typename T>
00164   struct ScalarTraits< Sacado::PCE::OrthogPoly<T> > {
00165     typedef Sacado::PCE::OrthogPoly<T> ScalarType;
00166     typedef typename Sacado::ValueType<T>::type ValueT;
00167     
00168     typedef typename Sacado::mpl::apply<ScalarType,typename Teuchos::ScalarTraits<ValueT>::magnitudeType>::type magnitudeType;
00169     typedef typename Sacado::mpl::apply<ScalarType,typename Teuchos::ScalarTraits<ValueT>::halfPrecision>::type halfPrecision;
00170     typedef typename Sacado::mpl::apply<ScalarType,typename Teuchos::ScalarTraits<ValueT>::doublePrecision>::type doublePrecision;
00171     
00172     static const bool isComplex = Teuchos::ScalarTraits<ValueT>::isComplex;
00173     static const bool isOrdinal = Teuchos::ScalarTraits<ValueT>::isOrdinal;
00174     static const bool isComparable = 
00175       Teuchos::ScalarTraits<ValueT>::isComparable;
00176     static const bool hasMachineParameters = 
00177       Teuchos::ScalarTraits<ValueT>::hasMachineParameters;
00178     static typename Teuchos::ScalarTraits<ValueT>::magnitudeType eps() {
00179       return Teuchos::ScalarTraits<ValueT>::eps();
00180     }
00181     static typename Teuchos::ScalarTraits<ValueT>::magnitudeType sfmin() {
00182       return Teuchos::ScalarTraits<ValueT>::sfmin();
00183     }
00184     static typename Teuchos::ScalarTraits<ValueT>::magnitudeType base()  {
00185       return Teuchos::ScalarTraits<ValueT>::base();
00186     }
00187     static typename Teuchos::ScalarTraits<ValueT>::magnitudeType prec()  {
00188       return Teuchos::ScalarTraits<ValueT>::prec();
00189     }
00190     static typename Teuchos::ScalarTraits<ValueT>::magnitudeType t()     {
00191       return Teuchos::ScalarTraits<ValueT>::t();
00192     }
00193     static typename Teuchos::ScalarTraits<ValueT>::magnitudeType rnd()   {
00194       return Teuchos::ScalarTraits<ValueT>::rnd();
00195     }
00196     static typename Teuchos::ScalarTraits<ValueT>::magnitudeType emin()  {
00197       return Teuchos::ScalarTraits<ValueT>::emin();
00198     }
00199     static typename Teuchos::ScalarTraits<ValueT>::magnitudeType rmin()  {
00200       return Teuchos::ScalarTraits<ValueT>::rmin();
00201     }
00202     static typename Teuchos::ScalarTraits<ValueT>::magnitudeType emax()  {
00203       return Teuchos::ScalarTraits<ValueT>::emax();
00204     }
00205     static typename Teuchos::ScalarTraits<ValueT>::magnitudeType rmax()  {
00206       return Teuchos::ScalarTraits<ValueT>::rmax();
00207     }
00208     static magnitudeType magnitude(const ScalarType& a) {
00209 #ifdef TEUCHOS_DEBUG
00210       TEST_FOR_EXCEPTION(is_pce_real(a), std::runtime_error,
00211        "Complex conjugate is not defined for "
00212        "complex PCE inputs.");
00213 #endif
00214       //return std::fabs(a); 
00215       magnitudeType b(a.size());
00216       if (Teuchos::ScalarTraits<ValueT>::real(a.val()) >= 0)
00217   for (int i=0; i<a.size(); i++)
00218     b.fastAccessCoeff(i) = 
00219       Teuchos::ScalarTraits<ValueT>::magnitude(a.fastAccessCoeff(i));
00220       else
00221   for (int i=0; i<a.size(); i++)
00222     b.fastAccessCoeff(i) = 
00223       -Teuchos::ScalarTraits<ValueT>::magnitude(a.fastAccessCoeff(i));
00224       return b;
00225     }
00226     static ValueT zero()  { 
00227       return ValueT(0.0); 
00228     }
00229     static ValueT one()   { 
00230       return ValueT(1.0); 
00231     }
00232     
00233     // Conjugate is only defined for real derivative components
00234     static ScalarType conjugate(const ScalarType& x) {
00235 #ifdef TEUCHOS_DEBUG
00236       TEST_FOR_EXCEPTION(is_pce_real(x), std::runtime_error,
00237        "Complex conjugate is not defined for "
00238        "complex PCE inputs.");
00239 #endif
00240   return x;
00241     }   
00242     
00243     // Real part is only defined for real derivative components
00244     static ScalarType real(const ScalarType& x) { 
00245 #ifdef TEUCHOS_DEBUG
00246       TEST_FOR_EXCEPTION(is_pce_real(x) == false, std::runtime_error,
00247        "Real component is not defined for "
00248        "complex PCE inputs.");
00249 #endif
00250       return x;
00251     }
00252     
00253     // Imaginary part is only defined for real derivative components
00254     static ScalarType imag(const ScalarType& x) { 
00255 #ifdef TEUCHOS_DEBUG
00256       TEST_FOR_EXCEPTION(is_pce_real(x) == false, std::runtime_error,
00257        "Imaginary component is not defined for "
00258        "complex PCE inputs.");
00259 #endif
00260       return x;
00261     }
00262     
00263     static ValueT nan() {
00264       return Teuchos::ScalarTraits<ValueT>::nan(); 
00265     }
00266     static bool isnaninf(const ScalarType& x) { 
00267       for (int i=0; i<x.size(); i++)
00268   if (Teuchos::ScalarTraits<ValueT>::isnaninf(x.fastAccessCoeff(i)))
00269     return true;
00270       return false;
00271     }
00272     static void seedrandom(unsigned int s) { 
00273       Teuchos::ScalarTraits<ValueT>::seedrandom(s); 
00274     }
00275     static ValueT random() { 
00276       return Teuchos::ScalarTraits<ValueT>::random(); 
00277     }
00278     static std::string name() { 
00279       return Sacado::StringName<ScalarType>::eval(); 
00280     }
00281     static ScalarType squareroot(const ScalarType& x) {
00282       return std::sqrt(x); 
00283     }
00284     static ScalarType pow(const ScalarType& x, const ScalarType& y) { 
00285       return std::pow(x,y); 
00286     }
00287 
00288     // Helper function to determine whether a complex value is real
00289     static bool is_complex_real(const ValueT& x) {
00290       return 
00291   Teuchos::ScalarTraits<ValueT>::magnitude(x-Teuchos::ScalarTraits<ValueT>::real(x)) == 0;
00292     }
00293 
00294     // Helper function to determine whether a Fad type is real
00295     static bool is_pce_real(const ScalarType& x) {
00296       if (x.size() == 0)
00297   return true;
00298       if (Teuchos::ScalarTraits<ValueT>::isComplex) {
00299   for (int i=0; i<x.size(); i++)
00300     if (!is_complex_real(x.fastAccessCoeff(i)))
00301       return false;
00302       }
00303       return true;
00304     }
00305 
00306   }; // class ScalarTraits< Sacado::PCE::OrthogPoly<T> >
00307 }
00308 #endif // HAVE_SACADO_TEUCHOS
00309 
00310 #endif // SACADO_PCE_UNIVARIATEHERMITETRAITS_HPP

Generated on Wed May 12 21:39:35 2010 for Sacado Package Browser (Single Doxygen Collection) by  doxygen 1.4.7