/////////////////////////////////////////////////////////////////////////////// // Copyright 2018 John Maddock. Distributed under the Boost // Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_MP_EIGEN_HPP #define BOOST_MP_EIGEN_HPP #include #include // // Generic Eigen support code: // namespace Eigen { template struct NumTraitsImp; template struct NumTraitsImp { using self_type = B1; using Real = typename boost::multiprecision::scalar_result_from_possible_complex::type; using NonInteger = self_type; // Not correct but we can't do much better?? using Literal = double; using Nested = self_type; enum { IsComplex = boost::multiprecision::number_category::value == boost::multiprecision::number_kind_complex, IsInteger = boost::multiprecision::number_category::value == boost::multiprecision::number_kind_integer, ReadCost = 1, AddCost = 4, MulCost = 8, IsSigned = std::numeric_limits::is_specialized ? std::numeric_limits::is_signed : true, RequireInitialization = 1, }; static Real epsilon() { static_assert(std::numeric_limits::is_specialized, "Eigen's NumTraits instantiated on a type with no numeric_limits support. Are you using a variable precision type?"); return std::numeric_limits::epsilon(); } static Real dummy_precision() { return 1000 * epsilon(); } static Real highest() { static_assert(std::numeric_limits::is_specialized, "Eigen's NumTraits instantiated on a type with no numeric_limits support. Are you using a variable precision type?"); return (std::numeric_limits::max)(); } static Real lowest() { static_assert(std::numeric_limits::is_specialized, "Eigen's NumTraits instantiated on a type with no numeric_limits support. Are you using a variable precision type?"); return (std::numeric_limits::min)(); } static int digits10_imp(const std::integral_constant&) { static_assert(std::numeric_limits::is_specialized, "Eigen's NumTraits instantiated on a type with no numeric_limits support. Are you using a variable precision type?"); return std::numeric_limits::digits10; } template static int digits10_imp(const std::integral_constant&) { return Real::thread_default_precision(); } static int digits10() { return digits10_imp(std::integral_constant < bool, std::numeric_limits::digits10 && (std::numeric_limits::digits10 != INT_MAX) ? true : false > ()); } static int digits() { // return the number of digits in the component type in case Real is complex // and we have no numeric_limits specialization. static_assert(std::numeric_limits::is_specialized, "Eigen's NumTraits instantiated on a type with no numeric_limits support. Are you using a variable precision type?"); return std::numeric_limits::digits; } static int min_exponent() { static_assert(std::numeric_limits::is_specialized, "Eigen's NumTraits instantiated on a type with no numeric_limits support. Are you using a variable precision type?"); return std::numeric_limits::min_exponent; } static int max_exponent() { static_assert(std::numeric_limits::is_specialized, "Eigen's NumTraits instantiated on a type with no numeric_limits support. Are you using a variable precision type?"); return std::numeric_limits::max_exponent; } static Real infinity() { static_assert(std::numeric_limits::is_specialized, "Eigen's NumTraits instantiated on a type with no numeric_limits support. Are you using a variable precision type?"); return std::numeric_limits::infinity(); } static Real quiet_NaN() { static_assert(std::numeric_limits::is_specialized, "Eigen's NumTraits instantiated on a type with no numeric_limits support. Are you using a variable precision type?"); return std::numeric_limits::quiet_NaN(); } }; template struct NumTraitsImp : public NumTraitsImp { // // This version is instantiated when B1 and B2 are different types, this happens for rational/complex/interval // types, in which case many methods defer to those of the "component type" B2. // using self_type = B1; using Real = typename boost::multiprecision::scalar_result_from_possible_complex::type; using NonInteger = self_type; // Not correct but we can't do much better?? using Literal = double; using Nested = self_type; enum { IsComplex = boost::multiprecision::number_category::value == boost::multiprecision::number_kind_complex, IsInteger = boost::multiprecision::number_category::value == boost::multiprecision::number_kind_integer, ReadCost = 1, AddCost = 4, MulCost = 8, IsSigned = std::numeric_limits::is_specialized ? std::numeric_limits::is_signed : true, RequireInitialization = 1, }; static B2 epsilon() { return NumTraitsImp::epsilon(); } static B2 dummy_precision() { return 1000 * epsilon(); } static B2 highest() { return NumTraitsImp::highest(); } static B2 lowest() { return NumTraitsImp::lowest(); } static int digits10() { return NumTraitsImp::digits10(); } static int digits() { return NumTraitsImp::digits(); } static int min_exponent() { return NumTraitsImp::min_exponent(); } static int max_exponent() { return NumTraitsImp::max_exponent(); } static B2 infinity() { return NumTraitsImp::infinity(); } static B2 quiet_NaN() { return NumTraitsImp::quiet_NaN(); } }; template struct NumTraits > : public NumTraitsImp, typename boost::multiprecision::number::value_type> {}; template struct NumTraits > : public NumTraits::result_type> {}; #define BOOST_MP_EIGEN_SCALAR_TRAITS_DECL(A) \ template \ struct ScalarBinaryOpTraits, A, BinaryOp> \ { \ /*static_assert(boost::multiprecision::is_compatible_arithmetic_type >::value, "Interoperability with this arithmetic type is not supported.");*/ \ using ReturnType = boost::multiprecision::number; \ }; \ template \ struct ScalarBinaryOpTraits, BinaryOp> \ { \ /*static_assert(boost::multiprecision::is_compatible_arithmetic_type >::value, "Interoperability with this arithmetic type is not supported.");*/ \ using ReturnType = boost::multiprecision::number; \ }; BOOST_MP_EIGEN_SCALAR_TRAITS_DECL(float) BOOST_MP_EIGEN_SCALAR_TRAITS_DECL(double) BOOST_MP_EIGEN_SCALAR_TRAITS_DECL(long double) BOOST_MP_EIGEN_SCALAR_TRAITS_DECL(char) BOOST_MP_EIGEN_SCALAR_TRAITS_DECL(unsigned char) BOOST_MP_EIGEN_SCALAR_TRAITS_DECL(signed char) BOOST_MP_EIGEN_SCALAR_TRAITS_DECL(short) BOOST_MP_EIGEN_SCALAR_TRAITS_DECL(unsigned short) BOOST_MP_EIGEN_SCALAR_TRAITS_DECL(int) BOOST_MP_EIGEN_SCALAR_TRAITS_DECL(unsigned int) BOOST_MP_EIGEN_SCALAR_TRAITS_DECL(long) BOOST_MP_EIGEN_SCALAR_TRAITS_DECL(unsigned long) #if 0 template struct ScalarBinaryOpTraits, boost::multiprecision::number, BinaryOp> { static_assert( boost::multiprecision::is_compatible_arithmetic_type, boost::multiprecision::number >::value || boost::multiprecision::is_compatible_arithmetic_type, boost::multiprecision::number >::value, "Interoperability with this arithmetic type is not supported."); using ReturnType = typename std::conditional, boost::multiprecision::number >::value, boost::multiprecision::number, boost::multiprecision::number >::type; }; template struct ScalarBinaryOpTraits, boost::multiprecision::et_on>, boost::multiprecision::mpfr_float, BinaryOp> { using ReturnType = boost::multiprecision::number, boost::multiprecision::et_on>; }; template struct ScalarBinaryOpTraits { using ReturnType = boost::multiprecision::number, boost::multiprecision::et_on>; }; template struct ScalarBinaryOpTraits, boost::multiprecision::number, BinaryOp> { using ReturnType = boost::multiprecision::number; }; #endif template struct ScalarBinaryOpTraits, boost::multiprecision::detail::expression, BinaryOp> { static_assert(std::is_convertible::result_type, boost::multiprecision::number >::value, "Interoperability with this arithmetic type is not supported."); using ReturnType = boost::multiprecision::number; }; template struct ScalarBinaryOpTraits, boost::multiprecision::number, BinaryOp> { static_assert(std::is_convertible::result_type, boost::multiprecision::number >::value, "Interoperability with this arithmetic type is not supported."); using ReturnType = boost::multiprecision::number; }; namespace numext { using boost::multiprecision::conj; } } // namespace Eigen #endif