7#ifndef XGBOOST_COMMON_MATH_H_
8#define XGBOOST_COMMON_MATH_H_
21template <
typename T>
XGBOOST_DEVICE T Sqr(T
const &w) {
return w * w; }
29 float constexpr kEps = 1e-16;
30 x = std::min(-x, 88.7f);
31 auto denom = expf(x) + 1.0f + kEps;
32 auto y = 1.0f / denom;
37 auto denom = std::exp(-x) + 1.0;
44template <
typename T,
typename U>
47 typename std::conditional<
48 std::is_floating_point<T>::value || std::is_floating_point<U>::value,
50 typename std::conditional<
51 std::is_signed<T>::value || std::is_signed<U>::value,
53 uint64_t>::type>::type;
54 return std::is_floating_point<Casted>::value ?
55 std::abs(
static_cast<Casted
>(a) -
static_cast<Casted
>(b)) < 1e-6 : a == b;
66template <
typename Iterator>
69 typename std::remove_reference<
70 decltype(std::declval<Iterator>().operator*())>::type
72 "Values should be of type bst_float");
74 for (Iterator i = start+1; i != end; ++i) {
75 wmax = fmaxf(*i, wmax);
78 for (Iterator i = start; i != end; ++i) {
82 for (Iterator i = start; i != end; ++i) {
83 *i /=
static_cast<float>(wsum);
94template<
typename Iterator>
96 Iterator maxit = begin;
97 for (Iterator it = begin; it != end; ++it) {
98 if (*it > *maxit) maxit = it;
111 return y + std::log(std::exp(x - y) + 1.0f);
113 return x + std::log(std::exp(y - x) + 1.0f);
124template<
typename Iterator>
125inline float LogSum(Iterator begin, Iterator end) {
127 for (Iterator it = begin; it != end; ++it) {
128 mx = std::max(mx, *it);
131 for (Iterator it = begin; it != end; ++it) {
132 sum += std::exp(*it - mx);
134 return mx + std::log(sum);
141 std::numeric_limits<T>::is_integer,
bool>::type
146#if XGBOOST_STRICT_R_MODE && !defined(__CUDA_ARCH__)
148bool CheckNAN(
double v);
153#if defined(__CUDA_ARCH__)
156 return std::isnan(x);
161#if defined(__CUDA_ARCH__)
164 return std::isnan(x);
171#if XGBOOST_STRICT_R_MODE && !defined(XGBOOST_USE_CUDA)
172double LogGamma(
double v);
183#pragma message("Warning: lgamma function was not available until VS2013"\
184 ", poisson regression will be disabled")
185 utils::Error(
"lgamma function was not available until VS2013");
186 return static_cast<T
>(1.0);
Copyright 2015-2023 by XGBoost Contributors.
#define XGBOOST_DEVICE
Tag function as usable by device.
Definition base.h:64
XGBOOST_DEVICE float Sigmoid(float x)
calculate the sigmoid of the input.
Definition math.h:28
XGBOOST_DEVICE void Softmax(Iterator start, Iterator end)
Do inplace softmax transformaton on start to end.
Definition math.h:67
XGBOOST_DEVICE constexpr bool CloseTo(T a, U b)
Equality test for both integer and floating point.
Definition math.h:45
float LogSum(float x, float y)
perform numerically safe logsum
Definition math.h:109
XGBOOST_DEVICE Iterator FindMaxIndex(Iterator begin, Iterator end)
Find the maximum iterator within the iterators.
Definition math.h:95
namespace of xgboost
Definition base.h:90
float bst_float
float type, used for storing statistics
Definition base.h:97