1 #ifndef STAN_MATH_FWD_SCAL_FUN_FDIM_HPP
2 #define STAN_MATH_FWD_SCAL_FUN_FDIM_HPP
3
4 #include <stan/math/fwd/core.hpp>
5 #include <stan/math/prim/scal/fun/fdim.hpp>
6
7 namespace stan {
8 namespace math {
9
10 /**
11 * Return the positive difference of the specified values (C++11).
12 *
13 * @tparam T Scalar type of autodiff variables.
14 * @param x First argument.
15 * @param y Second argument.
16 * @return Return the differences of the arguments if it is
17 * positive and 0 otherwise.
18 */
19 template <typename T>
fdim(const fvar<T> & x,const fvar<T> & y)20 inline fvar<T> fdim(const fvar<T>& x, const fvar<T>& y) {
21 if (x.val_ < y.val_)
22 return fvar<T>(fdim(x.val_, y.val_), 0);
23 else
24 return fvar<T>(fdim(x.val_, y.val_), x.d_ - y.d_);
25 }
26
27 /**
28 * Return the positive difference of the specified values (C++11).
29 *
30 * @tparam T Scalar type of autodiff variables.
31 * @param x First argument.
32 * @param y Second argument.
33 * @return Return the differences of the arguments if it is
34 * positive and 0 otherwise.
35 */
36 template <typename T>
fdim(const fvar<T> & x,double y)37 inline fvar<T> fdim(const fvar<T>& x, double y) {
38 if (x.val_ < y)
39 return fvar<T>(fdim(x.val_, y), 0);
40 else
41 return fvar<T>(fdim(x.val_, y), x.d_);
42 }
43
44 /**
45 * Return the positive difference of the specified values (C++11).
46 *
47 * @tparam T Scalar type of autodiff variables.
48 * @param x First argument.
49 * @param y Second argument.
50 * @return Return the differences of the arguments if it is
51 * positive and 0 otherwise.
52 */
53 template <typename T>
fdim(double x,const fvar<T> & y)54 inline fvar<T> fdim(double x, const fvar<T>& y) {
55 if (x < y.val_)
56 return fvar<T>(fdim(x, y.val_), 0);
57 else
58 return fvar<T>(fdim(x, y.val_), -y.d_);
59 }
60
61 } // namespace math
62 } // namespace stan
63 #endif
64