vSMC
vSMC: Scalable Monte Carlo
cmath.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/cxx11/cmath.hpp
3 //----------------------------------------------------------------------------
4 // vSMC: Scalable Monte Carlo
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013,2014, Yan Zhou
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //============================================================================
31 
32 #ifndef VSMC_CXX11_CMATH_HPP
33 #define VSMC_CXX11_CMATH_HPP
34 
35 #include <vsmc/internal/config.hpp>
36 #include <cmath>
37 
38 #if VSMC_HAS_CXX11LIB_CMATH
39 
40 namespace vsmc { namespace cxx11 {
41 
42 using std::exp2;
43 using std::log2;
44 using std::expm1;
45 using std::log1p;
46 using std::cbrt;
47 using std::hypot;
48 using std::asinh;
49 using std::acosh;
50 using std::atanh;
51 using std::erf;
52 using std::erfc;
53 using std::lgamma;
54 using std::tgamma;
55 
56 } } // namespace vsmc::cxx11
57 
58 #elif VSMC_HAS_C99LIB_MATH
59 
60 #define VSMC_DEFINE_C99_MATH_SPECIAL(name) \
61 inline float (name) (float x) {return ::name##f(x);} \
62 inline double (name) (double x) {return ::name(x);} \
63 inline long double (name) (long double x) {return ::name##l(x);} \
64 template <typename T> inline double (name) (T x) \
65 {return ::name(static_cast<double>(x));}
66 
67 namespace vsmc { namespace cxx11 {
68 
69 inline float (hypot) (float x, float y) {return ::hypotf(x, y);}
70 inline double (hypot) (float x, double y) {return ::hypot (x, y);}
71 inline long double (hypot) (float x, long double y) {return ::hypotl(x, y);}
72 inline double (hypot) (double x, float y) {return ::hypot (x, y);}
73 inline double (hypot) (double x, double y) {return ::hypot (x, y);}
74 inline long double (hypot) (double x, long double y) {return ::hypotl(x, y);}
75 inline long double (hypot) (long double x, float y) {return ::hypotl(x, y);}
76 inline long double (hypot) (long double x, double y) {return ::hypotl(x, y);}
77 inline long double (hypot) (long double x, long double y) {return ::hypotl(x, y);}
78 template <typename T1, typename T2> inline double (hypot) (T1 x, T2 y)
79 {return ::hypot(static_cast<double>(x), static_cast<double>(y));}
80 
81 VSMC_DEFINE_C99_MATH_SPECIAL(exp2)
82 VSMC_DEFINE_C99_MATH_SPECIAL(log2)
83 VSMC_DEFINE_C99_MATH_SPECIAL(expm1)
84 VSMC_DEFINE_C99_MATH_SPECIAL(log1p)
85 VSMC_DEFINE_C99_MATH_SPECIAL(cbrt)
86 VSMC_DEFINE_C99_MATH_SPECIAL(asinh)
87 VSMC_DEFINE_C99_MATH_SPECIAL(acosh)
88 VSMC_DEFINE_C99_MATH_SPECIAL(atanh)
89 VSMC_DEFINE_C99_MATH_SPECIAL(erf)
90 VSMC_DEFINE_C99_MATH_SPECIAL(erfc)
91 VSMC_DEFINE_C99_MATH_SPECIAL(lgamma)
92 VSMC_DEFINE_C99_MATH_SPECIAL(tgamma)
93 
94 } } // namespace vsmc::cxx11
95 
96 #else // VSMC_HAS_CXX11LIB_CMATH
97 
98 #include <vsmc/math/constants.hpp>
99 #include <boost/math/special_functions.hpp>
100 
101 namespace vsmc { namespace cxx11 {
102 
103 inline float (exp2) (float x)
104 {using std::exp; return exp(x * math::ln_2<float>());}
105 inline double (exp2) (double x)
106 {using std::exp; return exp(x * math::ln_2<double>());}
107 inline long double (exp2) (long double x)
108 {using std::exp; return exp(x * math::ln_2<long double>());}
109 template <typename T> inline double (exp2) (T x)
110 {using std::exp; return exp(static_cast<double>(x) * math::ln_2<double>());}
111 
112 inline float (log2) (float x)
113 {using std::log; return log(x) * math::ln_inv_2<float>();}
114 inline double (log2) (double x)
115 {using std::log; return log(x) * math::ln_inv_2<double>();}
116 inline long double (log2) (long double x)
117 {using std::log; return log(x) * math::ln_inv_2<long double>();}
118 template <typename T> inline double (log2) (T x)
119 {using std::log; return log(static_cast<double>(x)) * math::ln_inv_2<double>();}
120 
121 using ::boost::math::expm1;
122 using ::boost::math::log1p;
123 using ::boost::math::cbrt;
124 using ::boost::math::hypot;
125 using ::boost::math::asinh;
126 using ::boost::math::acosh;
127 using ::boost::math::atanh;
128 using ::boost::math::erf;
129 using ::boost::math::erfc;
130 using ::boost::math::lgamma;
131 using ::boost::math::tgamma;
132 
133 } } // namespace vsmc::cxx11
134 
135 #endif // VSMC_HAS_CXX11LIB_CMATH
136 
137 #endif // VSMC_CXX11_CMATH_HPP
Definition: adapter.hpp:37
long double ln_2< long double >()
Definition: constants.hpp:258
double ln_2< double >()
Definition: constants.hpp:258
double ln_inv_2< double >()
Definition: constants.hpp:278
float ln_2< float >()
Definition: constants.hpp:258
float ln_inv_2< float >()
Definition: constants.hpp:278
long double ln_inv_2< long double >()
Definition: constants.hpp:278