Chris@16
|
1 // (C) Copyright John Maddock 2005.
|
Chris@16
|
2 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
3 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
4
|
Chris@16
|
5 #ifndef BOOST_MATH_COMPLEX_ACOS_INCLUDED
|
Chris@16
|
6 #define BOOST_MATH_COMPLEX_ACOS_INCLUDED
|
Chris@16
|
7
|
Chris@16
|
8 #ifndef BOOST_MATH_COMPLEX_DETAILS_INCLUDED
|
Chris@16
|
9 # include <boost/math/complex/details.hpp>
|
Chris@16
|
10 #endif
|
Chris@16
|
11 #ifndef BOOST_MATH_LOG1P_INCLUDED
|
Chris@16
|
12 # include <boost/math/special_functions/log1p.hpp>
|
Chris@16
|
13 #endif
|
Chris@16
|
14 #include <boost/assert.hpp>
|
Chris@16
|
15
|
Chris@16
|
16 #ifdef BOOST_NO_STDC_NAMESPACE
|
Chris@16
|
17 namespace std{ using ::sqrt; using ::fabs; using ::acos; using ::asin; using ::atan; using ::atan2; }
|
Chris@16
|
18 #endif
|
Chris@16
|
19
|
Chris@16
|
20 namespace boost{ namespace math{
|
Chris@16
|
21
|
Chris@16
|
22 template<class T>
|
Chris@16
|
23 std::complex<T> acos(const std::complex<T>& z)
|
Chris@16
|
24 {
|
Chris@16
|
25 //
|
Chris@16
|
26 // This implementation is a transcription of the pseudo-code in:
|
Chris@16
|
27 //
|
Chris@16
|
28 // "Implementing the Complex Arcsine and Arccosine Functions using Exception Handling."
|
Chris@16
|
29 // T E Hull, Thomas F Fairgrieve and Ping Tak Peter Tang.
|
Chris@16
|
30 // ACM Transactions on Mathematical Software, Vol 23, No 3, Sept 1997.
|
Chris@16
|
31 //
|
Chris@16
|
32
|
Chris@16
|
33 //
|
Chris@16
|
34 // These static constants should really be in a maths constants library,
|
Chris@16
|
35 // note that we have tweaked a_crossover as per: https://svn.boost.org/trac/boost/ticket/7290
|
Chris@16
|
36 //
|
Chris@16
|
37 static const T one = static_cast<T>(1);
|
Chris@16
|
38 //static const T two = static_cast<T>(2);
|
Chris@16
|
39 static const T half = static_cast<T>(0.5L);
|
Chris@16
|
40 static const T a_crossover = static_cast<T>(10);
|
Chris@16
|
41 static const T b_crossover = static_cast<T>(0.6417L);
|
Chris@16
|
42 static const T s_pi = boost::math::constants::pi<T>();
|
Chris@16
|
43 static const T half_pi = s_pi / 2;
|
Chris@16
|
44 static const T log_two = boost::math::constants::ln_two<T>();
|
Chris@16
|
45 static const T quarter_pi = s_pi / 4;
|
Chris@16
|
46
|
Chris@16
|
47 #ifdef BOOST_MSVC
|
Chris@16
|
48 #pragma warning(push)
|
Chris@16
|
49 #pragma warning(disable:4127)
|
Chris@16
|
50 #endif
|
Chris@16
|
51 //
|
Chris@16
|
52 // Get real and imaginary parts, discard the signs as we can
|
Chris@16
|
53 // figure out the sign of the result later:
|
Chris@16
|
54 //
|
Chris@16
|
55 T x = std::fabs(z.real());
|
Chris@16
|
56 T y = std::fabs(z.imag());
|
Chris@16
|
57
|
Chris@16
|
58 T real, imag; // these hold our result
|
Chris@16
|
59
|
Chris@16
|
60 //
|
Chris@16
|
61 // Handle special cases specified by the C99 standard,
|
Chris@16
|
62 // many of these special cases aren't really needed here,
|
Chris@16
|
63 // but doing it this way prevents overflow/underflow arithmetic
|
Chris@16
|
64 // in the main body of the logic, which may trip up some machines:
|
Chris@16
|
65 //
|
Chris@16
|
66 if((boost::math::isinf)(x))
|
Chris@16
|
67 {
|
Chris@16
|
68 if((boost::math::isinf)(y))
|
Chris@16
|
69 {
|
Chris@16
|
70 real = quarter_pi;
|
Chris@16
|
71 imag = std::numeric_limits<T>::infinity();
|
Chris@16
|
72 }
|
Chris@16
|
73 else if((boost::math::isnan)(y))
|
Chris@16
|
74 {
|
Chris@16
|
75 return std::complex<T>(y, -std::numeric_limits<T>::infinity());
|
Chris@16
|
76 }
|
Chris@16
|
77 else
|
Chris@16
|
78 {
|
Chris@16
|
79 // y is not infinity or nan:
|
Chris@16
|
80 real = 0;
|
Chris@16
|
81 imag = std::numeric_limits<T>::infinity();
|
Chris@16
|
82 }
|
Chris@16
|
83 }
|
Chris@16
|
84 else if((boost::math::isnan)(x))
|
Chris@16
|
85 {
|
Chris@16
|
86 if((boost::math::isinf)(y))
|
Chris@16
|
87 return std::complex<T>(x, ((boost::math::signbit)(z.imag())) ? std::numeric_limits<T>::infinity() : -std::numeric_limits<T>::infinity());
|
Chris@16
|
88 return std::complex<T>(x, x);
|
Chris@16
|
89 }
|
Chris@16
|
90 else if((boost::math::isinf)(y))
|
Chris@16
|
91 {
|
Chris@16
|
92 real = half_pi;
|
Chris@16
|
93 imag = std::numeric_limits<T>::infinity();
|
Chris@16
|
94 }
|
Chris@16
|
95 else if((boost::math::isnan)(y))
|
Chris@16
|
96 {
|
Chris@16
|
97 return std::complex<T>((x == 0) ? half_pi : y, y);
|
Chris@16
|
98 }
|
Chris@16
|
99 else
|
Chris@16
|
100 {
|
Chris@16
|
101 //
|
Chris@16
|
102 // What follows is the regular Hull et al code,
|
Chris@16
|
103 // begin with the special case for real numbers:
|
Chris@16
|
104 //
|
Chris@16
|
105 if((y == 0) && (x <= one))
|
Chris@16
|
106 return std::complex<T>((x == 0) ? half_pi : std::acos(z.real()), (boost::math::changesign)(z.imag()));
|
Chris@16
|
107 //
|
Chris@16
|
108 // Figure out if our input is within the "safe area" identified by Hull et al.
|
Chris@16
|
109 // This would be more efficient with portable floating point exception handling;
|
Chris@16
|
110 // fortunately the quantities M and u identified by Hull et al (figure 3),
|
Chris@16
|
111 // match with the max and min methods of numeric_limits<T>.
|
Chris@16
|
112 //
|
Chris@16
|
113 T safe_max = detail::safe_max(static_cast<T>(8));
|
Chris@16
|
114 T safe_min = detail::safe_min(static_cast<T>(4));
|
Chris@16
|
115
|
Chris@16
|
116 T xp1 = one + x;
|
Chris@16
|
117 T xm1 = x - one;
|
Chris@16
|
118
|
Chris@16
|
119 if((x < safe_max) && (x > safe_min) && (y < safe_max) && (y > safe_min))
|
Chris@16
|
120 {
|
Chris@16
|
121 T yy = y * y;
|
Chris@16
|
122 T r = std::sqrt(xp1*xp1 + yy);
|
Chris@16
|
123 T s = std::sqrt(xm1*xm1 + yy);
|
Chris@16
|
124 T a = half * (r + s);
|
Chris@16
|
125 T b = x / a;
|
Chris@16
|
126
|
Chris@16
|
127 if(b <= b_crossover)
|
Chris@16
|
128 {
|
Chris@16
|
129 real = std::acos(b);
|
Chris@16
|
130 }
|
Chris@16
|
131 else
|
Chris@16
|
132 {
|
Chris@16
|
133 T apx = a + x;
|
Chris@16
|
134 if(x <= one)
|
Chris@16
|
135 {
|
Chris@16
|
136 real = std::atan(std::sqrt(half * apx * (yy /(r + xp1) + (s-xm1)))/x);
|
Chris@16
|
137 }
|
Chris@16
|
138 else
|
Chris@16
|
139 {
|
Chris@16
|
140 real = std::atan((y * std::sqrt(half * (apx/(r + xp1) + apx/(s+xm1))))/x);
|
Chris@16
|
141 }
|
Chris@16
|
142 }
|
Chris@16
|
143
|
Chris@16
|
144 if(a <= a_crossover)
|
Chris@16
|
145 {
|
Chris@16
|
146 T am1;
|
Chris@16
|
147 if(x < one)
|
Chris@16
|
148 {
|
Chris@16
|
149 am1 = half * (yy/(r + xp1) + yy/(s - xm1));
|
Chris@16
|
150 }
|
Chris@16
|
151 else
|
Chris@16
|
152 {
|
Chris@16
|
153 am1 = half * (yy/(r + xp1) + (s + xm1));
|
Chris@16
|
154 }
|
Chris@16
|
155 imag = boost::math::log1p(am1 + std::sqrt(am1 * (a + one)));
|
Chris@16
|
156 }
|
Chris@16
|
157 else
|
Chris@16
|
158 {
|
Chris@16
|
159 imag = std::log(a + std::sqrt(a*a - one));
|
Chris@16
|
160 }
|
Chris@16
|
161 }
|
Chris@16
|
162 else
|
Chris@16
|
163 {
|
Chris@16
|
164 //
|
Chris@16
|
165 // This is the Hull et al exception handling code from Fig 6 of their paper:
|
Chris@16
|
166 //
|
Chris@16
|
167 if(y <= (std::numeric_limits<T>::epsilon() * std::fabs(xm1)))
|
Chris@16
|
168 {
|
Chris@16
|
169 if(x < one)
|
Chris@16
|
170 {
|
Chris@16
|
171 real = std::acos(x);
|
Chris@16
|
172 imag = y / std::sqrt(xp1*(one-x));
|
Chris@16
|
173 }
|
Chris@16
|
174 else
|
Chris@16
|
175 {
|
Chris@16
|
176 // This deviates from Hull et al's paper as per https://svn.boost.org/trac/boost/ticket/7290
|
Chris@16
|
177 if(((std::numeric_limits<T>::max)() / xp1) > xm1)
|
Chris@16
|
178 {
|
Chris@16
|
179 // xp1 * xm1 won't overflow:
|
Chris@16
|
180 real = y / std::sqrt(xm1*xp1);
|
Chris@16
|
181 imag = boost::math::log1p(xm1 + std::sqrt(xp1*xm1));
|
Chris@16
|
182 }
|
Chris@16
|
183 else
|
Chris@16
|
184 {
|
Chris@16
|
185 real = y / x;
|
Chris@16
|
186 imag = log_two + std::log(x);
|
Chris@16
|
187 }
|
Chris@16
|
188 }
|
Chris@16
|
189 }
|
Chris@16
|
190 else if(y <= safe_min)
|
Chris@16
|
191 {
|
Chris@16
|
192 // There is an assumption in Hull et al's analysis that
|
Chris@16
|
193 // if we get here then x == 1. This is true for all "good"
|
Chris@16
|
194 // machines where :
|
Chris@16
|
195 //
|
Chris@16
|
196 // E^2 > 8*sqrt(u); with:
|
Chris@16
|
197 //
|
Chris@16
|
198 // E = std::numeric_limits<T>::epsilon()
|
Chris@16
|
199 // u = (std::numeric_limits<T>::min)()
|
Chris@16
|
200 //
|
Chris@16
|
201 // Hull et al provide alternative code for "bad" machines
|
Chris@16
|
202 // but we have no way to test that here, so for now just assert
|
Chris@16
|
203 // on the assumption:
|
Chris@16
|
204 //
|
Chris@16
|
205 BOOST_ASSERT(x == 1);
|
Chris@16
|
206 real = std::sqrt(y);
|
Chris@16
|
207 imag = std::sqrt(y);
|
Chris@16
|
208 }
|
Chris@16
|
209 else if(std::numeric_limits<T>::epsilon() * y - one >= x)
|
Chris@16
|
210 {
|
Chris@16
|
211 real = half_pi;
|
Chris@16
|
212 imag = log_two + std::log(y);
|
Chris@16
|
213 }
|
Chris@16
|
214 else if(x > one)
|
Chris@16
|
215 {
|
Chris@16
|
216 real = std::atan(y/x);
|
Chris@16
|
217 T xoy = x/y;
|
Chris@16
|
218 imag = log_two + std::log(y) + half * boost::math::log1p(xoy*xoy);
|
Chris@16
|
219 }
|
Chris@16
|
220 else
|
Chris@16
|
221 {
|
Chris@16
|
222 real = half_pi;
|
Chris@16
|
223 T a = std::sqrt(one + y*y);
|
Chris@16
|
224 imag = half * boost::math::log1p(static_cast<T>(2)*y*(y+a));
|
Chris@16
|
225 }
|
Chris@16
|
226 }
|
Chris@16
|
227 }
|
Chris@16
|
228
|
Chris@16
|
229 //
|
Chris@16
|
230 // Finish off by working out the sign of the result:
|
Chris@16
|
231 //
|
Chris@16
|
232 if((boost::math::signbit)(z.real()))
|
Chris@16
|
233 real = s_pi - real;
|
Chris@16
|
234 if(!(boost::math::signbit)(z.imag()))
|
Chris@16
|
235 imag = (boost::math::changesign)(imag);
|
Chris@16
|
236
|
Chris@16
|
237 return std::complex<T>(real, imag);
|
Chris@16
|
238 #ifdef BOOST_MSVC
|
Chris@16
|
239 #pragma warning(pop)
|
Chris@16
|
240 #endif
|
Chris@16
|
241 }
|
Chris@16
|
242
|
Chris@16
|
243 } } // namespaces
|
Chris@16
|
244
|
Chris@16
|
245 #endif // BOOST_MATH_COMPLEX_ACOS_INCLUDED
|