Stan Math Library  2.20.0
reverse mode automatic differentiation
fabs.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_FWD_SCAL_FUN_FABS_HPP
2 #define STAN_MATH_FWD_SCAL_FUN_FABS_HPP
3 
4 #include <stan/math/fwd/meta.hpp>
5 #include <stan/math/fwd/core.hpp>
9 #include <cmath>
10 
11 namespace stan {
12 namespace math {
13 
14 template <typename T>
15 inline fvar<T> fabs(const fvar<T>& x) {
16  using std::fabs;
17 
18  if (unlikely(is_nan(value_of(x.val_))))
19  return fvar<T>(fabs(x.val_), NOT_A_NUMBER);
20  else if (x.val_ > 0.0)
21  return x;
22  else if (x.val_ < 0.0)
23  return fvar<T>(-x.val_, -x.d_);
24  else
25  return fvar<T>(0, 0);
26 }
27 
28 } // namespace math
29 } // namespace stan
30 #endif
T d_
The tangent (derivative) of this variable.
Definition: fvar.hpp:50
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:15
const double NOT_A_NUMBER
(Quiet) not-a-number value.
Definition: constants.hpp:58
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:17
#define unlikely(x)
Definition: likely.hpp:9
T val_
The value of this variable.
Definition: fvar.hpp:45
var fabs(const var &a)
Return the absolute value of the variable (cmath).
Definition: fabs.hpp:50
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
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.