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_ASIN_INCLUDED
|
Chris@16
|
6 #define BOOST_MATH_COMPLEX_ASIN_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 inline std::complex<T> asin(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 the value of 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 #ifdef BOOST_MSVC
|
Chris@16
|
47 #pragma warning(push)
|
Chris@16
|
48 #pragma warning(disable:4127)
|
Chris@16
|
49 #endif
|
Chris@16
|
50 //
|
Chris@16
|
51 // Get real and imaginary parts, discard the signs as we can
|
Chris@16
|
52 // figure out the sign of the result later:
|
Chris@16
|
53 //
|
Chris@16
|
54 T x = std::fabs(z.real());
|
Chris@16
|
55 T y = std::fabs(z.imag());
|
Chris@16
|
56 T real, imag; // our results
|
Chris@16
|
57
|
Chris@16
|
58 //
|
Chris@16
|
59 // Begin by handling the special cases for infinities and nan's
|
Chris@16
|
60 // specified in C99, most of this is handled by the regular logic
|
Chris@16
|
61 // below, but handling it as a special case prevents overflow/underflow
|
Chris@16
|
62 // arithmetic which may trip up some machines:
|
Chris@16
|
63 //
|
Chris@16
|
64 if((boost::math::isnan)(x))
|
Chris@16
|
65 {
|
Chris@16
|
66 if((boost::math::isnan)(y))
|
Chris@16
|
67 return std::complex<T>(x, x);
|
Chris@16
|
68 if((boost::math::isinf)(y))
|
Chris@16
|
69 {
|
Chris@16
|
70 real = x;
|
Chris@16
|
71 imag = std::numeric_limits<T>::infinity();
|
Chris@16
|
72 }
|
Chris@16
|
73 else
|
Chris@16
|
74 return std::complex<T>(x, x);
|
Chris@16
|
75 }
|
Chris@16
|
76 else if((boost::math::isnan)(y))
|
Chris@16
|
77 {
|
Chris@16
|
78 if(x == 0)
|
Chris@16
|
79 {
|
Chris@16
|
80 real = 0;
|
Chris@16
|
81 imag = y;
|
Chris@16
|
82 }
|
Chris@16
|
83 else if((boost::math::isinf)(x))
|
Chris@16
|
84 {
|
Chris@16
|
85 real = y;
|
Chris@16
|
86 imag = std::numeric_limits<T>::infinity();
|
Chris@16
|
87 }
|
Chris@16
|
88 else
|
Chris@16
|
89 return std::complex<T>(y, y);
|
Chris@16
|
90 }
|
Chris@16
|
91 else if((boost::math::isinf)(x))
|
Chris@16
|
92 {
|
Chris@16
|
93 if((boost::math::isinf)(y))
|
Chris@16
|
94 {
|
Chris@16
|
95 real = quarter_pi;
|
Chris@16
|
96 imag = std::numeric_limits<T>::infinity();
|
Chris@16
|
97 }
|
Chris@16
|
98 else
|
Chris@16
|
99 {
|
Chris@16
|
100 real = half_pi;
|
Chris@16
|
101 imag = std::numeric_limits<T>::infinity();
|
Chris@16
|
102 }
|
Chris@16
|
103 }
|
Chris@16
|
104 else if((boost::math::isinf)(y))
|
Chris@16
|
105 {
|
Chris@16
|
106 real = 0;
|
Chris@16
|
107 imag = std::numeric_limits<T>::infinity();
|
Chris@16
|
108 }
|
Chris@16
|
109 else
|
Chris@16
|
110 {
|
Chris@16
|
111 //
|
Chris@16
|
112 // special case for real numbers:
|
Chris@16
|
113 //
|
Chris@16
|
114 if((y == 0) && (x <= one))
|
Chris@16
|
115 return std::complex<T>(std::asin(z.real()), z.imag());
|
Chris@16
|
116 //
|
Chris@16
|
117 // Figure out if our input is within the "safe area" identified by Hull et al.
|
Chris@16
|
118 // This would be more efficient with portable floating point exception handling;
|
Chris@16
|
119 // fortunately the quantities M and u identified by Hull et al (figure 3),
|
Chris@16
|
120 // match with the max and min methods of numeric_limits<T>.
|
Chris@16
|
121 //
|
Chris@16
|
122 T safe_max = detail::safe_max(static_cast<T>(8));
|
Chris@16
|
123 T safe_min = detail::safe_min(static_cast<T>(4));
|
Chris@16
|
124
|
Chris@16
|
125 T xp1 = one + x;
|
Chris@16
|
126 T xm1 = x - one;
|
Chris@16
|
127
|
Chris@16
|
128 if((x < safe_max) && (x > safe_min) && (y < safe_max) && (y > safe_min))
|
Chris@16
|
129 {
|
Chris@16
|
130 T yy = y * y;
|
Chris@16
|
131 T r = std::sqrt(xp1*xp1 + yy);
|
Chris@16
|
132 T s = std::sqrt(xm1*xm1 + yy);
|
Chris@16
|
133 T a = half * (r + s);
|
Chris@16
|
134 T b = x / a;
|
Chris@16
|
135
|
Chris@16
|
136 if(b <= b_crossover)
|
Chris@16
|
137 {
|
Chris@16
|
138 real = std::asin(b);
|
Chris@16
|
139 }
|
Chris@16
|
140 else
|
Chris@16
|
141 {
|
Chris@16
|
142 T apx = a + x;
|
Chris@16
|
143 if(x <= one)
|
Chris@16
|
144 {
|
Chris@16
|
145 real = std::atan(x/std::sqrt(half * apx * (yy /(r + xp1) + (s-xm1))));
|
Chris@16
|
146 }
|
Chris@16
|
147 else
|
Chris@16
|
148 {
|
Chris@16
|
149 real = std::atan(x/(y * std::sqrt(half * (apx/(r + xp1) + apx/(s+xm1)))));
|
Chris@16
|
150 }
|
Chris@16
|
151 }
|
Chris@16
|
152
|
Chris@16
|
153 if(a <= a_crossover)
|
Chris@16
|
154 {
|
Chris@16
|
155 T am1;
|
Chris@16
|
156 if(x < one)
|
Chris@16
|
157 {
|
Chris@16
|
158 am1 = half * (yy/(r + xp1) + yy/(s - xm1));
|
Chris@16
|
159 }
|
Chris@16
|
160 else
|
Chris@16
|
161 {
|
Chris@16
|
162 am1 = half * (yy/(r + xp1) + (s + xm1));
|
Chris@16
|
163 }
|
Chris@16
|
164 imag = boost::math::log1p(am1 + std::sqrt(am1 * (a + one)));
|
Chris@16
|
165 }
|
Chris@16
|
166 else
|
Chris@16
|
167 {
|
Chris@16
|
168 imag = std::log(a + std::sqrt(a*a - one));
|
Chris@16
|
169 }
|
Chris@16
|
170 }
|
Chris@16
|
171 else
|
Chris@16
|
172 {
|
Chris@16
|
173 //
|
Chris@16
|
174 // This is the Hull et al exception handling code from Fig 3 of their paper:
|
Chris@16
|
175 //
|
Chris@16
|
176 if(y <= (std::numeric_limits<T>::epsilon() * std::fabs(xm1)))
|
Chris@16
|
177 {
|
Chris@16
|
178 if(x < one)
|
Chris@16
|
179 {
|
Chris@16
|
180 real = std::asin(x);
|
Chris@16
|
181 imag = y / std::sqrt(-xp1*xm1);
|
Chris@16
|
182 }
|
Chris@16
|
183 else
|
Chris@16
|
184 {
|
Chris@16
|
185 real = half_pi;
|
Chris@16
|
186 if(((std::numeric_limits<T>::max)() / xp1) > xm1)
|
Chris@16
|
187 {
|
Chris@16
|
188 // xp1 * xm1 won't overflow:
|
Chris@16
|
189 imag = boost::math::log1p(xm1 + std::sqrt(xp1*xm1));
|
Chris@16
|
190 }
|
Chris@16
|
191 else
|
Chris@16
|
192 {
|
Chris@16
|
193 imag = log_two + std::log(x);
|
Chris@16
|
194 }
|
Chris@16
|
195 }
|
Chris@16
|
196 }
|
Chris@16
|
197 else if(y <= safe_min)
|
Chris@16
|
198 {
|
Chris@16
|
199 // There is an assumption in Hull et al's analysis that
|
Chris@16
|
200 // if we get here then x == 1. This is true for all "good"
|
Chris@16
|
201 // machines where :
|
Chris@16
|
202 //
|
Chris@16
|
203 // E^2 > 8*sqrt(u); with:
|
Chris@16
|
204 //
|
Chris@16
|
205 // E = std::numeric_limits<T>::epsilon()
|
Chris@16
|
206 // u = (std::numeric_limits<T>::min)()
|
Chris@16
|
207 //
|
Chris@16
|
208 // Hull et al provide alternative code for "bad" machines
|
Chris@16
|
209 // but we have no way to test that here, so for now just assert
|
Chris@16
|
210 // on the assumption:
|
Chris@16
|
211 //
|
Chris@16
|
212 BOOST_ASSERT(x == 1);
|
Chris@16
|
213 real = half_pi - std::sqrt(y);
|
Chris@16
|
214 imag = std::sqrt(y);
|
Chris@16
|
215 }
|
Chris@16
|
216 else if(std::numeric_limits<T>::epsilon() * y - one >= x)
|
Chris@16
|
217 {
|
Chris@16
|
218 real = x/y; // This can underflow!
|
Chris@16
|
219 imag = log_two + std::log(y);
|
Chris@16
|
220 }
|
Chris@16
|
221 else if(x > one)
|
Chris@16
|
222 {
|
Chris@16
|
223 real = std::atan(x/y);
|
Chris@16
|
224 T xoy = x/y;
|
Chris@16
|
225 imag = log_two + std::log(y) + half * boost::math::log1p(xoy*xoy);
|
Chris@16
|
226 }
|
Chris@16
|
227 else
|
Chris@16
|
228 {
|
Chris@16
|
229 T a = std::sqrt(one + y*y);
|
Chris@16
|
230 real = x/a; // This can underflow!
|
Chris@16
|
231 imag = half * boost::math::log1p(static_cast<T>(2)*y*(y+a));
|
Chris@16
|
232 }
|
Chris@16
|
233 }
|
Chris@16
|
234 }
|
Chris@16
|
235
|
Chris@16
|
236 //
|
Chris@16
|
237 // Finish off by working out the sign of the result:
|
Chris@16
|
238 //
|
Chris@16
|
239 if((boost::math::signbit)(z.real()))
|
Chris@16
|
240 real = (boost::math::changesign)(real);
|
Chris@16
|
241 if((boost::math::signbit)(z.imag()))
|
Chris@16
|
242 imag = (boost::math::changesign)(imag);
|
Chris@16
|
243
|
Chris@16
|
244 return std::complex<T>(real, imag);
|
Chris@16
|
245 #ifdef BOOST_MSVC
|
Chris@16
|
246 #pragma warning(pop)
|
Chris@16
|
247 #endif
|
Chris@16
|
248 }
|
Chris@16
|
249
|
Chris@16
|
250 } } // namespaces
|
Chris@16
|
251
|
Chris@16
|
252 #endif // BOOST_MATH_COMPLEX_ASIN_INCLUDED
|