7 #ifndef XGBOOST_COMMON_MATH_H_
8 #define XGBOOST_COMMON_MATH_H_
26 return 1.0f / (1.0f + expf(-x));
35 template <
typename T,
typename U>
38 typename std::conditional<
39 std::is_floating_point<T>::value || std::is_floating_point<U>::value,
41 typename std::conditional<
42 std::is_signed<T>::value || std::is_signed<U>::value,
44 uint64_t>::type>::type;
45 return std::is_floating_point<Casted>::value ?
46 std::abs(
static_cast<Casted
>(a) -
static_cast<Casted
>(b)) < 1e-6 : a == b;
57 template <
typename Iterator>
60 typename std::remove_reference<
61 decltype(std::declval<Iterator>().
operator*())>::type
63 "Values should be of type bst_float");
65 for (Iterator i = start+1; i != end; ++i) {
66 wmax = fmaxf(*i, wmax);
69 for (Iterator i = start; i != end; ++i) {
73 for (Iterator i = start; i != end; ++i) {
74 *i /=
static_cast<float>(wsum);
85 template<
typename Iterator>
87 Iterator maxit = begin;
88 for (Iterator it = begin; it != end; ++it) {
89 if (*it > *maxit) maxit = it;
102 return y + std::log(std::exp(x - y) + 1.0f);
104 return x + std::log(std::exp(y - x) + 1.0f);
115 template<
typename Iterator>
116 inline float LogSum(Iterator begin, Iterator end) {
118 for (Iterator it = begin; it != end; ++it) {
119 mx = std::max(mx, *it);
122 for (Iterator it = begin; it != end; ++it) {
123 sum += std::exp(*it - mx);
125 return mx + std::log(sum);
129 inline static bool CmpFirst(
const std::pair<float, unsigned> &a,
130 const std::pair<float, unsigned> &b) {
131 return a.first > b.first;
133 inline static bool CmpSecond(
const std::pair<float, unsigned> &a,
134 const std::pair<float, unsigned> &b) {
135 return a.second > b.second;
140 template <
typename T>
142 std::numeric_limits<T>::is_integer,
bool>::type
147 #if XGBOOST_STRICT_R_MODE && !defined(__CUDA_ARCH__)
154 #if defined(__CUDA_ARCH__)
157 return std::isnan(x);
158 #endif // defined(__CUDA_ARCH__)
162 #if defined(__CUDA_ARCH__)
165 return std::isnan(x);
166 #endif // defined(__CUDA_ARCH__)
169 #endif // XGBOOST_STRICT_R_MODE && !defined(__CUDA_ARCH__)
172 #if XGBOOST_STRICT_R_MODE && !defined(XGBOOST_USE_CUDA)
175 #else // Not R or R with GPU.
184 #pragma message("Warning: lgamma function was not available until VS2013"\
185 ", poisson regression will be disabled")
186 utils::Error(
"lgamma function was not available until VS2013");
187 return static_cast<T
>(1.0);
188 #endif // _MSC_VER >= 1800
195 #endif // XGBOOST_STRICT_R_MODE && !defined(XGBOOST_USE_CUDA)
199 #endif // XGBOOST_COMMON_MATH_H_