Automatic Differentiation
 
Loading...
Searching...
No Matches
fmax.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_FMAX_HPP
2#define STAN_MATH_REV_FUN_FMAX_HPP
3
9
10namespace stan {
11namespace math {
12
61inline var fmax(const var& a, const var& b) {
62 if (unlikely(is_nan(a))) {
63 if (unlikely(is_nan(b))) {
64 return make_callback_var(NOT_A_NUMBER, [a, b](auto& vi) mutable {
65 a.adj() = NOT_A_NUMBER;
66 b.adj() = NOT_A_NUMBER;
67 });
68 }
69 return b;
70 }
71 if (unlikely(is_nan(b))) {
72 return a;
73 }
74 return a > b ? a : b;
75}
76
91inline var fmax(const var& a, double b) {
92 if (unlikely(is_nan(a))) {
93 if (unlikely(is_nan(b))) {
94 return make_callback_var(
95 NOT_A_NUMBER, [a](auto& vi) mutable { a.adj() = NOT_A_NUMBER; });
96 }
97 return var(b);
98 }
99 if (unlikely(is_nan(b))) {
100 return a;
101 }
102 return a >= b ? a : var(b);
103}
104
119inline var fmax(double a, const var& b) {
120 if (unlikely(is_nan(b))) {
121 if (unlikely(is_nan(a))) {
122 return make_callback_var(
123 NOT_A_NUMBER, [b](auto& vi) mutable { b.adj() = NOT_A_NUMBER; });
124 }
125 return var(a);
126 }
127 if (unlikely(is_nan(a))) {
128 return b;
129 }
130 return a > b ? var(a) : b;
131}
132
133} // namespace math
134} // namespace stan
135#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
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...
fvar< T > fmax(const fvar< T > &x1, const fvar< T > &x2)
Return the greater of the two specified arguments.
Definition fmax.hpp:23
var_value< double > var
Definition var.hpp:1187
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...