Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fmax.hpp
Go to the documentation of this file.
1 #ifndef STAN__AGRAD__FWD__FUNCTIONS__FMAX_HPP
2 #define STAN__AGRAD__FWD__FUNCTIONS__FMAX_HPP
3 
5 #include <stan/meta/traits.hpp>
7 #include <math.h>
8 #include <stan/meta/likely.hpp>
9 
10 namespace stan {
11 
12  namespace agrad {
13 
14  template <typename T>
15  inline
16  fvar<T>
17  fmax(const fvar<T>& x1, const fvar<T>& x2) {
20  if (unlikely(boost::math::isnan(x1.val_))) {
21  if (boost::math::isnan(x2.val_))
22  return fvar<T>(fmax(x1.val_,x2.val_),NOT_A_NUMBER);
23  else
24  return fvar<T>(x2.val_, x2.d_);
25  } else if (unlikely(boost::math::isnan(x2.val_)))
26  return fvar<T>(x1.val_,x1.d_);
27  else if(x1.val_ > x2.val_)
28  return fvar<T>(x1.val_, x1.d_);
29  else if(x1.val_ == x2.val_)
30  return fvar<T>(x1.val_, NOT_A_NUMBER);
31  else
32  return fvar<T>(x2.val_, x2.d_);
33  }
34 
35  template <typename T>
36  inline
37  fvar<T>
38  fmax(const double x1, const fvar<T>& x2) {
41  if (unlikely(boost::math::isnan(x1))) {
42  if (boost::math::isnan(x2.val_))
43  return fvar<T>(fmax(x1,x2.val_),NOT_A_NUMBER);
44  else
45  return fvar<T>(x2.val_, x2.d_);
46  } else if (unlikely(boost::math::isnan(x2.val_)))
47  return fvar<T>(x1,0.0);
48  else if(x1 > x2.val_)
49  return fvar<T>(x1, 0.0);
50  else if(x1 == x2.val_)
51  return fvar<T>(x2.val_, NOT_A_NUMBER);
52  else
53  return fvar<T>(x2.val_, x2.d_);
54  }
55 
56  template <typename T>
57  inline
58  fvar<T>
59  fmax(const fvar<T>& x1, const double x2) {
62  if (unlikely(boost::math::isnan(x1.val_))) {
63  if (boost::math::isnan(x2))
64  return fvar<T>(fmax(x1.val_,x2),NOT_A_NUMBER);
65  else
66  return fvar<T>(x2, 0.0);
67  } else if (unlikely(boost::math::isnan(x2)))
68  return fvar<T>(x1.val_,x1.d_);
69  else if(x1.val_ > x2)
70  return fvar<T>(x1.val_, x1.d_);
71  else if(x1.val_ == x2)
72  return fvar<T>(x1.val_, NOT_A_NUMBER);
73  else
74  return fvar<T>(x2, 0.0);
75  }
76  }
77 }
78 #endif
bool isnan(const stan::agrad::var &v)
Checks if the given number is NaN.
var fmax(const double &a, const stan::agrad::var &b)
Returns the maximum of a scalar and variable, promoting the scalar to a variable if it is larger (C99...
Definition: fmax.hpp:132
const double NOT_A_NUMBER
(Quiet) not-a-number value.
Definition: constants.hpp:53
fvar< T > fmax(const fvar< T > &x1, const fvar< T > &x2)
Definition: fmax.hpp:17
#define unlikely(x)
Definition: likely.hpp:9

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