Stan Math Library  2.20.0
reverse mode automatic differentiation
fmax.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_FWD_SCAL_FUN_FMAX_HPP
2 #define STAN_MATH_FWD_SCAL_FUN_FMAX_HPP
3 
4 #include <stan/math/fwd/meta.hpp>
5 #include <stan/math/fwd/core.hpp>
9 
10 namespace stan {
11 namespace math {
12 
21 template <typename T>
22 inline fvar<T> fmax(const fvar<T>& x1, const fvar<T>& x2) {
23  if (unlikely(is_nan(x1.val_))) {
24  if (is_nan(x2.val_))
25  return fvar<T>(fmax(x1.val_, x2.val_), NOT_A_NUMBER);
26  else
27  return fvar<T>(x2.val_, x2.d_);
28  } else if (unlikely(is_nan(x2.val_))) {
29  return fvar<T>(x1.val_, x1.d_);
30  } else if (x1.val_ > x2.val_) {
31  return fvar<T>(x1.val_, x1.d_);
32  } else if (x1.val_ == x2.val_) {
33  return fvar<T>(x1.val_, NOT_A_NUMBER);
34  } else {
35  return fvar<T>(x2.val_, x2.d_);
36  }
37 }
38 
47 template <typename T>
48 inline fvar<T> fmax(double x1, const fvar<T>& x2) {
49  if (unlikely(is_nan(x1))) {
50  if (is_nan(x2.val_))
51  return fvar<T>(fmax(x1, x2.val_), NOT_A_NUMBER);
52  else
53  return fvar<T>(x2.val_, x2.d_);
54  } else if (unlikely(is_nan(x2.val_))) {
55  return fvar<T>(x1, 0.0);
56  } else if (x1 > x2.val_) {
57  return fvar<T>(x1, 0.0);
58  } else if (x1 == x2.val_) {
59  return fvar<T>(x2.val_, NOT_A_NUMBER);
60  } else {
61  return fvar<T>(x2.val_, x2.d_);
62  }
63 }
64 
73 template <typename T>
74 inline fvar<T> fmax(const fvar<T>& x1, double x2) {
75  if (unlikely(is_nan(x1.val_))) {
76  if (is_nan(x2))
77  return fvar<T>(fmax(x1.val_, x2), NOT_A_NUMBER);
78  else
79  return fvar<T>(x2, 0.0);
80  } else if (unlikely(is_nan(x2))) {
81  return fvar<T>(x1.val_, x1.d_);
82  } else if (x1.val_ > x2) {
83  return fvar<T>(x1.val_, x1.d_);
84  } else if (x1.val_ == x2) {
85  return fvar<T>(x1.val_, NOT_A_NUMBER);
86  } else {
87  return fvar<T>(x2, 0.0);
88  }
89 }
90 } // namespace math
91 } // namespace stan
92 #endif
T d_
The tangent (derivative) of this variable.
Definition: fvar.hpp:50
const double NOT_A_NUMBER
(Quiet) not-a-number value.
Definition: constants.hpp:58
#define unlikely(x)
Definition: likely.hpp:9
T val_
The value of this variable.
Definition: fvar.hpp:45
int is_nan(const fvar< T > &x)
Returns 1 if the input&#39;s value is NaN and 0 otherwise.
Definition: is_nan.hpp:20
fvar< T > fmax(const fvar< T > &x1, const fvar< T > &x2)
Return the greater of the two specified arguments.
Definition: fmax.hpp:22
This template class represents scalars used in forward-mode automatic differentiation, which consist of values and directional derivatives of the specified template type.
Definition: fvar.hpp:41

     [ Stan Home Page ] © 2011–2018, Stan Development Team.