7 #ifndef XGBOOST_COMMON_MATH_H_ 8 #define XGBOOST_COMMON_MATH_H_ 26 return 1.0f / (1.0f + expf(-x));
32 template <
typename T,
typename U>
35 typename std::conditional<
36 std::is_floating_point<T>::value || std::is_floating_point<U>::value,
38 typename std::conditional<
39 std::is_signed<T>::value || std::is_signed<U>::value,
41 uint64_t>::type>::type;
42 return std::is_floating_point<Casted>::value ?
43 std::abs(static_cast<Casted>(a) -static_cast<Casted>(b)) < 1e-6 : a == b;
54 template <
typename Iterator>
57 typename std::remove_reference<
58 decltype(std::declval<Iterator>().
operator*())>::type
60 "Values should be of type bst_float");
62 for (Iterator i = start+1; i != end; ++i) {
63 wmax = fmaxf(*i, wmax);
66 for (Iterator i = start; i != end; ++i) {
70 for (Iterator i = start; i != end; ++i) {
71 *i /=
static_cast<float>(wsum);
82 template<
typename Iterator>
84 Iterator maxit = begin;
85 for (Iterator it = begin; it != end; ++it) {
86 if (*it > *maxit) maxit = it;
97 inline float LogSum(
float x,
float y) {
99 return y + std::log(std::exp(x - y) + 1.0f);
101 return x + std::log(std::exp(y - x) + 1.0f);
112 template<
typename Iterator>
113 inline float LogSum(Iterator begin, Iterator end) {
115 for (Iterator it = begin; it != end; ++it) {
116 mx = std::max(mx, *it);
119 for (Iterator it = begin; it != end; ++it) {
120 sum += std::exp(*it - mx);
122 return mx + std::log(sum);
126 inline static bool CmpFirst(
const std::pair<float, unsigned> &a,
127 const std::pair<float, unsigned> &b) {
128 return a.first > b.first;
130 inline static bool CmpSecond(
const std::pair<float, unsigned> &a,
131 const std::pair<float, unsigned> &b) {
132 return a.second > b.second;
137 template <
typename T>
139 std::numeric_limits<T>::is_integer,
bool>::type
144 #if XGBOOST_STRICT_R_MODE && !defined(__CUDA_ARCH__) 151 #if defined(__CUDA_ARCH__) 154 return std::isnan(x);
155 #endif // defined(__CUDA_ARCH__) 159 #if defined(__CUDA_ARCH__) 162 return std::isnan(x);
163 #endif // defined(__CUDA_ARCH__) 166 #endif // XGBOOST_STRICT_R_MODE && !defined(__CUDA_ARCH__) 169 #if XGBOOST_STRICT_R_MODE && !defined(XGBOOST_USE_CUDA) 172 #else // Not R or R with GPU. 181 #pragma message("Warning: lgamma function was not available until VS2013"\ 182 ", poisson regression will be disabled") 183 utils::Error(
"lgamma function was not available until VS2013");
184 return static_cast<T
>(1.0);
185 #endif // _MSC_VER >= 1800 192 #endif // XGBOOST_STRICT_R_MODE && !defined(XGBOOST_USE_CUDA) 196 #endif // XGBOOST_COMMON_MATH_H_ float bst_float
float type, used for storing statistics
Definition: base.h:111
float LogSum(float x, float y)
perform numerically safe logsum
Definition: math.h:97
XGBOOST_DEVICE void Softmax(Iterator start, Iterator end)
Do inplace softmax transformaton on start to end.
Definition: math.h:55
XGBOOST_DEVICE T LogGamma(T v)
Definition: math.h:175
XGBOOST_DEVICE constexpr bool CloseTo(T a, U b)
Equality test for both integer and floating point.
Definition: math.h:33
XGBOOST_DEVICE Iterator FindMaxIndex(Iterator begin, Iterator end)
Find the maximum iterator within the iterators.
Definition: math.h:83
#define XGBOOST_DEVICE
Tag function as usable by device.
Definition: base.h:84
namespace of xgboost
Definition: base.h:102
defines configuration macros of xgboost.
XGBOOST_DEVICE float Sigmoid(float x)
calculate the sigmoid of the input.
Definition: math.h:25
XGBOOST_DEVICE std::enable_if< std::numeric_limits< T >::is_integer, bool >::type CheckNAN(T)
Definition: math.h:140