Automatic Differentiation
 
Loading...
Searching...
No Matches
fmin.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_FMIN_HPP
2#define STAN_MATH_REV_FUN_FMIN_HPP
3
9
10namespace stan {
11namespace math {
12
57inline var fmin(const var& a, const var& b) {
58 if (unlikely(is_nan(a))) {
59 if (unlikely(is_nan(b))) {
60 return var(new precomp_vv_vari(NOT_A_NUMBER, a.vi_, b.vi_, NOT_A_NUMBER,
62 }
63 return b;
64 }
65 if (unlikely(is_nan(b))) {
66 return a;
67 }
68 return a < b ? a : b;
69}
70
85inline var fmin(const var& a, double b) {
86 if (unlikely(is_nan(a))) {
87 if (unlikely(is_nan(b))) {
88 return make_callback_var(
89 NOT_A_NUMBER, [a](auto& vi) mutable { a.adj() = NOT_A_NUMBER; });
90 }
91 return var(b);
92 }
93 if (unlikely(is_nan(b))) {
94 return a;
95 }
96 return a <= b ? a : var(b);
97}
98
113inline var fmin(double a, const var& b) {
114 if (unlikely(is_nan(b))) {
115 if (unlikely(is_nan(a))) {
116 return make_callback_var(
117 NOT_A_NUMBER, [b](auto& vi) mutable { b.adj() = NOT_A_NUMBER; });
118 }
119 return var(a);
120 }
121 if (unlikely(is_nan(a))) {
122 return b;
123 }
124 return b <= a ? b : var(a);
125}
126
127} // namespace math
128} // namespace stan
129#endif
#define unlikely(x)
static constexpr double NOT_A_NUMBER
(Quiet) not-a-number value.
Definition constants.hpp:56
bool is_nan(T &&x)
Returns 1 if the input's value is NaN and 0 otherwise.
Definition is_nan.hpp:22
fvar< T > fmin(const fvar< T > &x1, const fvar< T > &x2)
Definition fmin.hpp:14
var_value< plain_type_t< T > > make_callback_var(T &&value, F &&functor)
Creates a new var initialized with a callback_vari with a given value and reverse-pass callback funct...
var_value< double > var
Definition var.hpp:1187
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...