1 #ifndef STAN_MATH_REV_FUN_SIN_HPP
2 #define STAN_MATH_REV_FUN_SIN_HPP
3 
4 #include <stan/math/prim/fun/cos.hpp>
5 #include <stan/math/prim/fun/isfinite.hpp>
6 #include <stan/math/prim/fun/isinf.hpp>
7 #include <stan/math/prim/fun/sin.hpp>
8 #include <stan/math/rev/core.hpp>
9 #include <stan/math/rev/meta.hpp>
10 #include <stan/math/rev/fun/is_inf.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 sine of a radian-scaled variable (cmath).
21  *
22  * The derivative is defined by
23  *
24  * \f$\frac{d}{dx} \sin x = \cos x\f$.
25  *
26  *
27    \f[
28    \mbox{sin}(x) =
29    \begin{cases}
30      \sin(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{sin}(x)}{\partial x} =
37    \begin{cases}
38      \cos(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 Sine of variable.
45  */
sin(const var & a)46 inline var sin(const var& a) {
47   return make_callback_var(std::sin(a.val()), [a](const auto& vi) mutable {
48     a.adj() += vi.adj() * std::cos(a.val());
49   });
50 }
51 
52 /**
53  * Return the sine of a radian-scaled variable (cmath).
54  *
55  * @tparam Varmat a `var_value` with inner Eigen type
56  * @param a Variable for radians of angle.
57  * @return Sine of variable.
58  */
59 template <typename VarMat, require_var_matrix_t<VarMat>* = nullptr>
sin(const VarMat & a)60 inline auto sin(const VarMat& a) {
61   return make_callback_var(
62       a.val().array().sin().matrix(), [a](const auto& vi) mutable {
63         a.adj() += vi.adj().cwiseProduct(a.val().array().cos().matrix());
64       });
65 }
66 
67 /**
68  * Return the sine of the complex argument.
69  *
70  * @param[in] z argument
71  * @return sine of the argument
72  */
sin(const std::complex<var> & z)73 inline std::complex<var> sin(const std::complex<var>& z) {
74   return stan::math::internal::complex_sin(z);
75 }
76 
77 }  // namespace math
78 }  // namespace stan
79 #endif
80