1 #ifndef STAN_MATH_PRIM_FUN_POLAR_HPP
2 #define STAN_MATH_PRIM_FUN_POLAR_HPP
3
4 #include <stan/math/prim/fun/cos.hpp>
5 #include <stan/math/prim/fun/is_inf.hpp>
6 #include <stan/math/prim/meta.hpp>
7 #include <cmath>
8 #include <complex>
9 #include <limits>
10
11 namespace stan {
12 namespace math {
13 namespace internal {
14 /**
15 * Returns complex number with specified magnitude and phase angle.
16 *
17 * @param[in] r magnitude
18 * @param[in] theta phase angle
19 * @return complex number with magnitude and phase angle
20 */
21 template <typename U, typename V>
complex_polar(const U & r,const V & theta)22 inline complex_return_t<U, V> complex_polar(const U& r, const V& theta) {
23 using std::cos;
24 using std::sin;
25 if (!(r >= 0) || is_inf(theta)) {
26 return {std::numeric_limits<double>::quiet_NaN()};
27 }
28 return {r * cos(theta), r * sin(theta)};
29 }
30 } // namespace internal
31
32 /**
33 * Returns the complex number with specified magnitude and phase angle.
34 *
35 * @param[in] r magnitude
36 * @param[in] theta phase angle
37 * @return complex number with magnitude and phase angle
38 */
polar(double r,double theta)39 inline std::complex<double> polar(double r, double theta) {
40 return internal::complex_polar(r, theta);
41 }
polar(double r,int theta)42 inline std::complex<double> polar(double r, int theta) {
43 return internal::complex_polar(r, static_cast<double>(theta));
44 }
polar(int r,double theta)45 inline std::complex<double> polar(int r, double theta) {
46 return internal::complex_polar(static_cast<double>(r), theta);
47 }
polar(int r,int theta)48 inline std::complex<double> polar(int r, int theta) {
49 return internal::complex_polar(static_cast<double>(r),
50 static_cast<double>(theta));
51 }
52
53 } // namespace math
54 } // namespace stan
55
56 #endif
57