1 #ifndef STAN_MATH_REV_FUN_COS_HPP
2 #define STAN_MATH_REV_FUN_COS_HPP
3 
4 #include <stan/math/prim/fun/abs.hpp>
5 #include <stan/math/prim/fun/cos.hpp>
6 #include <stan/math/prim/fun/isinf.hpp>
7 #include <stan/math/prim/fun/isfinite.hpp>
8 #include <stan/math/rev/core.hpp>
9 #include <stan/math/rev/meta.hpp>
10 #include <stan/math/rev/fun/abs.hpp>
11 #include <stan/math/rev/fun/cosh.hpp>
12 #include <stan/math/rev/fun/sinh.hpp>
13 #include <cmath>
14 #include <complex>
15 
16 namespace stan {
17 namespace math {
18 
19 /**
20  * Return the cosine of a radian-scaled variable (cmath).
21  *
22  * The derivative is defined by
23  *
24  * \f$\frac{d}{dx} \cos x = - \sin x\f$.
25  *
26  *
27    \f[
28    \mbox{cos}(x) =
29    \begin{cases}
30      \cos(x) & \mbox{if } -\infty\leq x \leq \infty \\[6pt]
31      \textrm{NaN} & \mbox{if } x = \textrm{NaN}
32    \end{cases}
33    \f]
34 
35    \f[
36    \frac{\partial\, \mbox{cos}(x)}{\partial x} =
37    \begin{cases}
38      -\sin(x) & \mbox{if } -\infty\leq x\leq \infty \\[6pt]
39      \textrm{NaN} & \mbox{if } x = \textrm{NaN}
40    \end{cases}
41    \f]
42  *
43  * @param a Variable for radians of angle.
44  * @return Cosine of variable.
45  */
cos(var a)46 inline var cos(var a) {
47   return make_callback_var(std::cos(a.val()), [a](const auto& vi) mutable {
48     a.adj() -= vi.adj() * std::sin(a.val());
49   });
50 }
51 
52 /**
53  * Return the cosine of a radian-scaled variable (cmath).
54  *
55  *
56  * @tparam Varmat a `var_value` with inner Eigen type
57  * @param a Variable for radians of angle.
58  * @return Cosine of variable.
59  */
60 template <typename VarMat, require_var_matrix_t<VarMat>* = nullptr>
cos(const VarMat & a)61 inline auto cos(const VarMat& a) {
62   return make_callback_var(
63       a.val().array().cos().matrix(), [a](const auto& vi) mutable {
64         a.adj() -= vi.adj().cwiseProduct(a.val().array().sin().matrix());
65       });
66 }
67 /**
68  * Return the cosine of the complex argument.
69  *
70  * @param[in] z argument
71  * @return cosine of the argument
72  */
cos(const std::complex<var> & z)73 inline std::complex<var> cos(const std::complex<var>& z) {
74   return stan::math::internal::complex_cos(z);
75 }
76 
77 }  // namespace math
78 }  // namespace stan
79 #endif
80