1 #ifndef STAN_MATH_REV_FUN_ATAN2_HPP
2 #define STAN_MATH_REV_FUN_ATAN2_HPP
3 
4 #include <stan/math/rev/meta.hpp>
5 #include <stan/math/rev/core.hpp>
6 #include <cmath>
7 
8 namespace stan {
9 namespace math {
10 
11 /**
12  * Return the principal value of the arc tangent, in radians, of
13  * the first variable divided by the second (cmath).
14  *
15  * The partial derivatives are defined by
16  *
17  * \f$ \frac{\partial}{\partial x} \arctan \frac{x}{y} = \frac{y}{x^2 + y^2}\f$,
18  * and
19  *
20  * \f$ \frac{\partial}{\partial y} \arctan \frac{x}{y} = \frac{-x}{x^2 +
21  * y^2}\f$.
22  *
23  * @param a Numerator variable.
24  * @param b Denominator variable.
25  * @return The arc tangent of the fraction, in radians.
26  */
atan2(const var & a,const var & b)27 inline var atan2(const var& a, const var& b) {
28   return make_callback_var(
29       std::atan2(a.val(), b.val()), [a, b](const auto& vi) mutable {
30         double a_sq_plus_b_sq = (a.val() * a.val()) + (b.val() * b.val());
31         a.adj() += vi.adj_ * b.val() / a_sq_plus_b_sq;
32         b.adj() -= vi.adj_ * a.val() / a_sq_plus_b_sq;
33       });
34 }
35 
36 /**
37  * Return the principal value of the arc tangent, in radians, of
38  * the first variable divided by the second scalar (cmath).
39  *
40  * The derivative with respect to the variable is
41  *
42  * \f$ \frac{d}{d x} \arctan \frac{x}{c} = \frac{c}{x^2 + c^2}\f$.
43  *
44  * @param a Numerator variable.
45  * @param b Denominator scalar.
46  * @return The arc tangent of the fraction, in radians.
47  */
atan2(const var & a,double b)48 inline var atan2(const var& a, double b) {
49   return make_callback_var(
50       std::atan2(a.val(), b), [a, b](const auto& vi) mutable {
51         double a_sq_plus_b_sq = (a.val() * a.val()) + (b * b);
52         a.adj() += vi.adj_ * b / a_sq_plus_b_sq;
53       });
54 }
55 
56 /**
57  * Return the principal value of the arc tangent, in radians, of
58  * the first scalar divided by the second variable (cmath).
59  *
60  * The derivative with respect to the variable is
61  *
62  * \f$ \frac{\partial}{\partial y} \arctan \frac{c}{y} = \frac{-c}{c^2 +
63  y^2}\f$.
64  *
65  *
66    \f[
67    \mbox{atan2}(x, y) =
68    \begin{cases}
69      \arctan\left(\frac{x}{y}\right) & \mbox{if } -\infty\leq x \leq \infty,
70  -\infty\leq y \leq \infty \\[6pt] \textrm{NaN} & \mbox{if } x = \textrm{NaN or
71  } y = \textrm{NaN} \end{cases} \f]
72 
73    \f[
74    \frac{\partial\, \mbox{atan2}(x, y)}{\partial x} =
75    \begin{cases}
76      \frac{y}{x^2+y^2} & \mbox{if } -\infty\leq x\leq \infty, -\infty\leq y \leq
77  \infty \\[6pt] \textrm{NaN} & \mbox{if } x = \textrm{NaN or } y = \textrm{NaN}
78    \end{cases}
79    \f]
80 
81    \f[
82    \frac{\partial\, \mbox{atan2}(x, y)}{\partial y} =
83    \begin{cases}
84      -\frac{x}{x^2+y^2} & \mbox{if } -\infty\leq x\leq \infty, -\infty\leq y
85  \leq \infty \\[6pt] \textrm{NaN} & \mbox{if } x = \textrm{NaN or } y =
86  \textrm{NaN} \end{cases} \f]
87  *
88  * @param a Numerator scalar.
89  * @param b Denominator variable.
90  * @return The arc tangent of the fraction, in radians.
91  */
atan2(double a,const var & b)92 inline var atan2(double a, const var& b) {
93   return make_callback_var(
94       std::atan2(a, b.val()), [a, b](const auto& vi) mutable {
95         double a_sq_plus_b_sq = (a * a) + (b.val() * b.val());
96         b.adj() -= vi.adj_ * a / a_sq_plus_b_sq;
97       });
98 }
99 
100 }  // namespace math
101 }  // namespace stan
102 #endif
103