1 #ifndef __STAN__MATH__SPECIAL_FUNCTIONS_HPP__
2 #define __STAN__MATH__SPECIAL_FUNCTIONS_HPP__
6 #include <boost/math/special_functions/gamma.hpp>
7 #include <boost/math/special_functions/beta.hpp>
8 #include <boost/math/tools/promotion.hpp>
9 #include <boost/throw_exception.hpp>
33 inline typename boost::math::tools::promote_args<T>::type
50 template <
typename T1,
typename T2>
51 inline typename boost::math::tools::promote_args<T1, T2>::type
53 return (a > b) ? (a - b) : 0.0;
68 template <
typename T1,
typename T2,
typename T3>
69 inline typename boost::math::tools::promote_args<T1,T2,T3>::type
70 fma(T1 a, T2 b, T3 c) {
86 inline typename boost::math::tools::promote_args<T>::type
89 const static double LOG2 =
std::log(2.0);
102 template <
typename T>
119 template <
typename T>
121 return y < 0.0 ? 0 : 1;
148 template <
typename T1,
typename T2>
149 inline typename boost::math::tools::promote_args<T1,T2>::type
174 template <
typename T_N,
typename T_n>
175 inline typename boost::math::tools::promote_args<T_N, T_n>::type
179 const double cutoff = 1000;
180 if ((N < cutoff) || (N - n < cutoff)) {
183 return n *
log(N - n) + (N + 0.5) *
log(N/(N-n))
184 + 1/(12*N) - n - 1/(12*(N-n)) -
lgamma(n + 1.0);
203 template <
typename T>
204 inline typename boost::math::tools::promote_args<T>::type
207 return 1.0 / (1.0 +
exp(-a));
224 template <
typename T>
225 inline typename boost::math::tools::promote_args<T>::type
228 return log(a / (1.0 - a));
248 template <
typename T>
249 inline typename boost::math::tools::promote_args<T>::type
251 static const double INV_SQRT_TWO = 1.0 /
std::sqrt(2.0);
268 template <
typename T>
269 inline typename boost::math::tools::promote_args<T>::type
290 template <
typename T>
291 inline typename boost::math::tools::promote_args<T>::type
294 return -
log(y ? y_hat : (1.0 - y_hat));
299 template <
typename Vector,
typename Scalar>
300 Scalar maximum(
const Vector& x) {
302 BOOST_THROW_EXCEPTION(std::invalid_argument (
"x must have at least one element"));
361 template <
typename Vector,
typename Scalar>
362 void softmax(
const Vector& x, Vector& simplex) {
364 if (x.size() != simplex.size())
365 BOOST_THROW_EXCEPTION(std::invalid_argument (
"x.size() != simplex.size()"));
367 Scalar max_x = maximum<Vector,Scalar>(x);
369 simplex[i] =
exp(x[i]-max_x);
398 template <
typename Vector>
401 if(simplex.size() != y.size())
402 BOOST_THROW_EXCEPTION(std::invalid_argument (
"simplex.size() != y.size()"));
403 for (
size_t i = 0; i < simplex.size(); ++i)
404 y[i] =
log(simplex[i]);
416 template <
typename T>
417 inline typename boost::math::tools::promote_args<T>::type
421 BOOST_THROW_EXCEPTION(std::domain_error (
"x can not be less than -1"));
423 if (x > 1
e-9 || x < -1
e-9)
425 else if (x > 1
e-16 || x < -1
e-16)
426 return x - 0.5 * x * x;
440 template <
typename T>
441 inline typename boost::math::tools::promote_args<T>::type
447 const double LOG_PI_OVER_FOUR =
std::log(boost::math::constants::pi<double>()) / 4.0;
466 template <
typename T>
467 inline typename boost::math::tools::promote_args<T>::type
469 typename boost::math::tools::promote_args<T>::type result
470 = k * (k - 1) * LOG_PI_OVER_FOUR;
471 for (
unsigned int j = 1; j <= k; ++j)
472 result +=
lgamma(x + (1.0 - j) / 2.0);
491 inline double if_else(
bool c,
double y_true,
double y_false) {
492 return c ? y_true : y_false;
509 template <
typename T>
514 template <
typename T_a,
typename T_b>
515 inline typename boost::math::tools::promote_args<T_a,T_b>::type
518 if (b == 0.0 && a == 0.0)
553 template <
typename T>
554 inline typename boost::math::tools::promote_args<T>::type
571 template <
typename T>
572 inline typename boost::math::tools::promote_args<T>::type
610 inline double ibeta(
const double& a,
630 using std::numeric_limits;
633 double max = -numeric_limits<double>::infinity();
634 for (
size_t ii = 0; ii < x.size(); ii++)
639 for (
size_t ii = 0; ii < x.size(); ii++)
640 if (x[ii] != -numeric_limits<double>::infinity())
654 template <
typename T>
672 template <
typename T1,
typename T2>
676 return (x1 != 0) || (x2 != 0);
691 template <
typename T1,
typename T2>
695 return (x1 != 0) && (x2 != 0);
710 template <
typename T1,
typename T2>
727 template <
typename T1,
typename T2>
744 template <
typename T1,
typename T2>
761 template <
typename T1,
typename T2>
778 template <
typename T1,
typename T2>
795 template <
typename T1,
typename T2>
821 template <
typename T1,
typename T2,
typename T3>
823 const T1& ,
const T1& ,
824 const T2& ,
const T2& ,
825 const T3& ,
const T3& ) {
841 template <
typename T>
843 return static_cast<double>(x);
870 return boost::math::constants::pi<double>();
internal::traits< Derived >::Index size_type
var sqrt(const var &a)
Return the square root of the specified variable (cmath).
var lgamma(const stan::agrad::var &a)
The log gamma function for variables (C99).
var erf(const stan::agrad::var &a)
The error function for variables (C99).
var pow(const var &base, const var &exponent)
Return the base raised to the power of the exponent (cmath).
var log(const var &a)
Return the natural log of the specified variable (cmath).
var exp(const var &a)
Return the exponentiation of the specified variable (cmath).
boost::math::tools::promote_args< T >::type inv_cloglog(T x)
The inverse complementary log-log function.
int logical_negation(T x)
The logical negation function which returns 1 if the input is equal to zero and 0 otherwise.
int logical_lt(T1 x1, T2 x2)
Return 1 if the first argument is strictly less than the second.
double value_of< double >(double x)
Return the specified argument.
boost::math::tools::promote_args< T >::type logit(T a)
Returns the logit function applied to the argument.
int logical_gt(T1 x1, T2 x2)
Return 1 if the first argument is strictly greater than the second.
double sqrt2()
Return the square root of two.
double epsilon()
Return minimum positive number representable.
const double NEGATIVE_EPSILON
Largest negative value (i.e., smallest absolute value).
void inverse_softmax(const Vector &simplex, Vector &y)
Writes the inverse softmax of the simplex argument into the second argument.
T sum(const std::vector< T > &xs)
Return the sum of the values in the specified standard vector.
double log10()
Return natural logarithm of ten.
double log_sum_exp(const double &a, const double &b)
Calculates the log sum of exponetials without overflow.
double positive_infinity()
Return positive infinity.
double negative_infinity()
Return negative infinity.
double not_a_number()
Return (quiet) not-a-number.
boost::math::tools::promote_args< T >::type lmgamma(unsigned int k, T x)
Return the natural logarithm of the multivariate gamma function with the speciifed dimensions and arg...
double e()
Return the base of the natural logarithm.
double ibeta(const double &a, const double &b, const double &x)
The normalized incomplete beta function of a, b, and x.
const double LOG_2
The natural logarithm of 2, .
const double SQRT_2
The value of the square root of 2, .
double simple_var(double v, const T1 &, const T1 &, const T2 &, const T2 &, const T3 &, const T3 &)
Return the scalar value and ignore the remaining arguments.
int logical_eq(T1 x1, T2 x2)
Return 1 if the first argument is equal to the second.
boost::math::tools::promote_args< T >::type log1m(T x)
Return the natural logarithm of one minus the specified value.
int as_bool(int x)
Return an integer with an equivalent boolean value to specified input.
int step(T y)
The step, or Heaviside, function.
boost::math::tools::promote_args< T >::type exp2(T y)
Return the exponent base 2 of the specified argument (C99).
int max(const std::vector< int > &x)
Returns the maximum coefficient in the specified column vector.
boost::math::tools::promote_args< T_a, T_b >::type multiply_log(T_a a, T_b b)
const double EPSILON
Smallest positive value.
boost::math::tools::promote_args< T >::type log1p(T x)
Return the natural logarithm of one plus the specified value.
const double E
The base of the natural logarithm, .
boost::math::tools::promote_args< T >::type log1m_inv_logit(const T &u)
Returns the natural logarithm of 1 minus the inverse logit of the specified argument.
boost::math::tools::promote_args< T >::type inv_logit(T a)
Returns the inverse logit function applied to the argument.
int logical_and(T1 x1, T2 x2)
The logical and function which returns 1 if both arguments are unequal to zero and 0 otherwise.
double negative_epsilon()
Return maximum negative number (i.e., negative number with smallest absolute value).
int logical_gte(T1 x1, T2 x2)
Return 1 if the first argument is greater than or equal to the second.
Eigen::Matrix< T, Eigen::Dynamic, 1 > softmax(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &v)
Return the softmax of the specified vector.
int logical_neq(T1 x1, T2 x2)
Return 1 if the first argument is unequal to the second.
boost::math::tools::promote_args< T >::type binary_log_loss(int y, T y_hat)
Returns the log loss function for binary classification with specified reference and response values.
boost::math::tools::promote_args< T1, T2, T3 >::type fma(T1 a, T2 b, T3 c)
The fused multiply-add operation (C99).
boost::math::tools::promote_args< T1, T2 >::type fdim(T1 a, T2 b)
The positive difference function (C99).
double value_of(T x)
Return the value of the specified scalar argument converted to a double value.
const double INFTY
Positive infinity.
boost::math::tools::promote_args< T >::type Phi(T x)
The unit normal cumulative distribution function.
double if_else(bool c, double y_true, double y_false)
Return the second argument if the first argument is true and otherwise return the second argument.
T square(T x)
Return the square of the specified argument.
boost::math::tools::promote_args< T1, T2 >::type lbeta(T1 a, T2 b)
Return the log of the beta function applied to the specified arguments.
boost::math::tools::promote_args< T_N, T_n >::type binomial_coefficient_log(T_N N, T_n n)
Return the log of the binomial coefficient for the specified arguments.
double log1p_exp(const double &a)
Calculates the log of 1 plus the exponential of the specified value without overflow.
const double NOT_A_NUMBER
(Quiet) not-a-number value.
boost::math::tools::promote_args< T >::type log2(T a)
Returns the base 2 logarithm of the argument (C99).
double pi()
Return the value of pi.
const double NEGATIVE_INFTY
Negative infinity.
const double LOG_10
The natural logarithm of 10, .
Eigen::Matrix< T, Rows, Cols > exp(const Eigen::Matrix< T, Rows, Cols > &m)
Return the element-wise exponentiation of the matrix or vector.
int logical_lte(T1 x1, T2 x2)
Return 1 if the first argument is less than or equal to the second.
int logical_or(T1 x1, T2 x2)
The logical or function which returns 1 if either argument is unequal to zero and 0 otherwise.
boost::math::tools::promote_args< T >::type log_inv_logit(const T &u)
Returns the natural logarithm of the inverse logit of the specified argument.
unsigned int int_step(T y)
The integer step, or Heaviside, function.
Eigen::Matrix< T, Rows, Cols > log(const Eigen::Matrix< T, Rows, Cols > &m)
Return the element-wise logarithm of the matrix or vector.
Probability, optimization and sampling library.