add math::bernoulli_number

add type_traits is_int, is_uint
This commit is contained in:
bolero-MURAKAMI 2012-05-12 23:18:19 +09:00
parent 576744f408
commit 29b9d07975
6 changed files with 395 additions and 51 deletions

231
sprout/math/bernoulli.hpp Normal file
View file

@ -0,0 +1,231 @@
#ifndef SPROUT_MATH_BERNOULLI_HPP
#define SPROUT_MATH_BERNOULLI_HPP
#include <cstddef>
#include <stdexcept>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/array.hpp>
namespace sprout {
namespace math {
namespace detail {
template<typename T>
struct bernoulli_numbers;
template<>
struct bernoulli_numbers<float> {
public:
typedef float type;
public:
SPROUT_STATIC_CONSTEXPR std::size_t limit = 64;
public:
typedef sprout::array<type, limit / 2 + 1> table_type;
public:
SPROUT_STATIC_CONSTEXPR table_type table = table_type{{
1.F,
1.F / 6,
-1.F / 30,
1.F / 42,
-1.F / 30,
5.F / 66,
-691.F / 2730,
7.F / 6,
-3617.F / 510,
43867.F / 798,
-174611.F / 330,
854513.F / 138,
-236364091.F / 2730,
8553103.F / 6,
-236364091.F / 870,
8615841276005.F / 14322,
-7709321041217.F / 510,
2577687858367.F / 6,
-26315271553053477373.F / 1919190,
2929993913841559.F / 6,
-261082718496449122051.F / 13530,
1520097643918070802691.F / 1806,
-2.783326957930102423502e22F / 690,
5.96451111593912163278e23F / 282,
-1.20866265222965259346e23F,
7.500866746076964366856e24F,
-5.038778101481068914138e26F,
3.65287764848181233351e28F,
-2.849876930245088222627e30F,
2.386542749968362764465e32F,
-2.139994925722533366581e34F,
2.050097572347809756992e36F,
-2.09380059113463784091e38F
}};
};
SPROUT_CONSTEXPR typename sprout::math::detail::bernoulli_numbers<
float
>::table_type sprout::math::detail::bernoulli_numbers<
float
>::table;
template<>
struct bernoulli_numbers<double> {
public:
typedef double type;
public:
SPROUT_STATIC_CONSTEXPR std::size_t limit = 100;
public:
typedef sprout::array<type, limit / 2 + 1> table_type;
public:
SPROUT_STATIC_CONSTEXPR table_type table = table_type{{
1.,
1. / 6,
-1. / 30,
1. / 42,
-1. / 30,
5. / 66,
-691. / 2730,
7. / 6,
-3617. / 510,
43867. / 798,
-174611. / 330,
854513. / 138,
-236364091. / 2730,
8553103. / 6,
-236364091. / 870,
8615841276005. / 14322,
-7709321041217. / 510,
2577687858367. / 6,
-26315271553053477373. / 1919190,
2929993913841559. / 6,
-261082718496449122051. / 13530,
1520097643918070802691. / 1806,
-2.783326957930102423502e22 / 690,
5.96451111593912163278e23 / 282,
-1.20866265222965259346e23,
7.500866746076964366856e24,
-5.038778101481068914138e26,
3.65287764848181233351e28,
-2.849876930245088222627e30,
2.386542749968362764465e32,
-2.139994925722533366581e34,
2.050097572347809756992e36,
-2.09380059113463784091e38,
2.275269648846351555965e40,
-2.62577102862395760473e42,
3.21250821027180325182e44,
-4.159827816679471091392e46,
5.692069548203528002388e48,
-8.21836294197845756923e50,
1.250290432716699301673e53,
-2.001558323324837027493e55,
3.36749829153643742334e57,
-5.947097050313544771866e59,
1.101191032362797755956e62,
-2.135525954525350118866e64,
4.332889698664119241962e66,
-9.18855282416693282262e68 ,
2.034689677632907449346e71,
-4.70038339580357310786e73,
1.131804344548424927068e76,
-2.838224957069370695926e78
}};
};
SPROUT_CONSTEXPR typename sprout::math::detail::bernoulli_numbers<
double
>::table_type sprout::math::detail::bernoulli_numbers<
double
>::table;
template<>
struct bernoulli_numbers<long double> {
public:
typedef long double type;
public:
SPROUT_STATIC_CONSTEXPR std::size_t limit = 100;
public:
typedef sprout::array<type, limit / 2 + 1> table_type;
public:
SPROUT_STATIC_CONSTEXPR table_type table = table_type{{
1.L,
1.L / 6,
-1.L / 30,
1.L / 42,
-1.L / 30,
5.L / 66,
-691.L / 2730,
7.L / 6,
-3617.L / 510,
43867.L / 798,
-174611.L / 330,
854513.L / 138,
-236364091.L / 2730,
8553103.L / 6,
-236364091.L / 870,
8615841276005.L / 14322,
-7709321041217.L / 510,
2577687858367.L / 6,
-26315271553053477373.L / 1919190,
2929993913841559.L / 6,
-261082718496449122051.L / 13530,
1520097643918070802691.L / 1806,
-2.783326957930102423502e22L / 690,
5.96451111593912163278e23L / 282,
-1.20866265222965259346e23L,
7.500866746076964366856e24L,
-5.038778101481068914138e26L,
3.65287764848181233351e28L,
-2.849876930245088222627e30L,
2.386542749968362764465e32L,
-2.139994925722533366581e34L,
2.050097572347809756992e36L,
-2.09380059113463784091e38L,
2.275269648846351555965e40L,
-2.62577102862395760473e42L,
3.21250821027180325182e44L,
-4.159827816679471091392e46L,
5.692069548203528002388e48L,
-8.21836294197845756923e50L,
1.250290432716699301673e53L,
-2.001558323324837027493e55L,
3.36749829153643742334e57L,
-5.947097050313544771866e59L,
1.101191032362797755956e62L,
-2.135525954525350118866e64L,
4.332889698664119241962e66L,
-9.18855282416693282262e68L,
2.034689677632907449346e71L,
-4.70038339580357310786e73L,
1.131804344548424927068e76L,
-2.838224957069370695926e78L
}};
};
SPROUT_CONSTEXPR typename sprout::math::detail::bernoulli_numbers<
long double
>::table_type sprout::math::detail::bernoulli_numbers<
long double
>::table;
} // namespace detail
//
// bernoulli_number_limit
//
template<typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
SPROUT_CONSTEXPR std::size_t bernoulli_number_limit() {
typedef typename std::remove_cv<T>::type type;
return sprout::math::detail::bernoulli_numbers<type>::limit;
}
//
// bernoulli_number
//
template<typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
SPROUT_CONSTEXPR T bernoulli_number(std::size_t x) {
typedef typename std::remove_cv<T>::type type;
return x <= sprout::math::bernoulli_number_limit<type>()
? x == 1 ? type(-1) / 2
: x % 2 ? type(0)
: sprout::math::detail::bernoulli_numbers<type>::table[x / 2]
: throw std::invalid_argument("bernoulli_number(): argument limit exceeded")
;
}
} // namespace math
using sprout::math::bernoulli_number;
} // namespace sprout
#endif // #ifndef SPROUT_MATH_BERNOULLI_HPP

View file

@ -2,20 +2,23 @@
#define SPROUT_MATH_FACTORIAL_HPP
#include <cstddef>
#include <cstdint>
#include <stdexcept>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/array.hpp>
#include <sprout/type_traits/is_int.hpp>
#include <sprout/type_traits/is_uint.hpp>
namespace sprout {
namespace math {
namespace detail {
template<typename T>
template<typename T, typename Enable = void>
struct factorials;
template<>
struct factorials<std::int8_t> {
template<typename T>
struct factorials<T, typename std::enable_if<sprout::is_int<T>::value && sizeof(T) == 1>::type> {
public:
typedef std::int8_t type;
typedef T type;
public:
SPROUT_STATIC_CONSTEXPR std::size_t limit = 5;
public:
@ -30,11 +33,17 @@ namespace sprout {
120
}};
};
SPROUT_CONSTEXPR typename sprout::math::detail::factorials<std::int8_t>::table_type sprout::math::detail::factorials<std::int8_t>::table;
template<>
struct factorials<std::uint8_t> {
template<typename T>
SPROUT_CONSTEXPR typename sprout::math::detail::factorials<
T, typename std::enable_if<sprout::is_int<T>::value && sizeof(T) == 1>::type
>::table_type sprout::math::detail::factorials<
T, typename std::enable_if<sprout::is_int<T>::value && sizeof(T) == 1>::type
>::table;
template<typename T>
struct factorials<T, typename std::enable_if<sprout::is_uint<T>::value && sizeof(T) == 1>::type> {
public:
typedef std::uint8_t type;
typedef T type;
public:
SPROUT_STATIC_CONSTEXPR std::size_t limit = 5;
public:
@ -49,11 +58,17 @@ namespace sprout {
120
}};
};
SPROUT_CONSTEXPR typename sprout::math::detail::factorials<std::uint8_t>::table_type sprout::math::detail::factorials<std::uint8_t>::table;
template<>
struct factorials<std::int16_t> {
template<typename T>
SPROUT_CONSTEXPR typename sprout::math::detail::factorials<
T, typename std::enable_if<sprout::is_uint<T>::value && sizeof(T) == 1>::type
>::table_type sprout::math::detail::factorials<
T, typename std::enable_if<sprout::is_uint<T>::value && sizeof(T) == 1>::type
>::table;
template<typename T>
struct factorials<T, typename std::enable_if<sprout::is_int<T>::value && sizeof(T) == 2>::type> {
public:
typedef std::int16_t type;
typedef T type;
public:
SPROUT_STATIC_CONSTEXPR std::size_t limit = 7;
public:
@ -70,11 +85,17 @@ namespace sprout {
5040
}};
};
SPROUT_CONSTEXPR typename sprout::math::detail::factorials<std::int16_t>::table_type sprout::math::detail::factorials<std::int16_t>::table;
template<>
struct factorials<std::uint16_t> {
template<typename T>
SPROUT_CONSTEXPR typename sprout::math::detail::factorials<
T, typename std::enable_if<sprout::is_int<T>::value && sizeof(T) == 2>::type
>::table_type sprout::math::detail::factorials<
T, typename std::enable_if<sprout::is_int<T>::value && sizeof(T) == 2>::type
>::table;
template<typename T>
struct factorials<T, typename std::enable_if<sprout::is_uint<T>::value && sizeof(T) == 2>::type> {
public:
typedef std::uint16_t type;
typedef T type;
public:
SPROUT_STATIC_CONSTEXPR std::size_t limit = 8;
public:
@ -92,11 +113,17 @@ namespace sprout {
40320
}};
};
SPROUT_CONSTEXPR typename sprout::math::detail::factorials<std::uint16_t>::table_type sprout::math::detail::factorials<std::uint16_t>::table;
template<>
struct factorials<std::int32_t> {
template<typename T>
SPROUT_CONSTEXPR typename sprout::math::detail::factorials<
T, typename std::enable_if<sprout::is_uint<T>::value && sizeof(T) == 2>::type
>::table_type sprout::math::detail::factorials<
T, typename std::enable_if<sprout::is_uint<T>::value && sizeof(T) == 2>::type
>::table;
template<typename T>
struct factorials<T, typename std::enable_if<sprout::is_int<T>::value && sizeof(T) == 4>::type> {
public:
typedef std::int32_t type;
typedef T type;
public:
SPROUT_STATIC_CONSTEXPR std::size_t limit = 11;
public:
@ -117,11 +144,17 @@ namespace sprout {
39916800
}};
};
SPROUT_CONSTEXPR typename sprout::math::detail::factorials<std::int32_t>::table_type sprout::math::detail::factorials<std::int32_t>::table;
template<>
struct factorials<std::uint32_t> {
template<typename T>
SPROUT_CONSTEXPR typename sprout::math::detail::factorials<
T, typename std::enable_if<sprout::is_int<T>::value && sizeof(T) == 4>::type
>::table_type sprout::math::detail::factorials<
T, typename std::enable_if<sprout::is_int<T>::value && sizeof(T) == 4>::type
>::table;
template<typename T>
struct factorials<T, typename std::enable_if<sprout::is_uint<T>::value && sizeof(T) == 4>::type> {
public:
typedef std::uint32_t type;
typedef T type;
public:
SPROUT_STATIC_CONSTEXPR std::size_t limit = 11;
public:
@ -142,11 +175,17 @@ namespace sprout {
39916800
}};
};
SPROUT_CONSTEXPR typename sprout::math::detail::factorials<std::uint32_t>::table_type sprout::math::detail::factorials<std::uint32_t>::table;
template<>
struct factorials<std::int64_t> {
template<typename T>
SPROUT_CONSTEXPR typename sprout::math::detail::factorials<
T, typename std::enable_if<sprout::is_uint<T>::value && sizeof(T) == 4>::type
>::table_type sprout::math::detail::factorials<
T, typename std::enable_if<sprout::is_uint<T>::value && sizeof(T) == 4>::type
>::table;
template<typename T>
struct factorials<T, typename std::enable_if<sprout::is_int<T>::value && sizeof(T) == 8>::type> {
public:
typedef std::int64_t type;
typedef T type;
public:
SPROUT_STATIC_CONSTEXPR std::size_t limit = 20;
public:
@ -176,11 +215,17 @@ namespace sprout {
INT64_C(2432902008176640000)
}};
};
SPROUT_CONSTEXPR typename sprout::math::detail::factorials<std::int64_t>::table_type sprout::math::detail::factorials<std::int64_t>::table;
template<>
struct factorials<std::uint64_t> {
template<typename T>
SPROUT_CONSTEXPR typename sprout::math::detail::factorials<
T, typename std::enable_if<sprout::is_int<T>::value && sizeof(T) == 8>::type
>::table_type sprout::math::detail::factorials<
T, typename std::enable_if<sprout::is_int<T>::value && sizeof(T) == 8>::type
>::table;
template<typename T>
struct factorials<T, typename std::enable_if<sprout::is_uint<T>::value && sizeof(T) == 8>::type> {
public:
typedef std::uint64_t type;
typedef T type;
public:
SPROUT_STATIC_CONSTEXPR std::size_t limit = 20;
public:
@ -210,11 +255,17 @@ namespace sprout {
UINT64_C(2432902008176640000)
}};
};
SPROUT_CONSTEXPR typename sprout::math::detail::factorials<std::uint64_t>::table_type sprout::math::detail::factorials<std::uint64_t>::table;
template<>
struct factorials<float> {
template<typename T>
SPROUT_CONSTEXPR typename sprout::math::detail::factorials<
T, typename std::enable_if<sprout::is_uint<T>::value && sizeof(T) == 8>::type
>::table_type sprout::math::detail::factorials<
T, typename std::enable_if<sprout::is_uint<T>::value && sizeof(T) == 8>::type
>::table;
template<typename T>
struct factorials<T, typename std::enable_if<std::is_same<T, float>::value>::type> {
public:
typedef float type;
typedef T type;
public:
SPROUT_STATIC_CONSTEXPR std::size_t limit = 34;
public:
@ -258,11 +309,17 @@ namespace sprout {
0.29523279903960414084761860964352e39F
}};
};
SPROUT_CONSTEXPR typename sprout::math::detail::factorials<float>::table_type sprout::math::detail::factorials<float>::table;
template<>
struct factorials<double> {
template<typename T>
SPROUT_CONSTEXPR typename sprout::math::detail::factorials<
T, typename std::enable_if<std::is_same<T, float>::value>::type
>::table_type sprout::math::detail::factorials<
T, typename std::enable_if<std::is_same<T, float>::value>::type
>::table;
template<typename T>
struct factorials<T, typename std::enable_if<std::is_same<T, double>::value>::type> {
public:
typedef double type;
typedef T type;
public:
SPROUT_STATIC_CONSTEXPR std::size_t limit = 170;
public:
@ -442,11 +499,17 @@ namespace sprout {
0.7257415615307998967396728211129263114717e307
}};
};
SPROUT_CONSTEXPR typename sprout::math::detail::factorials<double>::table_type sprout::math::detail::factorials<double>::table;
template<>
struct factorials<long double> {
template<typename T>
SPROUT_CONSTEXPR typename sprout::math::detail::factorials<
T, typename std::enable_if<std::is_same<T, double>::value>::type
>::table_type sprout::math::detail::factorials<
T, typename std::enable_if<std::is_same<T, double>::value>::type
>::table;
template<typename T>
struct factorials<T, typename std::enable_if<std::is_same<T, long double>::value>::type> {
public:
typedef long double type;
typedef T type;
public:
SPROUT_STATIC_CONSTEXPR std::size_t limit = 170;
public:
@ -626,22 +689,29 @@ namespace sprout {
0.7257415615307998967396728211129263114717e307L
}};
};
SPROUT_CONSTEXPR typename sprout::math::detail::factorials<long double>::table_type sprout::math::detail::factorials<long double>::table;
template<typename T>
SPROUT_CONSTEXPR typename sprout::math::detail::factorials<
T, typename std::enable_if<std::is_same<T, long double>::value>::type
>::table_type sprout::math::detail::factorials<
T, typename std::enable_if<std::is_same<T, long double>::value>::type
>::table;
} // namespace detail
//
// factorial_limit
//
template<typename T>
template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value>::type>
SPROUT_CONSTEXPR std::size_t factorial_limit() {
return sprout::math::detail::factorials<T>::limit;
typedef typename std::remove_cv<T>::type type;
return sprout::math::detail::factorials<type>::limit;
}
//
// factorial
//
template<typename T>
template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value>::type>
SPROUT_CONSTEXPR T factorial(std::size_t x) {
return x <= sprout::math::factorial_limit<T>()
? sprout::math::detail::factorials<T>::table[x]
typedef typename std::remove_cv<T>::type type;
return x <= sprout::math::factorial_limit<type>()
? sprout::math::detail::factorials<type>::table[x]
: throw std::invalid_argument("factorial(): argument limit exceeded")
;
}

View file

@ -9,5 +9,6 @@
#include <sprout/math/power.hpp>
#include <sprout/math/operations.hpp>
#include <sprout/math/factorial.hpp>
#include <sprout/math/bernoulli.hpp>
#endif // #ifndef SPROUT_MATH_FUNCTIONS_HPP

View file

@ -2,6 +2,8 @@
#define SPROUT_TYPE_TRAITS_HPP
#include <sprout/config.hpp>
#include <sprout/type_traits/is_int.hpp>
#include <sprout/type_traits/is_uint.hpp>
#include <sprout/type_traits/lvalue_reference.hpp>
#include <sprout/type_traits/const_reference.hpp>
#include <sprout/type_traits/has_xxx.hpp>

View file

@ -0,0 +1,20 @@
#ifndef SPROUT_TYPE_TRAITS_IS_INT_HPP
#define SPROUT_TYPE_TRAITS_IS_INT_HPP
#include <type_traits>
#include <sprout/config.hpp>
namespace sprout {
//
// is_int
//
template<typename T>
struct is_int
: public std::integral_constant<
bool,
std::is_integral<T>::value && std::is_signed<T>::value
>
{};
} // namespace sprout
#endif // #ifndef SPROUT_TYPE_TRAITS_IS_INT_HPP

View file

@ -0,0 +1,20 @@
#ifndef SPROUT_TYPE_TRAITS_IS_UINT_HPP
#define SPROUT_TYPE_TRAITS_IS_UINT_HPP
#include <type_traits>
#include <sprout/config.hpp>
namespace sprout {
//
// is_uint
//
template<typename T>
struct is_uint
: public std::integral_constant<
bool,
std::is_integral<T>::value && std::is_unsigned<T>::value
>
{};
} // namespace sprout
#endif // #ifndef SPROUT_TYPE_TRAITS_IS_UINT_HPP