Automatic Differentiation
 
Loading...
Searching...
No Matches
fabs.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_FWD_FUN_FABS_HPP
2#define STAN_MATH_FWD_FUN_FABS_HPP
3
9#include <cmath>
10
11namespace stan {
12namespace math {
13
14template <typename T>
15inline 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
29} // namespace math
30} // namespace stan
31#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
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
fvar< T > fabs(const fvar< T > &x)
Definition fabs.hpp:15
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Scalar val_
The value of this variable.
Definition fvar.hpp:49
Scalar d_
The tangent (derivative) of this variable.
Definition fvar.hpp:61
This template class represents scalars used in forward-mode automatic differentiation,...
Definition fvar.hpp:40