Debian Codegraph @

rpython/rlib/rcomplex.py

raw symbols SCIP
1 import math
2 from math import fabs
3 from rpython.rlib.rfloat import asinh, log1p, isfinite
4 from rpython.rlib.constant import DBL_MIN, CM_SCALE_UP, CM_SCALE_DOWN
5 from rpython.rlib.constant import CM_LARGE_DOUBLE, DBL_MANT_DIG
6 from rpython.rlib.constant import M_LN2, M_LN10
7 from rpython.rlib.constant import CM_SQRT_LARGE_DOUBLE, CM_SQRT_DBL_MIN
8 from rpython.rlib.constant import CM_LOG_LARGE_DOUBLE
9 from rpython.rlib.special_value import special_type, INF, NAN
10 from rpython.rlib.special_value import sqrt_special_values
11 from rpython.rlib.special_value import acos_special_values
12 from rpython.rlib.special_value import acosh_special_values
13 from rpython.rlib.special_value import asinh_special_values
14 from rpython.rlib.special_value import atanh_special_values
15 from rpython.rlib.special_value import log_special_values
16 from rpython.rlib.special_value import exp_special_values
17 from rpython.rlib.special_value import cosh_special_values
18 from rpython.rlib.special_value import sinh_special_values
19 from rpython.rlib.special_value import tanh_special_values
20 from rpython.rlib.special_value import rect_special_values
21
22 #binary
23
24 def c_add(x, y):
25 (r1, i1), (r2, i2) = x, y
26 r = r1 + r2
27 i = i1 + i2
28 return (r, i)
29
30 def c_sub(x, y):
31 (r1, i1), (r2, i2) = x, y
32 r = r1 - r2
33 i = i1 - i2
34 return (r, i)
35
36 def c_mul(x, y):
37 (r1, i1), (r2, i2) = x, y
38 r = r1 * r2 - i1 * i2
39 i = r1 * i2 + i1 * r2
40 return (r, i)
41
42 def c_div(x, y): #x/y
43 (r1, i1), (r2, i2) = x, y
44 if r2 < 0:
45 abs_r2 = -r2
46 else:
47 abs_r2 = r2
48 if i2 < 0:
49 abs_i2 = -i2
50 else:
51 abs_i2 = i2
52 if abs_r2 >= abs_i2:
53 if abs_r2 == 0.0:
54 raise ZeroDivisionError
55 else:
56 ratio = i2 / r2
57 denom = r2 + i2 * ratio
58 rr = (r1 + i1 * ratio) / denom
59 ir = (i1 - r1 * ratio) / denom
60 elif math.isnan(r2):
61 rr = NAN
62 ir = NAN
63 else:
64 ratio = r2 / i2
65 denom = r2 * ratio + i2
66 assert i2 != 0.0
67 rr = (r1 * ratio + i1) / denom
68 ir = (i1 * ratio - r1) / denom
69 return (rr, ir)
70
71 def c_pow(x, y):
72 (r1, i1), (r2, i2) = x, y
73 if i1 == 0 and i2 == 0 and r1 > 0:
74 rr = math.pow(r1, r2)
75 ir = 0.
76 elif r2 == 0.0 and i2 == 0.0:
77 rr, ir = 1, 0
78 elif r1 == 1.0 and i1 == 0.0:
79 rr, ir = (1.0, 0.0)
80 elif r1 == 0.0 and i1 == 0.0:
81 if i2 != 0.0 or r2 < 0.0:
82 raise ZeroDivisionError
83 rr, ir = (0.0, 0.0)
84 else:
85 vabs = math.hypot(r1,i1)
86 len = math.pow(vabs,r2)
87 at = math.atan2(i1,r1)
88 phase = at * r2
89 if i2 != 0.0:
90 len /= math.exp(at * i2)
91 phase += i2 * math.log(vabs)
92 try:
93 rr = len * math.cos(phase)
94 ir = len * math.sin(phase)
95 except ValueError:
96 rr = NAN
97 ir = NAN
98 return (rr, ir)
99
100 #unary
101
102 def c_neg(r, i):
103 return (-r, -i)
104
105
106 def c_sqrt(x, y):
107 '''
108 Method: use symmetries to reduce to the case when x = z.real and y
109 = z.imag are nonnegative. Then the real part of the result is
110 given by
111
112 s = sqrt((x + hypot(x, y))/2)
113
114 and the imaginary part is
115
116 d = (y/2)/s
117
118 If either x or y is very large then there's a risk of overflow in
119 computation of the expression x + hypot(x, y). We can avoid this
120 by rewriting the formula for s as:
121
122 s = 2*sqrt(x/8 + hypot(x/8, y/8))
123
124 This costs us two extra multiplications/divisions, but avoids the
125 overhead of checking for x and y large.
126
127 If both x and y are subnormal then hypot(x, y) may also be
128 subnormal, so will lack full precision. We solve this by rescaling
129 x and y by a sufficiently large power of 2 to ensure that x and y
130 are normal.
131 '''
132 if not isfinite(x) or not isfinite(y):
133 return sqrt_special_values[special_type(x)][special_type(y)]
134
135 if x == 0. and y == 0.:
136 return (0., y)
137
138 ax = fabs(x)
139 ay = fabs(y)
140
141 if ax < DBL_MIN and ay < DBL_MIN and (ax > 0. or ay > 0.):
142 # here we catch cases where hypot(ax, ay) is subnormal
143 ax = math.ldexp(ax, CM_SCALE_UP)
144 ay1= math.ldexp(ay, CM_SCALE_UP)
145 s = math.ldexp(math.sqrt(ax + math.hypot(ax, ay1)),
146 CM_SCALE_DOWN)
147 else:
148 ax /= 8.
149 s = 2.*math.sqrt(ax + math.hypot(ax, ay/8.))
150
151 d = ay/(2.*s)
152
153 if x >= 0.:
154 return (s, math.copysign(d, y))
155 else:
156 return (d, math.copysign(s, y))
157
158
159
160 def c_acos(x, y):
161 if not isfinite(x) or not isfinite(y):
162 return acos_special_values[special_type(x)][special_type(y)]
163
164 if fabs(x) > CM_LARGE_DOUBLE or fabs(y) > CM_LARGE_DOUBLE:
165 # avoid unnecessary overflow for large arguments
166 real = math.atan2(fabs(y), x)
167 # split into cases to make sure that the branch cut has the
168 # correct continuity on systems with unsigned zeros
169 if x < 0.:
170 imag = -math.copysign(math.log(math.hypot(x/2., y/2.)) +
171 M_LN2*2., y)
172 else:
173 imag = math.copysign(math.log(math.hypot(x/2., y/2.)) +
174 M_LN2*2., -y)
175 else:
176 s1x, s1y = c_sqrt(1.-x, -y)
177 s2x, s2y = c_sqrt(1.+x, y)
178 real = 2.*math.atan2(s1x, s2x)
179 imag = asinh(s2x*s1y - s2y*s1x)
180 return (real, imag)
181
182
183 def c_acosh(x, y):
184 # XXX the following two lines seem unnecessary at least on Linux;
185 # the tests pass fine without them
186 if not isfinite(x) or not isfinite(y):
187 return acosh_special_values[special_type(x)][special_type(y)]
188
189 if fabs(x) > CM_LARGE_DOUBLE or fabs(y) > CM_LARGE_DOUBLE:
190 # avoid unnecessary overflow for large arguments
191 real = math.log(math.hypot(x/2., y/2.)) + M_LN2*2.
192 imag = math.atan2(y, x)
193 else:
194 s1x, s1y = c_sqrt(x - 1., y)
195 s2x, s2y = c_sqrt(x + 1., y)
196 real = asinh(s1x*s2x + s1y*s2y)
197 imag = 2.*math.atan2(s1y, s2x)
198 return (real, imag)
199
200
201 def c_asin(x, y):
202 # asin(z) = -i asinh(iz)
203 sx, sy = c_asinh(-y, x)
204 return (sy, -sx)
205
206
207 def c_asinh(x, y):
208 if not isfinite(x) or not isfinite(y):
209 return asinh_special_values[special_type(x)][special_type(y)]
210
211 if fabs(x) > CM_LARGE_DOUBLE or fabs(y) > CM_LARGE_DOUBLE:
212 if y >= 0.:
213 real = math.copysign(math.log(math.hypot(x/2., y/2.)) +
214 M_LN2*2., x)
215 else:
216 real = -math.copysign(math.log(math.hypot(x/2., y/2.)) +
217 M_LN2*2., -x)
218 imag = math.atan2(y, fabs(x))
219 else:
220 s1x, s1y = c_sqrt(1.+y, -x)
221 s2x, s2y = c_sqrt(1.-y, x)
222 real = asinh(s1x*s2y - s2x*s1y)
223 imag = math.atan2(y, s1x*s2x - s1y*s2y)
224 return (real, imag)
225
226
227 def c_atan(x, y):
228 # atan(z) = -i atanh(iz)
229 sx, sy = c_atanh(-y, x)
230 return (sy, -sx)
231
232
233 def c_atanh(x, y):
234 if not isfinite(x) or not isfinite(y):
235 return atanh_special_values[special_type(x)][special_type(y)]
236
237 # Reduce to case where x >= 0., using atanh(z) = -atanh(-z).
238 if x < 0.:
239 return c_neg(*c_atanh(*c_neg(x, y)))
240
241 ay = fabs(y)
242 if x > CM_SQRT_LARGE_DOUBLE or ay > CM_SQRT_LARGE_DOUBLE:
243 # if abs(z) is large then we use the approximation
244 # atanh(z) ~ 1/z +/- i*pi/2 (+/- depending on the sign
245 # of y
246 h = math.hypot(x/2., y/2.) # safe from overflow
247 real = x/4./h/h
248 # the two negations in the next line cancel each other out
249 # except when working with unsigned zeros: they're there to
250 # ensure that the branch cut has the correct continuity on
251 # systems that don't support signed zeros
252 imag = -math.copysign(math.pi/2., -y)
253 elif x == 1. and ay < CM_SQRT_DBL_MIN:
254 # C99 standard says: atanh(1+/-0.) should be inf +/- 0i
255 if ay == 0.:
256 raise ValueError("math domain error")
257 #real = INF
258 #imag = y
259 else:
260 real = -math.log(math.sqrt(ay)/math.sqrt(math.hypot(ay, 2.)))
261 imag = math.copysign(math.atan2(2., -ay) / 2, y)
262 else:
263 real = log1p(4.*x/((1-x)*(1-x) + ay*ay))/4.
264 imag = -math.atan2(-2.*y, (1-x)*(1+x) - ay*ay) / 2.
265 return (real, imag)
266
267
268 def c_log(x, y):
269 # The usual formula for the real part is log(hypot(z.real, z.imag)).
270 # There are four situations where this formula is potentially
271 # problematic:
272 #
273 # (1) the absolute value of z is subnormal. Then hypot is subnormal,
274 # so has fewer than the usual number of bits of accuracy, hence may
275 # have large relative error. This then gives a large absolute error
276 # in the log. This can be solved by rescaling z by a suitable power
277 # of 2.
278 #
279 # (2) the absolute value of z is greater than DBL_MAX (e.g. when both
280 # z.real and z.imag are within a factor of 1/sqrt(2) of DBL_MAX)
281 # Again, rescaling solves this.
282 #
283 # (3) the absolute value of z is close to 1. In this case it's
284 # difficult to achieve good accuracy, at least in part because a
285 # change of 1ulp in the real or imaginary part of z can result in a
286 # change of billions of ulps in the correctly rounded answer.
287 #
288 # (4) z = 0. The simplest thing to do here is to call the
289 # floating-point log with an argument of 0, and let its behaviour
290 # (returning -infinity, signaling a floating-point exception, setting
291 # errno, or whatever) determine that of c_log. So the usual formula
292 # is fine here.
293
294 # XXX the following two lines seem unnecessary at least on Linux;
295 # the tests pass fine without them
296 if not isfinite(x) or not isfinite(y):
297 return log_special_values[special_type(x)][special_type(y)]
298
299 ax = fabs(x)
300 ay = fabs(y)
301
302 if ax > CM_LARGE_DOUBLE or ay > CM_LARGE_DOUBLE:
303 real = math.log(math.hypot(ax/2., ay/2.)) + M_LN2
304 elif ax < DBL_MIN and ay < DBL_MIN:
305 if ax > 0. or ay > 0.:
306 # catch cases where hypot(ax, ay) is subnormal
307 real = math.log(math.hypot(math.ldexp(ax, DBL_MANT_DIG),
308 math.ldexp(ay, DBL_MANT_DIG)))
309 real -= DBL_MANT_DIG*M_LN2
310 else:
311 # log(+/-0. +/- 0i)
312 raise ValueError("math domain error")
313 #real = -INF
314 #imag = atan2(y, x)
315 else:
316 h = math.hypot(ax, ay)
317 if 0.71 <= h and h <= 1.73:
318 am = max(ax, ay)
319 an = min(ax, ay)
320 real = log1p((am-1)*(am+1) + an*an) / 2.
321 else:
322 real = math.log(h)
323 imag = math.atan2(y, x)
324 return (real, imag)
325
326
327 def c_log10(x, y):
328 rx, ry = c_log(x, y)
329 return (rx / M_LN10, ry / M_LN10)
330
331 def c_exp(x, y):
332 if not isfinite(x) or not isfinite(y):
333 if math.isinf(x) and isfinite(y) and y != 0.:
334 if x > 0:
335 real = math.copysign(INF, math.cos(y))
336 imag = math.copysign(INF, math.sin(y))
337 else:
338 real = math.copysign(0., math.cos(y))
339 imag = math.copysign(0., math.sin(y))
340 r = (real, imag)
341 else:
342 r = exp_special_values[special_type(x)][special_type(y)]
343
344 # need to raise ValueError if y is +/- infinity and x is not
345 # a NaN and not -infinity
346 if math.isinf(y) and (isfinite(x) or (math.isinf(x) and x > 0)):
347 raise ValueError("math domain error")
348 return r
349
350 if x > CM_LOG_LARGE_DOUBLE:
351 l = math.exp(x-1.)
352 real = l * math.cos(y) * math.e
353 imag = l * math.sin(y) * math.e
354 else:
355 l = math.exp(x)
356 real = l * math.cos(y)
357 imag = l * math.sin(y)
358 if math.isinf(real) or math.isinf(imag):
359 raise OverflowError("math range error")
360 return real, imag
361
362
363 def c_cosh(x, y):
364 if not isfinite(x) or not isfinite(y):
365 if math.isinf(x) and isfinite(y) and y != 0.:
366 if x > 0:
367 real = math.copysign(INF, math.cos(y))
368 imag = math.copysign(INF, math.sin(y))
369 else:
370 real = math.copysign(INF, math.cos(y))
371 imag = -math.copysign(INF, math.sin(y))
372 r = (real, imag)
373 else:
374 r = cosh_special_values[special_type(x)][special_type(y)]
375
376 # need to raise ValueError if y is +/- infinity and x is not
377 # a NaN
378 if math.isinf(y) and not math.isnan(x):
379 raise ValueError("math domain error")
380 return r
381
382 if fabs(x) > CM_LOG_LARGE_DOUBLE:
383 # deal correctly with cases where cosh(x) overflows but
384 # cosh(z) does not.
385 x_minus_one = x - math.copysign(1., x)
386 real = math.cos(y) * math.cosh(x_minus_one) * math.e
387 imag = math.sin(y) * math.sinh(x_minus_one) * math.e
388 else:
389 real = math.cos(y) * math.cosh(x)
390 imag = math.sin(y) * math.sinh(x)
391 if math.isinf(real) or math.isinf(imag):
392 raise OverflowError("math range error")
393 return real, imag
394
395
396 def c_sinh(x, y):
397 # special treatment for sinh(+/-inf + iy) if y is finite and nonzero
398 if not isfinite(x) or not isfinite(y):
399 if math.isinf(x) and isfinite(y) and y != 0.:
400 if x > 0:
401 real = math.copysign(INF, math.cos(y))
402 imag = math.copysign(INF, math.sin(y))
403 else:
404 real = -math.copysign(INF, math.cos(y))
405 imag = math.copysign(INF, math.sin(y))
406 r = (real, imag)
407 else:
408 r = sinh_special_values[special_type(x)][special_type(y)]
409
410 # need to raise ValueError if y is +/- infinity and x is not
411 # a NaN
412 if math.isinf(y) and not math.isnan(x):
413 raise ValueError("math domain error")
414 return r
415
416 if fabs(x) > CM_LOG_LARGE_DOUBLE:
417 x_minus_one = x - math.copysign(1., x)
418 real = math.cos(y) * math.sinh(x_minus_one) * math.e
419 imag = math.sin(y) * math.cosh(x_minus_one) * math.e
420 else:
421 real = math.cos(y) * math.sinh(x)
422 imag = math.sin(y) * math.cosh(x)
423 if math.isinf(real) or math.isinf(imag):
424 raise OverflowError("math range error")
425 return real, imag
426
427
428 def c_tanh(x, y):
429 # Formula:
430 #
431 # tanh(x+iy) = (tanh(x)(1+tan(y)^2) + i tan(y)(1-tanh(x))^2) /
432 # (1+tan(y)^2 tanh(x)^2)
433 #
434 # To avoid excessive roundoff error, 1-tanh(x)^2 is better computed
435 # as 1/cosh(x)^2. When abs(x) is large, we approximate 1-tanh(x)^2
436 # by 4 exp(-2*x) instead, to avoid possible overflow in the
437 # computation of cosh(x).
438
439 if not isfinite(x) or not isfinite(y):
440 if math.isinf(x) and isfinite(y) and y != 0.:
441 if x > 0:
442 real = 1.0 # vv XXX why is the 2. there?
443 imag = math.copysign(0., 2. * math.sin(y) * math.cos(y))
444 else:
445 real = -1.0
446 imag = math.copysign(0., 2. * math.sin(y) * math.cos(y))
447 r = (real, imag)
448 else:
449 r = tanh_special_values[special_type(x)][special_type(y)]
450
451 # need to raise ValueError if y is +/-infinity and x is finite
452 if math.isinf(y) and isfinite(x):
453 raise ValueError("math domain error")
454 return r
455
456 if fabs(x) > CM_LOG_LARGE_DOUBLE:
457 real = math.copysign(1., x)
458 imag = 4. * math.sin(y) * math.cos(y) * math.exp(-2.*fabs(x))
459 else:
460 tx = math.tanh(x)
461 ty = math.tan(y)
462 cx = 1. / math.cosh(x)
463 txty = tx * ty
464 denom = 1. + txty * txty
465 real = tx * (1. + ty*ty) / denom
466 imag = ((ty / denom) * cx) * cx
467 return real, imag
468
469
470 def c_cos(r, i):
471 # cos(z) = cosh(iz)
472 return c_cosh(-i, r)
473
474 def c_sin(r, i):
475 # sin(z) = -i sinh(iz)
476 sr, si = c_sinh(-i, r)
477 return si, -sr
478
479 def c_tan(r, i):
480 # tan(z) = -i tanh(iz)
481 sr, si = c_tanh(-i, r)
482 return si, -sr
483
484
485 def c_rect(r, phi):
486 if not isfinite(r) or not isfinite(phi):
487 # if r is +/-infinity and phi is finite but nonzero then
488 # result is (+-INF +-INF i), but we need to compute cos(phi)
489 # and sin(phi) to figure out the signs.
490 if math.isinf(r) and isfinite(phi) and phi != 0.:
491 if r > 0:
492 real = math.copysign(INF, math.cos(phi))
493 imag = math.copysign(INF, math.sin(phi))
494 else:
495 real = -math.copysign(INF, math.cos(phi))
496 imag = -math.copysign(INF, math.sin(phi))
497 z = (real, imag)
498 else:
499 z = rect_special_values[special_type(r)][special_type(phi)]
500
501 # need to raise ValueError if r is a nonzero number and phi
502 # is infinite
503 if r != 0. and not math.isnan(r) and math.isinf(phi):
504 raise ValueError("math domain error")
505 return z
506
507 real = r * math.cos(phi)
508 imag = r * math.sin(phi)
509 return real, imag
510
511
512 def c_phase(x, y):
513 # Windows screws up atan2 for inf and nan, and alpha Tru64 5.1 doesn't
514 # follow C99 for atan2(0., 0.).
515 if math.isnan(x) or math.isnan(y):
516 return NAN
517 if math.isinf(y):
518 if math.isinf(x):
519 if math.copysign(1., x) == 1.:
520 # atan2(+-inf, +inf) == +-pi/4
521 return math.copysign(0.25 * math.pi, y)
522 else:
523 # atan2(+-inf, -inf) == +-pi*3/4
524 return math.copysign(0.75 * math.pi, y)
525 # atan2(+-inf, x) == +-pi/2 for finite x
526 return math.copysign(0.5 * math.pi, y)
527 if math.isinf(x) or y == 0.:
528 if math.copysign(1., x) == 1.:
529 # atan2(+-y, +inf) = atan2(+-0, +x) = +-0.
530 return math.copysign(0., y)
531 else:
532 # atan2(+-y, -inf) = atan2(+-0., -x) = +-pi.
533 return math.copysign(math.pi, y)
534 return math.atan2(y, x)
535
536
537 def c_abs(r, i):
538 if not isfinite(r) or not isfinite(i):
539 # C99 rules: if either the real or the imaginary part is an
540 # infinity, return infinity, even if the other part is a NaN.
541 if math.isinf(r):
542 return INF
543 if math.isinf(i):
544 return INF
545
546 # either the real or imaginary part is a NaN,
547 # and neither is infinite. Result should be NaN.
548 return NAN
549
550 result = math.hypot(r, i)
551 if not isfinite(result):
552 raise OverflowError("math range error")
553 return result
554
555
556 def c_polar(r, i):
557 real = c_abs(r, i)
558 phi = c_phase(r, i)
559 return real, phi
560
561
562 def c_isinf(r, i):
563 return math.isinf(r) or math.isinf(i)
564
565
566 def c_isnan(r, i):
567 return math.isnan(r) or math.isnan(i)
568
569
570 def c_isfinite(r, i):
571 return isfinite(r) and isfinite(i)