// @HEADER // ***************************************************************************** // Sacado Package // // Copyright 2006 NTESS and the Sacado contributors. // SPDX-License-Identifier: LGPL-2.1-or-later // // *********************************************************************** // // The forward-mode AD classes in Sacado are a derivative work of the // expression template classes in the Fad package by Nicolas Di Cesare. // The following banner is included in the original Fad source code: // // ************ DO NOT REMOVE THIS BANNER **************** // // Nicolas Di Cesare // http://www.ann.jussieu.fr/~dicesare // // CEMRACS 98 : C++ courses, // templates : new C++ techniques // for scientific computing // //******************************************************** // // NumericalTraits class to illustrate TRAITS // //******************************************************** // @HEADER #ifndef SACADO_TRAITS_HPP #define SACADO_TRAITS_HPP #include #include #include "Sacado_ConfigDefs.h" #include "Sacado_dummy_arg.hpp" #include "Sacado_mpl_enable_if.hpp" #include "Sacado_mpl_disable_if.hpp" #ifdef HAVE_SACADO_COMPLEX #include #endif namespace Sacado { /*! * \brief Enum use to signal whether the derivative array should be * initialized in AD object constructors. */ enum DerivInit { NoInitDerivArray = 0, //!< Do not initialize the derivative array InitDerivArray //!< Initialize the derivative array }; //! Is a type an expression template struct IsExpr { static const bool value = false; }; //! Determine whether a given type is a view template struct IsView { static const bool value = false; }; //! Get the base Fad type from a view/expression template struct BaseExprType { typedef T type; }; //! Get view type for any Fad type template struct ViewFadType {}; //! Specialize this for a given type T to disable default Promote rules template struct OverrideDefaultPromote { static const bool value = false; }; //! Base template specification for %Promote /*! * The %Promote classes provide a mechanism for computing the * promoted type of a binary operation. */ template struct Promote {}; //! Specialization of %Promote for a single type template struct Promote< A, A, typename mpl::enable_if_c< !OverrideDefaultPromote::value >::type > { typedef typename BaseExprType::type type; }; //! Specialization of %Promote when A is convertible to B but not vice-versa template struct Promote< A, B, typename mpl::enable_if_c< std::is_convertible::value && !std::is_convertible::value && !OverrideDefaultPromote::value && !OverrideDefaultPromote::value >::type > { typedef typename BaseExprType::type type; }; //! Specialization of %Promote when B is convertible to A but not vice-versa template struct Promote< A, B, typename mpl::enable_if_c< std::is_convertible::value && !std::is_convertible::value && !OverrideDefaultPromote::value && !OverrideDefaultPromote::value >::type > { typedef typename BaseExprType::type type; }; /*! * \brief Specialization of Promote when A and B are convertible to each * other, and one of them is an expression. */ template struct Promote< A, B, typename mpl::enable_if_c< std::is_convertible::value && std::is_convertible::value && !std::is_same::value && ( IsExpr::value || IsExpr::value ) >::type > { typedef typename BaseExprType::type A_base_fad_type; typedef typename BaseExprType::type B_base_fad_type; typedef typename Promote< A_base_fad_type, B_base_fad_type >::type type; }; /*! * \brief Specialization of Promote when A is an expression and B is * convertible to its value-type, e.g., Promote< fad-expression, double > * (using BaseExprType to remove ViewFad) */ template struct Promote< A, B, typename mpl::enable_if_c< !std::is_convertible::value && !std::is_convertible::value && IsExpr::value && std::is_convertible< B, typename BaseExprType< typename A::value_type >::type >::value >::type > { typedef typename BaseExprType::type type; }; /*! * \brief Specialization of Promote when B is an expression and A is * convertible to its value-type, e.g., Promote< double, fad-expression > * (using BaseExprType to remove ViewFad) */ template struct Promote< A, B, typename mpl::enable_if_c< !std::is_convertible::value && !std::is_convertible::value && IsExpr::value && std::is_convertible< A, typename BaseExprType< typename B::value_type >::type >::value >::type > { typedef typename BaseExprType::type type; }; /*! * \brief Specialization of Promote when A and B are (different) expressions, * with the same value type, e.g, Promote< fad-expr1, fad-expr2 > * (using BaseExprType to remove ViewFad) */ template struct Promote< A, B, typename mpl::enable_if_c< !std::is_convertible::value && !std::is_convertible::value && IsExpr::value && IsExpr::value && std::is_same< typename BaseExprType< typename A::value_type >::type, typename BaseExprType< typename B::value_type >::type >::value >::type > { typedef typename BaseExprType::type A_base_expr_type; typedef typename BaseExprType::type B_base_expr_type; typedef typename Promote< A_base_expr_type, B_base_expr_type >::type type; }; //! Specialization of %Promote to builtin types #define SACADO_PROMOTE_SPECIALIZATION(type1,type2,type3) \ template <> struct Promote< type1, type2, void > { \ typedef type3 type; \ }; \ template <> struct Promote< type2, type1, void > { \ typedef type3 type; \ }; SACADO_PROMOTE_SPECIALIZATION(double,float,double) SACADO_PROMOTE_SPECIALIZATION(double,long,double) SACADO_PROMOTE_SPECIALIZATION(double,int,double) SACADO_PROMOTE_SPECIALIZATION(float,long,float) SACADO_PROMOTE_SPECIALIZATION(float,int,float) #ifdef HAVE_SACADO_COMPLEX SACADO_PROMOTE_SPECIALIZATION(std::complex,std::complex,std::complex) SACADO_PROMOTE_SPECIALIZATION(std::complex,double,std::complex) SACADO_PROMOTE_SPECIALIZATION(std::complex,float,std::complex) SACADO_PROMOTE_SPECIALIZATION(std::complex,long,std::complex) SACADO_PROMOTE_SPECIALIZATION(std::complex,int,std::complex) SACADO_PROMOTE_SPECIALIZATION(std::complex,float,std::complex) SACADO_PROMOTE_SPECIALIZATION(std::complex,long,std::complex) SACADO_PROMOTE_SPECIALIZATION(std::complex,int,std::complex) #endif // HAVE_SACADO_COMPLEX #undef SACADO_PROMOTE_SPECIALIZATION // Macros for building proper Promote specialization for AD types #define SACADO_AD_PROMOTE_SPEC(NS, AD) /* */ #define SACADO_AD_PROMOTE_SPEC2(NS, AD) /* */ #define SACADO_FAD_PROMOTE_SPEC(NS, FAD) /* */ #define SACADO_SFAD_PROMOTE_SPEC(NS, FAD) /* */ #define SACADO_EXPR_PROMOTE_SPEC(NS) /* */ #define SACADO_VFAD_PROMOTE_SPEC(NS) /* */ #define SACADO_RAD_PROMOTE_SPEC(NS) \ namespace NS { \ template class ADvar; \ template class ADvari; \ } \ template \ struct OverrideDefaultPromote< NS :: ADvari & > { \ static const bool value = true; \ }; \ template \ struct Promote< NS :: ADvar , \ NS :: ADvari & > { \ typedef NS :: ADvar type; \ }; \ template \ struct Promote< NS :: ADvari &, \ NS :: ADvar > { \ typedef NS :: ADvar type; \ }; \ template \ struct Promote< NS :: ADvari &, \ typename NS :: ADvari ::value_type > { \ typedef NS :: ADvar type; \ }; \ template \ struct Promote< typename NS :: ADvari ::value_type, \ NS :: ADvari & > { \ typedef NS :: ADvar type; \ }; \ template \ struct Promote< NS :: ADvari &, \ typename dummy< typename NS :: ADvari ::value_type, \ typename NS :: ADvari ::scalar_type \ >::type > { \ typedef NS :: ADvar type; \ }; \ template \ struct Promote< typename dummy< typename NS :: ADvari ::value_type, \ typename NS :: ADvari ::scalar_type \ >::type, \ NS :: ADvari & > { \ typedef NS :: ADvar type; \ }; // // We define defaults for all of the traits to make Sacado easier to use. // The default choices are based on what appears to be the "safest" choice // for any scalar type. They may not work in all cases, in which case a // specialization should be provided. // //! Base template specification for %ScalarType /*! * The %ScalarType classes provide a mechanism for computing the * base underlying type of nested AD classes */ template struct ScalarType { typedef T type; }; //! Specialization of %ScalarType for const types /*! * This should work for most types */ template struct ScalarType { typedef const typename ScalarType::type type; }; //! Base template specification for %ValueType /*! * The %ValueType classes provide a mechanism for computing the * the type stored in AD classes */ template struct ValueType { typedef T type; }; //! Specialization of %ValueType for const types /*! * This should work for most types */ template struct ValueType { typedef const typename ValueType::type type; }; //! Base template specification for %IsADType /*! * The %IsADType classes provide a mechanism for * determining whether a type is an AD type */ template struct IsADType { static const bool value = false; }; //! Base template specification for %IsScalarType /*! * The %IsScalarType classes provide a mechanism for * determining whether a type is a scalar type (float, double, etc...) */ template struct IsScalarType { static const bool value = false; }; //! Base template specification for %IsSimdType /*! * The %IsSimdType classes provide a mechanism for computing the * determining whether a type is a Simd type (Doubles, MP::Vector, ...) */ template struct IsSimdType { static const bool value = false; }; //! Base template specification for %Value /*! * The %Value functor returns the value of an AD type. */ template struct Value { SACADO_INLINE_FUNCTION static const T& eval(const T& x) { return x; } }; //! Specialization of Value for const types template struct Value { typedef typename ValueType::type value_type; SACADO_INLINE_FUNCTION static const value_type& eval(const T& x) { return Value::eval(x); } }; //! Base template specification for %ScalarValue /*! * The %ScalarValue functor returns the base scalar value of an AD type, * i.e., something that isn't an AD type. */ template struct ScalarValue { SACADO_INLINE_FUNCTION static const T& eval(const T& x) { return x; } }; //! Specialization of ScalarValue for const types template struct ScalarValue { typedef typename ScalarType::type scalar_type; SACADO_INLINE_FUNCTION static const scalar_type& eval(const T& x) { return ScalarValue::eval(x); } }; //! A simple template function for invoking ScalarValue<> template SACADO_INLINE_FUNCTION typename ScalarType::type scalarValue(const T& x) { return ScalarValue::eval(x); } //! Base template specification for marking constants template struct MarkConstant { SACADO_INLINE_FUNCTION static void eval(T& x) {} }; //! Base template specification for string names of types template struct StringName { static std::string eval() { return ""; } }; //! Base template specification for testing equivalence template struct IsEqual { SACADO_INLINE_FUNCTION static bool eval(const T& x, const T& y) { return x == y; } }; //! Base template specification for testing whether type is statically sized template struct IsStaticallySized { static const bool value = false; }; //! Specialization of %IsStaticallySized for const types /*! * This should work for most types */ template struct IsStaticallySized { static const bool value = IsStaticallySized::value; }; //! Base template specification for static size template struct StaticSize { static const unsigned value = 0; }; //! Base template specification for whether a type is a Fad type template struct IsFad { static const bool value = false; }; //! Base template specification for whether a type is a Fad type template struct IsFad< const T > { static const bool value = IsFad::value; }; //! Remove const from a type template struct RemoveConst { typedef T type; }; //! Remove const from a type template struct RemoveConst< const T > { typedef T type; }; //! Specialization of above classes to builtin types #define SACADO_BUILTIN_SPECIALIZATION(t,NAME) \ template <> struct ScalarType< t > { \ typedef t type; \ }; \ template <> struct ValueType< t > { \ typedef t type; \ }; \ template <> struct IsADType< t > { \ static const bool value = false; \ }; \ template <> struct IsScalarType< t > { \ static const bool value = true; \ }; \ template <> struct Value< t > { \ SACADO_INLINE_FUNCTION \ static const t& eval(const t& x) { return x; } \ }; \ template <> struct ScalarValue< t > { \ SACADO_INLINE_FUNCTION \ static const t& eval(const t& x) { return x; } \ }; \ template <> struct StringName< t > { \ static std::string eval() { return NAME; } \ }; \ template <> struct IsEqual< t > { \ SACADO_INLINE_FUNCTION \ static bool eval(const t& x, const t& y) { \ return x == y; } \ }; \ template <> struct IsStaticallySized< t > { \ static const bool value = true; \ }; #define SACADO_BUILTIN_SPECIALIZATION_COMPLEX(t,NAME) \ template <> struct ScalarType< t > { \ typedef t type; \ }; \ template <> struct ValueType< t > { \ typedef t type; \ }; \ template <> struct IsADType< t > { \ static const bool value = false; \ }; \ template <> struct IsScalarType< t > { \ static const bool value = true; \ }; \ template <> struct Value< t > { \ static const t& eval(const t& x) { return x; } \ }; \ template <> struct ScalarValue< t > { \ static const t& eval(const t& x) { return x; } \ }; \ template <> struct StringName< t > { \ static std::string eval() { return NAME; } \ }; \ template <> struct IsEqual< t > { \ static bool eval(const t& x, const t& y) { \ return x == y; } \ }; \ template <> struct IsStaticallySized< t > { \ static const bool value = true; \ }; SACADO_BUILTIN_SPECIALIZATION(char,"char") SACADO_BUILTIN_SPECIALIZATION(float,"float") SACADO_BUILTIN_SPECIALIZATION(double,"double") SACADO_BUILTIN_SPECIALIZATION(int,"int") SACADO_BUILTIN_SPECIALIZATION(unsigned int,"unsigned int") SACADO_BUILTIN_SPECIALIZATION(long,"long") SACADO_BUILTIN_SPECIALIZATION(unsigned long,"unsigned long") SACADO_BUILTIN_SPECIALIZATION(bool,"bool") #ifdef HAVE_SACADO_COMPLEX SACADO_BUILTIN_SPECIALIZATION_COMPLEX(std::complex,"std::complex") SACADO_BUILTIN_SPECIALIZATION_COMPLEX(std::complex,"std::complex") #endif #undef SACADO_BUILTIN_SPECIALIZATION #undef SACADO_BUILTIN_SPECIALIZATION_COMPLEX template< typename T , T v , bool NonZero = ( v != T(0) ) > struct integral_nonzero { // Declaration of 'static const' causes an unresolved linker symbol in debug // static const T value = v ; enum { value = T(v) }; typedef T value_type ; typedef integral_nonzero type ; SACADO_INLINE_FUNCTION integral_nonzero() {} SACADO_INLINE_FUNCTION integral_nonzero( const T & ) {} SACADO_INLINE_FUNCTION integral_nonzero( const integral_nonzero & ) {} SACADO_INLINE_FUNCTION integral_nonzero& operator=(const integral_nonzero &) {return *this;} SACADO_INLINE_FUNCTION integral_nonzero& operator=(const T &) {return *this;} }; template< typename T , T zero > struct integral_nonzero { T value ; typedef T value_type ; typedef integral_nonzero type ; SACADO_INLINE_FUNCTION integral_nonzero() : value() {} SACADO_INLINE_FUNCTION integral_nonzero( const T & v ) : value(v) {} SACADO_INLINE_FUNCTION integral_nonzero( const integral_nonzero & v) : value(v.value) {} SACADO_INLINE_FUNCTION integral_nonzero& operator=(const integral_nonzero & v) { value = v.value; return *this; } SACADO_INLINE_FUNCTION integral_nonzero& operator=(const T & v) { value = v; return *this; } }; } // namespace Sacado #endif // SACADO_TRAITS_HPP