1 | n/a | /* Complex math module */ |
---|
2 | n/a | |
---|
3 | n/a | /* much code borrowed from mathmodule.c */ |
---|
4 | n/a | |
---|
5 | n/a | #include "Python.h" |
---|
6 | n/a | #include "_math.h" |
---|
7 | n/a | /* we need DBL_MAX, DBL_MIN, DBL_EPSILON, DBL_MANT_DIG and FLT_RADIX from |
---|
8 | n/a | float.h. We assume that FLT_RADIX is either 2 or 16. */ |
---|
9 | n/a | #include <float.h> |
---|
10 | n/a | |
---|
11 | n/a | #include "clinic/cmathmodule.c.h" |
---|
12 | n/a | /*[clinic input] |
---|
13 | n/a | module cmath |
---|
14 | n/a | [clinic start generated code]*/ |
---|
15 | n/a | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=308d6839f4a46333]*/ |
---|
16 | n/a | |
---|
17 | n/a | /*[python input] |
---|
18 | n/a | class Py_complex_protected_converter(Py_complex_converter): |
---|
19 | n/a | def modify(self): |
---|
20 | n/a | return 'errno = 0; PyFPE_START_PROTECT("complex function", goto exit);' |
---|
21 | n/a | |
---|
22 | n/a | |
---|
23 | n/a | class Py_complex_protected_return_converter(CReturnConverter): |
---|
24 | n/a | type = "Py_complex" |
---|
25 | n/a | |
---|
26 | n/a | def render(self, function, data): |
---|
27 | n/a | self.declare(data) |
---|
28 | n/a | data.return_conversion.append(""" |
---|
29 | n/a | PyFPE_END_PROTECT(_return_value); |
---|
30 | n/a | if (errno == EDOM) { |
---|
31 | n/a | PyErr_SetString(PyExc_ValueError, "math domain error"); |
---|
32 | n/a | goto exit; |
---|
33 | n/a | } |
---|
34 | n/a | else if (errno == ERANGE) { |
---|
35 | n/a | PyErr_SetString(PyExc_OverflowError, "math range error"); |
---|
36 | n/a | goto exit; |
---|
37 | n/a | } |
---|
38 | n/a | else { |
---|
39 | n/a | return_value = PyComplex_FromCComplex(_return_value); |
---|
40 | n/a | } |
---|
41 | n/a | """.strip()) |
---|
42 | n/a | [python start generated code]*/ |
---|
43 | n/a | /*[python end generated code: output=da39a3ee5e6b4b0d input=345daa075b1028e7]*/ |
---|
44 | n/a | |
---|
45 | n/a | #if (FLT_RADIX != 2 && FLT_RADIX != 16) |
---|
46 | n/a | #error "Modules/cmathmodule.c expects FLT_RADIX to be 2 or 16" |
---|
47 | n/a | #endif |
---|
48 | n/a | |
---|
49 | n/a | #ifndef M_LN2 |
---|
50 | n/a | #define M_LN2 (0.6931471805599453094) /* natural log of 2 */ |
---|
51 | n/a | #endif |
---|
52 | n/a | |
---|
53 | n/a | #ifndef M_LN10 |
---|
54 | n/a | #define M_LN10 (2.302585092994045684) /* natural log of 10 */ |
---|
55 | n/a | #endif |
---|
56 | n/a | |
---|
57 | n/a | /* |
---|
58 | n/a | CM_LARGE_DOUBLE is used to avoid spurious overflow in the sqrt, log, |
---|
59 | n/a | inverse trig and inverse hyperbolic trig functions. Its log is used in the |
---|
60 | n/a | evaluation of exp, cos, cosh, sin, sinh, tan, and tanh to avoid unnecessary |
---|
61 | n/a | overflow. |
---|
62 | n/a | */ |
---|
63 | n/a | |
---|
64 | n/a | #define CM_LARGE_DOUBLE (DBL_MAX/4.) |
---|
65 | n/a | #define CM_SQRT_LARGE_DOUBLE (sqrt(CM_LARGE_DOUBLE)) |
---|
66 | n/a | #define CM_LOG_LARGE_DOUBLE (log(CM_LARGE_DOUBLE)) |
---|
67 | n/a | #define CM_SQRT_DBL_MIN (sqrt(DBL_MIN)) |
---|
68 | n/a | |
---|
69 | n/a | /* |
---|
70 | n/a | CM_SCALE_UP is an odd integer chosen such that multiplication by |
---|
71 | n/a | 2**CM_SCALE_UP is sufficient to turn a subnormal into a normal. |
---|
72 | n/a | CM_SCALE_DOWN is (-(CM_SCALE_UP+1)/2). These scalings are used to compute |
---|
73 | n/a | square roots accurately when the real and imaginary parts of the argument |
---|
74 | n/a | are subnormal. |
---|
75 | n/a | */ |
---|
76 | n/a | |
---|
77 | n/a | #if FLT_RADIX==2 |
---|
78 | n/a | #define CM_SCALE_UP (2*(DBL_MANT_DIG/2) + 1) |
---|
79 | n/a | #elif FLT_RADIX==16 |
---|
80 | n/a | #define CM_SCALE_UP (4*DBL_MANT_DIG+1) |
---|
81 | n/a | #endif |
---|
82 | n/a | #define CM_SCALE_DOWN (-(CM_SCALE_UP+1)/2) |
---|
83 | n/a | |
---|
84 | n/a | /* Constants cmath.inf, cmath.infj, cmath.nan, cmath.nanj. |
---|
85 | n/a | cmath.nan and cmath.nanj are defined only when either |
---|
86 | n/a | PY_NO_SHORT_FLOAT_REPR is *not* defined (which should be |
---|
87 | n/a | the most common situation on machines using an IEEE 754 |
---|
88 | n/a | representation), or Py_NAN is defined. */ |
---|
89 | n/a | |
---|
90 | n/a | static double |
---|
91 | n/a | m_inf(void) |
---|
92 | n/a | { |
---|
93 | n/a | #ifndef PY_NO_SHORT_FLOAT_REPR |
---|
94 | n/a | return _Py_dg_infinity(0); |
---|
95 | n/a | #else |
---|
96 | n/a | return Py_HUGE_VAL; |
---|
97 | n/a | #endif |
---|
98 | n/a | } |
---|
99 | n/a | |
---|
100 | n/a | static Py_complex |
---|
101 | n/a | c_infj(void) |
---|
102 | n/a | { |
---|
103 | n/a | Py_complex r; |
---|
104 | n/a | r.real = 0.0; |
---|
105 | n/a | r.imag = m_inf(); |
---|
106 | n/a | return r; |
---|
107 | n/a | } |
---|
108 | n/a | |
---|
109 | n/a | #if !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN) |
---|
110 | n/a | |
---|
111 | n/a | static double |
---|
112 | n/a | m_nan(void) |
---|
113 | n/a | { |
---|
114 | n/a | #ifndef PY_NO_SHORT_FLOAT_REPR |
---|
115 | n/a | return _Py_dg_stdnan(0); |
---|
116 | n/a | #else |
---|
117 | n/a | return Py_NAN; |
---|
118 | n/a | #endif |
---|
119 | n/a | } |
---|
120 | n/a | |
---|
121 | n/a | static Py_complex |
---|
122 | n/a | c_nanj(void) |
---|
123 | n/a | { |
---|
124 | n/a | Py_complex r; |
---|
125 | n/a | r.real = 0.0; |
---|
126 | n/a | r.imag = m_nan(); |
---|
127 | n/a | return r; |
---|
128 | n/a | } |
---|
129 | n/a | |
---|
130 | n/a | #endif |
---|
131 | n/a | |
---|
132 | n/a | /* forward declarations */ |
---|
133 | n/a | static Py_complex cmath_asinh_impl(PyObject *, Py_complex); |
---|
134 | n/a | static Py_complex cmath_atanh_impl(PyObject *, Py_complex); |
---|
135 | n/a | static Py_complex cmath_cosh_impl(PyObject *, Py_complex); |
---|
136 | n/a | static Py_complex cmath_sinh_impl(PyObject *, Py_complex); |
---|
137 | n/a | static Py_complex cmath_sqrt_impl(PyObject *, Py_complex); |
---|
138 | n/a | static Py_complex cmath_tanh_impl(PyObject *, Py_complex); |
---|
139 | n/a | static PyObject * math_error(void); |
---|
140 | n/a | |
---|
141 | n/a | /* Code to deal with special values (infinities, NaNs, etc.). */ |
---|
142 | n/a | |
---|
143 | n/a | /* special_type takes a double and returns an integer code indicating |
---|
144 | n/a | the type of the double as follows: |
---|
145 | n/a | */ |
---|
146 | n/a | |
---|
147 | n/a | enum special_types { |
---|
148 | n/a | ST_NINF, /* 0, negative infinity */ |
---|
149 | n/a | ST_NEG, /* 1, negative finite number (nonzero) */ |
---|
150 | n/a | ST_NZERO, /* 2, -0. */ |
---|
151 | n/a | ST_PZERO, /* 3, +0. */ |
---|
152 | n/a | ST_POS, /* 4, positive finite number (nonzero) */ |
---|
153 | n/a | ST_PINF, /* 5, positive infinity */ |
---|
154 | n/a | ST_NAN /* 6, Not a Number */ |
---|
155 | n/a | }; |
---|
156 | n/a | |
---|
157 | n/a | static enum special_types |
---|
158 | n/a | special_type(double d) |
---|
159 | n/a | { |
---|
160 | n/a | if (Py_IS_FINITE(d)) { |
---|
161 | n/a | if (d != 0) { |
---|
162 | n/a | if (copysign(1., d) == 1.) |
---|
163 | n/a | return ST_POS; |
---|
164 | n/a | else |
---|
165 | n/a | return ST_NEG; |
---|
166 | n/a | } |
---|
167 | n/a | else { |
---|
168 | n/a | if (copysign(1., d) == 1.) |
---|
169 | n/a | return ST_PZERO; |
---|
170 | n/a | else |
---|
171 | n/a | return ST_NZERO; |
---|
172 | n/a | } |
---|
173 | n/a | } |
---|
174 | n/a | if (Py_IS_NAN(d)) |
---|
175 | n/a | return ST_NAN; |
---|
176 | n/a | if (copysign(1., d) == 1.) |
---|
177 | n/a | return ST_PINF; |
---|
178 | n/a | else |
---|
179 | n/a | return ST_NINF; |
---|
180 | n/a | } |
---|
181 | n/a | |
---|
182 | n/a | #define SPECIAL_VALUE(z, table) \ |
---|
183 | n/a | if (!Py_IS_FINITE((z).real) || !Py_IS_FINITE((z).imag)) { \ |
---|
184 | n/a | errno = 0; \ |
---|
185 | n/a | return table[special_type((z).real)] \ |
---|
186 | n/a | [special_type((z).imag)]; \ |
---|
187 | n/a | } |
---|
188 | n/a | |
---|
189 | n/a | #define P Py_MATH_PI |
---|
190 | n/a | #define P14 0.25*Py_MATH_PI |
---|
191 | n/a | #define P12 0.5*Py_MATH_PI |
---|
192 | n/a | #define P34 0.75*Py_MATH_PI |
---|
193 | n/a | #define INF Py_HUGE_VAL |
---|
194 | n/a | #define N Py_NAN |
---|
195 | n/a | #define U -9.5426319407711027e33 /* unlikely value, used as placeholder */ |
---|
196 | n/a | |
---|
197 | n/a | /* First, the C functions that do the real work. Each of the c_* |
---|
198 | n/a | functions computes and returns the C99 Annex G recommended result |
---|
199 | n/a | and also sets errno as follows: errno = 0 if no floating-point |
---|
200 | n/a | exception is associated with the result; errno = EDOM if C99 Annex |
---|
201 | n/a | G recommends raising divide-by-zero or invalid for this result; and |
---|
202 | n/a | errno = ERANGE where the overflow floating-point signal should be |
---|
203 | n/a | raised. |
---|
204 | n/a | */ |
---|
205 | n/a | |
---|
206 | n/a | static Py_complex acos_special_values[7][7]; |
---|
207 | n/a | |
---|
208 | n/a | /*[clinic input] |
---|
209 | n/a | cmath.acos -> Py_complex_protected |
---|
210 | n/a | |
---|
211 | n/a | z: Py_complex_protected |
---|
212 | n/a | / |
---|
213 | n/a | |
---|
214 | n/a | Return the arc cosine of z. |
---|
215 | n/a | [clinic start generated code]*/ |
---|
216 | n/a | |
---|
217 | n/a | static Py_complex |
---|
218 | n/a | cmath_acos_impl(PyObject *module, Py_complex z) |
---|
219 | n/a | /*[clinic end generated code: output=40bd42853fd460ae input=bd6cbd78ae851927]*/ |
---|
220 | n/a | { |
---|
221 | n/a | Py_complex s1, s2, r; |
---|
222 | n/a | |
---|
223 | n/a | SPECIAL_VALUE(z, acos_special_values); |
---|
224 | n/a | |
---|
225 | n/a | if (fabs(z.real) > CM_LARGE_DOUBLE || fabs(z.imag) > CM_LARGE_DOUBLE) { |
---|
226 | n/a | /* avoid unnecessary overflow for large arguments */ |
---|
227 | n/a | r.real = atan2(fabs(z.imag), z.real); |
---|
228 | n/a | /* split into cases to make sure that the branch cut has the |
---|
229 | n/a | correct continuity on systems with unsigned zeros */ |
---|
230 | n/a | if (z.real < 0.) { |
---|
231 | n/a | r.imag = -copysign(log(hypot(z.real/2., z.imag/2.)) + |
---|
232 | n/a | M_LN2*2., z.imag); |
---|
233 | n/a | } else { |
---|
234 | n/a | r.imag = copysign(log(hypot(z.real/2., z.imag/2.)) + |
---|
235 | n/a | M_LN2*2., -z.imag); |
---|
236 | n/a | } |
---|
237 | n/a | } else { |
---|
238 | n/a | s1.real = 1.-z.real; |
---|
239 | n/a | s1.imag = -z.imag; |
---|
240 | n/a | s1 = cmath_sqrt_impl(module, s1); |
---|
241 | n/a | s2.real = 1.+z.real; |
---|
242 | n/a | s2.imag = z.imag; |
---|
243 | n/a | s2 = cmath_sqrt_impl(module, s2); |
---|
244 | n/a | r.real = 2.*atan2(s1.real, s2.real); |
---|
245 | n/a | r.imag = m_asinh(s2.real*s1.imag - s2.imag*s1.real); |
---|
246 | n/a | } |
---|
247 | n/a | errno = 0; |
---|
248 | n/a | return r; |
---|
249 | n/a | } |
---|
250 | n/a | |
---|
251 | n/a | |
---|
252 | n/a | static Py_complex acosh_special_values[7][7]; |
---|
253 | n/a | |
---|
254 | n/a | /*[clinic input] |
---|
255 | n/a | cmath.acosh = cmath.acos |
---|
256 | n/a | |
---|
257 | n/a | Return the inverse hyperbolic cosine of z. |
---|
258 | n/a | [clinic start generated code]*/ |
---|
259 | n/a | |
---|
260 | n/a | static Py_complex |
---|
261 | n/a | cmath_acosh_impl(PyObject *module, Py_complex z) |
---|
262 | n/a | /*[clinic end generated code: output=3e2454d4fcf404ca input=3f61bee7d703e53c]*/ |
---|
263 | n/a | { |
---|
264 | n/a | Py_complex s1, s2, r; |
---|
265 | n/a | |
---|
266 | n/a | SPECIAL_VALUE(z, acosh_special_values); |
---|
267 | n/a | |
---|
268 | n/a | if (fabs(z.real) > CM_LARGE_DOUBLE || fabs(z.imag) > CM_LARGE_DOUBLE) { |
---|
269 | n/a | /* avoid unnecessary overflow for large arguments */ |
---|
270 | n/a | r.real = log(hypot(z.real/2., z.imag/2.)) + M_LN2*2.; |
---|
271 | n/a | r.imag = atan2(z.imag, z.real); |
---|
272 | n/a | } else { |
---|
273 | n/a | s1.real = z.real - 1.; |
---|
274 | n/a | s1.imag = z.imag; |
---|
275 | n/a | s1 = cmath_sqrt_impl(module, s1); |
---|
276 | n/a | s2.real = z.real + 1.; |
---|
277 | n/a | s2.imag = z.imag; |
---|
278 | n/a | s2 = cmath_sqrt_impl(module, s2); |
---|
279 | n/a | r.real = m_asinh(s1.real*s2.real + s1.imag*s2.imag); |
---|
280 | n/a | r.imag = 2.*atan2(s1.imag, s2.real); |
---|
281 | n/a | } |
---|
282 | n/a | errno = 0; |
---|
283 | n/a | return r; |
---|
284 | n/a | } |
---|
285 | n/a | |
---|
286 | n/a | /*[clinic input] |
---|
287 | n/a | cmath.asin = cmath.acos |
---|
288 | n/a | |
---|
289 | n/a | Return the arc sine of z. |
---|
290 | n/a | [clinic start generated code]*/ |
---|
291 | n/a | |
---|
292 | n/a | static Py_complex |
---|
293 | n/a | cmath_asin_impl(PyObject *module, Py_complex z) |
---|
294 | n/a | /*[clinic end generated code: output=3b264cd1b16bf4e1 input=be0bf0cfdd5239c5]*/ |
---|
295 | n/a | { |
---|
296 | n/a | /* asin(z) = -i asinh(iz) */ |
---|
297 | n/a | Py_complex s, r; |
---|
298 | n/a | s.real = -z.imag; |
---|
299 | n/a | s.imag = z.real; |
---|
300 | n/a | s = cmath_asinh_impl(module, s); |
---|
301 | n/a | r.real = s.imag; |
---|
302 | n/a | r.imag = -s.real; |
---|
303 | n/a | return r; |
---|
304 | n/a | } |
---|
305 | n/a | |
---|
306 | n/a | |
---|
307 | n/a | static Py_complex asinh_special_values[7][7]; |
---|
308 | n/a | |
---|
309 | n/a | /*[clinic input] |
---|
310 | n/a | cmath.asinh = cmath.acos |
---|
311 | n/a | |
---|
312 | n/a | Return the inverse hyperbolic sine of z. |
---|
313 | n/a | [clinic start generated code]*/ |
---|
314 | n/a | |
---|
315 | n/a | static Py_complex |
---|
316 | n/a | cmath_asinh_impl(PyObject *module, Py_complex z) |
---|
317 | n/a | /*[clinic end generated code: output=733d8107841a7599 input=5c09448fcfc89a79]*/ |
---|
318 | n/a | { |
---|
319 | n/a | Py_complex s1, s2, r; |
---|
320 | n/a | |
---|
321 | n/a | SPECIAL_VALUE(z, asinh_special_values); |
---|
322 | n/a | |
---|
323 | n/a | if (fabs(z.real) > CM_LARGE_DOUBLE || fabs(z.imag) > CM_LARGE_DOUBLE) { |
---|
324 | n/a | if (z.imag >= 0.) { |
---|
325 | n/a | r.real = copysign(log(hypot(z.real/2., z.imag/2.)) + |
---|
326 | n/a | M_LN2*2., z.real); |
---|
327 | n/a | } else { |
---|
328 | n/a | r.real = -copysign(log(hypot(z.real/2., z.imag/2.)) + |
---|
329 | n/a | M_LN2*2., -z.real); |
---|
330 | n/a | } |
---|
331 | n/a | r.imag = atan2(z.imag, fabs(z.real)); |
---|
332 | n/a | } else { |
---|
333 | n/a | s1.real = 1.+z.imag; |
---|
334 | n/a | s1.imag = -z.real; |
---|
335 | n/a | s1 = cmath_sqrt_impl(module, s1); |
---|
336 | n/a | s2.real = 1.-z.imag; |
---|
337 | n/a | s2.imag = z.real; |
---|
338 | n/a | s2 = cmath_sqrt_impl(module, s2); |
---|
339 | n/a | r.real = m_asinh(s1.real*s2.imag-s2.real*s1.imag); |
---|
340 | n/a | r.imag = atan2(z.imag, s1.real*s2.real-s1.imag*s2.imag); |
---|
341 | n/a | } |
---|
342 | n/a | errno = 0; |
---|
343 | n/a | return r; |
---|
344 | n/a | } |
---|
345 | n/a | |
---|
346 | n/a | |
---|
347 | n/a | /*[clinic input] |
---|
348 | n/a | cmath.atan = cmath.acos |
---|
349 | n/a | |
---|
350 | n/a | Return the arc tangent of z. |
---|
351 | n/a | [clinic start generated code]*/ |
---|
352 | n/a | |
---|
353 | n/a | static Py_complex |
---|
354 | n/a | cmath_atan_impl(PyObject *module, Py_complex z) |
---|
355 | n/a | /*[clinic end generated code: output=b6bfc497058acba4 input=3b21ff7d5eac632a]*/ |
---|
356 | n/a | { |
---|
357 | n/a | /* atan(z) = -i atanh(iz) */ |
---|
358 | n/a | Py_complex s, r; |
---|
359 | n/a | s.real = -z.imag; |
---|
360 | n/a | s.imag = z.real; |
---|
361 | n/a | s = cmath_atanh_impl(module, s); |
---|
362 | n/a | r.real = s.imag; |
---|
363 | n/a | r.imag = -s.real; |
---|
364 | n/a | return r; |
---|
365 | n/a | } |
---|
366 | n/a | |
---|
367 | n/a | /* Windows screws up atan2 for inf and nan, and alpha Tru64 5.1 doesn't follow |
---|
368 | n/a | C99 for atan2(0., 0.). */ |
---|
369 | n/a | static double |
---|
370 | n/a | c_atan2(Py_complex z) |
---|
371 | n/a | { |
---|
372 | n/a | if (Py_IS_NAN(z.real) || Py_IS_NAN(z.imag)) |
---|
373 | n/a | return Py_NAN; |
---|
374 | n/a | if (Py_IS_INFINITY(z.imag)) { |
---|
375 | n/a | if (Py_IS_INFINITY(z.real)) { |
---|
376 | n/a | if (copysign(1., z.real) == 1.) |
---|
377 | n/a | /* atan2(+-inf, +inf) == +-pi/4 */ |
---|
378 | n/a | return copysign(0.25*Py_MATH_PI, z.imag); |
---|
379 | n/a | else |
---|
380 | n/a | /* atan2(+-inf, -inf) == +-pi*3/4 */ |
---|
381 | n/a | return copysign(0.75*Py_MATH_PI, z.imag); |
---|
382 | n/a | } |
---|
383 | n/a | /* atan2(+-inf, x) == +-pi/2 for finite x */ |
---|
384 | n/a | return copysign(0.5*Py_MATH_PI, z.imag); |
---|
385 | n/a | } |
---|
386 | n/a | if (Py_IS_INFINITY(z.real) || z.imag == 0.) { |
---|
387 | n/a | if (copysign(1., z.real) == 1.) |
---|
388 | n/a | /* atan2(+-y, +inf) = atan2(+-0, +x) = +-0. */ |
---|
389 | n/a | return copysign(0., z.imag); |
---|
390 | n/a | else |
---|
391 | n/a | /* atan2(+-y, -inf) = atan2(+-0., -x) = +-pi. */ |
---|
392 | n/a | return copysign(Py_MATH_PI, z.imag); |
---|
393 | n/a | } |
---|
394 | n/a | return atan2(z.imag, z.real); |
---|
395 | n/a | } |
---|
396 | n/a | |
---|
397 | n/a | |
---|
398 | n/a | static Py_complex atanh_special_values[7][7]; |
---|
399 | n/a | |
---|
400 | n/a | /*[clinic input] |
---|
401 | n/a | cmath.atanh = cmath.acos |
---|
402 | n/a | |
---|
403 | n/a | Return the inverse hyperbolic tangent of z. |
---|
404 | n/a | [clinic start generated code]*/ |
---|
405 | n/a | |
---|
406 | n/a | static Py_complex |
---|
407 | n/a | cmath_atanh_impl(PyObject *module, Py_complex z) |
---|
408 | n/a | /*[clinic end generated code: output=e83355f93a989c9e input=2b3fdb82fb34487b]*/ |
---|
409 | n/a | { |
---|
410 | n/a | Py_complex r; |
---|
411 | n/a | double ay, h; |
---|
412 | n/a | |
---|
413 | n/a | SPECIAL_VALUE(z, atanh_special_values); |
---|
414 | n/a | |
---|
415 | n/a | /* Reduce to case where z.real >= 0., using atanh(z) = -atanh(-z). */ |
---|
416 | n/a | if (z.real < 0.) { |
---|
417 | n/a | return _Py_c_neg(cmath_atanh_impl(module, _Py_c_neg(z))); |
---|
418 | n/a | } |
---|
419 | n/a | |
---|
420 | n/a | ay = fabs(z.imag); |
---|
421 | n/a | if (z.real > CM_SQRT_LARGE_DOUBLE || ay > CM_SQRT_LARGE_DOUBLE) { |
---|
422 | n/a | /* |
---|
423 | n/a | if abs(z) is large then we use the approximation |
---|
424 | n/a | atanh(z) ~ 1/z +/- i*pi/2 (+/- depending on the sign |
---|
425 | n/a | of z.imag) |
---|
426 | n/a | */ |
---|
427 | n/a | h = hypot(z.real/2., z.imag/2.); /* safe from overflow */ |
---|
428 | n/a | r.real = z.real/4./h/h; |
---|
429 | n/a | /* the two negations in the next line cancel each other out |
---|
430 | n/a | except when working with unsigned zeros: they're there to |
---|
431 | n/a | ensure that the branch cut has the correct continuity on |
---|
432 | n/a | systems that don't support signed zeros */ |
---|
433 | n/a | r.imag = -copysign(Py_MATH_PI/2., -z.imag); |
---|
434 | n/a | errno = 0; |
---|
435 | n/a | } else if (z.real == 1. && ay < CM_SQRT_DBL_MIN) { |
---|
436 | n/a | /* C99 standard says: atanh(1+/-0.) should be inf +/- 0i */ |
---|
437 | n/a | if (ay == 0.) { |
---|
438 | n/a | r.real = INF; |
---|
439 | n/a | r.imag = z.imag; |
---|
440 | n/a | errno = EDOM; |
---|
441 | n/a | } else { |
---|
442 | n/a | r.real = -log(sqrt(ay)/sqrt(hypot(ay, 2.))); |
---|
443 | n/a | r.imag = copysign(atan2(2., -ay)/2, z.imag); |
---|
444 | n/a | errno = 0; |
---|
445 | n/a | } |
---|
446 | n/a | } else { |
---|
447 | n/a | r.real = m_log1p(4.*z.real/((1-z.real)*(1-z.real) + ay*ay))/4.; |
---|
448 | n/a | r.imag = -atan2(-2.*z.imag, (1-z.real)*(1+z.real) - ay*ay)/2.; |
---|
449 | n/a | errno = 0; |
---|
450 | n/a | } |
---|
451 | n/a | return r; |
---|
452 | n/a | } |
---|
453 | n/a | |
---|
454 | n/a | |
---|
455 | n/a | /*[clinic input] |
---|
456 | n/a | cmath.cos = cmath.acos |
---|
457 | n/a | |
---|
458 | n/a | Return the cosine of z. |
---|
459 | n/a | [clinic start generated code]*/ |
---|
460 | n/a | |
---|
461 | n/a | static Py_complex |
---|
462 | n/a | cmath_cos_impl(PyObject *module, Py_complex z) |
---|
463 | n/a | /*[clinic end generated code: output=fd64918d5b3186db input=6022e39b77127ac7]*/ |
---|
464 | n/a | { |
---|
465 | n/a | /* cos(z) = cosh(iz) */ |
---|
466 | n/a | Py_complex r; |
---|
467 | n/a | r.real = -z.imag; |
---|
468 | n/a | r.imag = z.real; |
---|
469 | n/a | r = cmath_cosh_impl(module, r); |
---|
470 | n/a | return r; |
---|
471 | n/a | } |
---|
472 | n/a | |
---|
473 | n/a | |
---|
474 | n/a | /* cosh(infinity + i*y) needs to be dealt with specially */ |
---|
475 | n/a | static Py_complex cosh_special_values[7][7]; |
---|
476 | n/a | |
---|
477 | n/a | /*[clinic input] |
---|
478 | n/a | cmath.cosh = cmath.acos |
---|
479 | n/a | |
---|
480 | n/a | Return the hyperbolic cosine of z. |
---|
481 | n/a | [clinic start generated code]*/ |
---|
482 | n/a | |
---|
483 | n/a | static Py_complex |
---|
484 | n/a | cmath_cosh_impl(PyObject *module, Py_complex z) |
---|
485 | n/a | /*[clinic end generated code: output=2e969047da601bdb input=d6b66339e9cc332b]*/ |
---|
486 | n/a | { |
---|
487 | n/a | Py_complex r; |
---|
488 | n/a | double x_minus_one; |
---|
489 | n/a | |
---|
490 | n/a | /* special treatment for cosh(+/-inf + iy) if y is not a NaN */ |
---|
491 | n/a | if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) { |
---|
492 | n/a | if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag) && |
---|
493 | n/a | (z.imag != 0.)) { |
---|
494 | n/a | if (z.real > 0) { |
---|
495 | n/a | r.real = copysign(INF, cos(z.imag)); |
---|
496 | n/a | r.imag = copysign(INF, sin(z.imag)); |
---|
497 | n/a | } |
---|
498 | n/a | else { |
---|
499 | n/a | r.real = copysign(INF, cos(z.imag)); |
---|
500 | n/a | r.imag = -copysign(INF, sin(z.imag)); |
---|
501 | n/a | } |
---|
502 | n/a | } |
---|
503 | n/a | else { |
---|
504 | n/a | r = cosh_special_values[special_type(z.real)] |
---|
505 | n/a | [special_type(z.imag)]; |
---|
506 | n/a | } |
---|
507 | n/a | /* need to set errno = EDOM if y is +/- infinity and x is not |
---|
508 | n/a | a NaN */ |
---|
509 | n/a | if (Py_IS_INFINITY(z.imag) && !Py_IS_NAN(z.real)) |
---|
510 | n/a | errno = EDOM; |
---|
511 | n/a | else |
---|
512 | n/a | errno = 0; |
---|
513 | n/a | return r; |
---|
514 | n/a | } |
---|
515 | n/a | |
---|
516 | n/a | if (fabs(z.real) > CM_LOG_LARGE_DOUBLE) { |
---|
517 | n/a | /* deal correctly with cases where cosh(z.real) overflows but |
---|
518 | n/a | cosh(z) does not. */ |
---|
519 | n/a | x_minus_one = z.real - copysign(1., z.real); |
---|
520 | n/a | r.real = cos(z.imag) * cosh(x_minus_one) * Py_MATH_E; |
---|
521 | n/a | r.imag = sin(z.imag) * sinh(x_minus_one) * Py_MATH_E; |
---|
522 | n/a | } else { |
---|
523 | n/a | r.real = cos(z.imag) * cosh(z.real); |
---|
524 | n/a | r.imag = sin(z.imag) * sinh(z.real); |
---|
525 | n/a | } |
---|
526 | n/a | /* detect overflow, and set errno accordingly */ |
---|
527 | n/a | if (Py_IS_INFINITY(r.real) || Py_IS_INFINITY(r.imag)) |
---|
528 | n/a | errno = ERANGE; |
---|
529 | n/a | else |
---|
530 | n/a | errno = 0; |
---|
531 | n/a | return r; |
---|
532 | n/a | } |
---|
533 | n/a | |
---|
534 | n/a | |
---|
535 | n/a | /* exp(infinity + i*y) and exp(-infinity + i*y) need special treatment for |
---|
536 | n/a | finite y */ |
---|
537 | n/a | static Py_complex exp_special_values[7][7]; |
---|
538 | n/a | |
---|
539 | n/a | /*[clinic input] |
---|
540 | n/a | cmath.exp = cmath.acos |
---|
541 | n/a | |
---|
542 | n/a | Return the exponential value e**z. |
---|
543 | n/a | [clinic start generated code]*/ |
---|
544 | n/a | |
---|
545 | n/a | static Py_complex |
---|
546 | n/a | cmath_exp_impl(PyObject *module, Py_complex z) |
---|
547 | n/a | /*[clinic end generated code: output=edcec61fb9dfda6c input=8b9e6cf8a92174c3]*/ |
---|
548 | n/a | { |
---|
549 | n/a | Py_complex r; |
---|
550 | n/a | double l; |
---|
551 | n/a | |
---|
552 | n/a | if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) { |
---|
553 | n/a | if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag) |
---|
554 | n/a | && (z.imag != 0.)) { |
---|
555 | n/a | if (z.real > 0) { |
---|
556 | n/a | r.real = copysign(INF, cos(z.imag)); |
---|
557 | n/a | r.imag = copysign(INF, sin(z.imag)); |
---|
558 | n/a | } |
---|
559 | n/a | else { |
---|
560 | n/a | r.real = copysign(0., cos(z.imag)); |
---|
561 | n/a | r.imag = copysign(0., sin(z.imag)); |
---|
562 | n/a | } |
---|
563 | n/a | } |
---|
564 | n/a | else { |
---|
565 | n/a | r = exp_special_values[special_type(z.real)] |
---|
566 | n/a | [special_type(z.imag)]; |
---|
567 | n/a | } |
---|
568 | n/a | /* need to set errno = EDOM if y is +/- infinity and x is not |
---|
569 | n/a | a NaN and not -infinity */ |
---|
570 | n/a | if (Py_IS_INFINITY(z.imag) && |
---|
571 | n/a | (Py_IS_FINITE(z.real) || |
---|
572 | n/a | (Py_IS_INFINITY(z.real) && z.real > 0))) |
---|
573 | n/a | errno = EDOM; |
---|
574 | n/a | else |
---|
575 | n/a | errno = 0; |
---|
576 | n/a | return r; |
---|
577 | n/a | } |
---|
578 | n/a | |
---|
579 | n/a | if (z.real > CM_LOG_LARGE_DOUBLE) { |
---|
580 | n/a | l = exp(z.real-1.); |
---|
581 | n/a | r.real = l*cos(z.imag)*Py_MATH_E; |
---|
582 | n/a | r.imag = l*sin(z.imag)*Py_MATH_E; |
---|
583 | n/a | } else { |
---|
584 | n/a | l = exp(z.real); |
---|
585 | n/a | r.real = l*cos(z.imag); |
---|
586 | n/a | r.imag = l*sin(z.imag); |
---|
587 | n/a | } |
---|
588 | n/a | /* detect overflow, and set errno accordingly */ |
---|
589 | n/a | if (Py_IS_INFINITY(r.real) || Py_IS_INFINITY(r.imag)) |
---|
590 | n/a | errno = ERANGE; |
---|
591 | n/a | else |
---|
592 | n/a | errno = 0; |
---|
593 | n/a | return r; |
---|
594 | n/a | } |
---|
595 | n/a | |
---|
596 | n/a | static Py_complex log_special_values[7][7]; |
---|
597 | n/a | |
---|
598 | n/a | static Py_complex |
---|
599 | n/a | c_log(Py_complex z) |
---|
600 | n/a | { |
---|
601 | n/a | /* |
---|
602 | n/a | The usual formula for the real part is log(hypot(z.real, z.imag)). |
---|
603 | n/a | There are four situations where this formula is potentially |
---|
604 | n/a | problematic: |
---|
605 | n/a | |
---|
606 | n/a | (1) the absolute value of z is subnormal. Then hypot is subnormal, |
---|
607 | n/a | so has fewer than the usual number of bits of accuracy, hence may |
---|
608 | n/a | have large relative error. This then gives a large absolute error |
---|
609 | n/a | in the log. This can be solved by rescaling z by a suitable power |
---|
610 | n/a | of 2. |
---|
611 | n/a | |
---|
612 | n/a | (2) the absolute value of z is greater than DBL_MAX (e.g. when both |
---|
613 | n/a | z.real and z.imag are within a factor of 1/sqrt(2) of DBL_MAX) |
---|
614 | n/a | Again, rescaling solves this. |
---|
615 | n/a | |
---|
616 | n/a | (3) the absolute value of z is close to 1. In this case it's |
---|
617 | n/a | difficult to achieve good accuracy, at least in part because a |
---|
618 | n/a | change of 1ulp in the real or imaginary part of z can result in a |
---|
619 | n/a | change of billions of ulps in the correctly rounded answer. |
---|
620 | n/a | |
---|
621 | n/a | (4) z = 0. The simplest thing to do here is to call the |
---|
622 | n/a | floating-point log with an argument of 0, and let its behaviour |
---|
623 | n/a | (returning -infinity, signaling a floating-point exception, setting |
---|
624 | n/a | errno, or whatever) determine that of c_log. So the usual formula |
---|
625 | n/a | is fine here. |
---|
626 | n/a | |
---|
627 | n/a | */ |
---|
628 | n/a | |
---|
629 | n/a | Py_complex r; |
---|
630 | n/a | double ax, ay, am, an, h; |
---|
631 | n/a | |
---|
632 | n/a | SPECIAL_VALUE(z, log_special_values); |
---|
633 | n/a | |
---|
634 | n/a | ax = fabs(z.real); |
---|
635 | n/a | ay = fabs(z.imag); |
---|
636 | n/a | |
---|
637 | n/a | if (ax > CM_LARGE_DOUBLE || ay > CM_LARGE_DOUBLE) { |
---|
638 | n/a | r.real = log(hypot(ax/2., ay/2.)) + M_LN2; |
---|
639 | n/a | } else if (ax < DBL_MIN && ay < DBL_MIN) { |
---|
640 | n/a | if (ax > 0. || ay > 0.) { |
---|
641 | n/a | /* catch cases where hypot(ax, ay) is subnormal */ |
---|
642 | n/a | r.real = log(hypot(ldexp(ax, DBL_MANT_DIG), |
---|
643 | n/a | ldexp(ay, DBL_MANT_DIG))) - DBL_MANT_DIG*M_LN2; |
---|
644 | n/a | } |
---|
645 | n/a | else { |
---|
646 | n/a | /* log(+/-0. +/- 0i) */ |
---|
647 | n/a | r.real = -INF; |
---|
648 | n/a | r.imag = atan2(z.imag, z.real); |
---|
649 | n/a | errno = EDOM; |
---|
650 | n/a | return r; |
---|
651 | n/a | } |
---|
652 | n/a | } else { |
---|
653 | n/a | h = hypot(ax, ay); |
---|
654 | n/a | if (0.71 <= h && h <= 1.73) { |
---|
655 | n/a | am = ax > ay ? ax : ay; /* max(ax, ay) */ |
---|
656 | n/a | an = ax > ay ? ay : ax; /* min(ax, ay) */ |
---|
657 | n/a | r.real = m_log1p((am-1)*(am+1)+an*an)/2.; |
---|
658 | n/a | } else { |
---|
659 | n/a | r.real = log(h); |
---|
660 | n/a | } |
---|
661 | n/a | } |
---|
662 | n/a | r.imag = atan2(z.imag, z.real); |
---|
663 | n/a | errno = 0; |
---|
664 | n/a | return r; |
---|
665 | n/a | } |
---|
666 | n/a | |
---|
667 | n/a | |
---|
668 | n/a | /*[clinic input] |
---|
669 | n/a | cmath.log10 = cmath.acos |
---|
670 | n/a | |
---|
671 | n/a | Return the base-10 logarithm of z. |
---|
672 | n/a | [clinic start generated code]*/ |
---|
673 | n/a | |
---|
674 | n/a | static Py_complex |
---|
675 | n/a | cmath_log10_impl(PyObject *module, Py_complex z) |
---|
676 | n/a | /*[clinic end generated code: output=2922779a7c38cbe1 input=cff5644f73c1519c]*/ |
---|
677 | n/a | { |
---|
678 | n/a | Py_complex r; |
---|
679 | n/a | int errno_save; |
---|
680 | n/a | |
---|
681 | n/a | r = c_log(z); |
---|
682 | n/a | errno_save = errno; /* just in case the divisions affect errno */ |
---|
683 | n/a | r.real = r.real / M_LN10; |
---|
684 | n/a | r.imag = r.imag / M_LN10; |
---|
685 | n/a | errno = errno_save; |
---|
686 | n/a | return r; |
---|
687 | n/a | } |
---|
688 | n/a | |
---|
689 | n/a | |
---|
690 | n/a | /*[clinic input] |
---|
691 | n/a | cmath.sin = cmath.acos |
---|
692 | n/a | |
---|
693 | n/a | Return the sine of z. |
---|
694 | n/a | [clinic start generated code]*/ |
---|
695 | n/a | |
---|
696 | n/a | static Py_complex |
---|
697 | n/a | cmath_sin_impl(PyObject *module, Py_complex z) |
---|
698 | n/a | /*[clinic end generated code: output=980370d2ff0bb5aa input=2d3519842a8b4b85]*/ |
---|
699 | n/a | { |
---|
700 | n/a | /* sin(z) = -i sin(iz) */ |
---|
701 | n/a | Py_complex s, r; |
---|
702 | n/a | s.real = -z.imag; |
---|
703 | n/a | s.imag = z.real; |
---|
704 | n/a | s = cmath_sinh_impl(module, s); |
---|
705 | n/a | r.real = s.imag; |
---|
706 | n/a | r.imag = -s.real; |
---|
707 | n/a | return r; |
---|
708 | n/a | } |
---|
709 | n/a | |
---|
710 | n/a | |
---|
711 | n/a | /* sinh(infinity + i*y) needs to be dealt with specially */ |
---|
712 | n/a | static Py_complex sinh_special_values[7][7]; |
---|
713 | n/a | |
---|
714 | n/a | /*[clinic input] |
---|
715 | n/a | cmath.sinh = cmath.acos |
---|
716 | n/a | |
---|
717 | n/a | Return the hyperbolic sine of z. |
---|
718 | n/a | [clinic start generated code]*/ |
---|
719 | n/a | |
---|
720 | n/a | static Py_complex |
---|
721 | n/a | cmath_sinh_impl(PyObject *module, Py_complex z) |
---|
722 | n/a | /*[clinic end generated code: output=38b0a6cce26f3536 input=d2d3fc8c1ddfd2dd]*/ |
---|
723 | n/a | { |
---|
724 | n/a | Py_complex r; |
---|
725 | n/a | double x_minus_one; |
---|
726 | n/a | |
---|
727 | n/a | /* special treatment for sinh(+/-inf + iy) if y is finite and |
---|
728 | n/a | nonzero */ |
---|
729 | n/a | if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) { |
---|
730 | n/a | if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag) |
---|
731 | n/a | && (z.imag != 0.)) { |
---|
732 | n/a | if (z.real > 0) { |
---|
733 | n/a | r.real = copysign(INF, cos(z.imag)); |
---|
734 | n/a | r.imag = copysign(INF, sin(z.imag)); |
---|
735 | n/a | } |
---|
736 | n/a | else { |
---|
737 | n/a | r.real = -copysign(INF, cos(z.imag)); |
---|
738 | n/a | r.imag = copysign(INF, sin(z.imag)); |
---|
739 | n/a | } |
---|
740 | n/a | } |
---|
741 | n/a | else { |
---|
742 | n/a | r = sinh_special_values[special_type(z.real)] |
---|
743 | n/a | [special_type(z.imag)]; |
---|
744 | n/a | } |
---|
745 | n/a | /* need to set errno = EDOM if y is +/- infinity and x is not |
---|
746 | n/a | a NaN */ |
---|
747 | n/a | if (Py_IS_INFINITY(z.imag) && !Py_IS_NAN(z.real)) |
---|
748 | n/a | errno = EDOM; |
---|
749 | n/a | else |
---|
750 | n/a | errno = 0; |
---|
751 | n/a | return r; |
---|
752 | n/a | } |
---|
753 | n/a | |
---|
754 | n/a | if (fabs(z.real) > CM_LOG_LARGE_DOUBLE) { |
---|
755 | n/a | x_minus_one = z.real - copysign(1., z.real); |
---|
756 | n/a | r.real = cos(z.imag) * sinh(x_minus_one) * Py_MATH_E; |
---|
757 | n/a | r.imag = sin(z.imag) * cosh(x_minus_one) * Py_MATH_E; |
---|
758 | n/a | } else { |
---|
759 | n/a | r.real = cos(z.imag) * sinh(z.real); |
---|
760 | n/a | r.imag = sin(z.imag) * cosh(z.real); |
---|
761 | n/a | } |
---|
762 | n/a | /* detect overflow, and set errno accordingly */ |
---|
763 | n/a | if (Py_IS_INFINITY(r.real) || Py_IS_INFINITY(r.imag)) |
---|
764 | n/a | errno = ERANGE; |
---|
765 | n/a | else |
---|
766 | n/a | errno = 0; |
---|
767 | n/a | return r; |
---|
768 | n/a | } |
---|
769 | n/a | |
---|
770 | n/a | |
---|
771 | n/a | static Py_complex sqrt_special_values[7][7]; |
---|
772 | n/a | |
---|
773 | n/a | /*[clinic input] |
---|
774 | n/a | cmath.sqrt = cmath.acos |
---|
775 | n/a | |
---|
776 | n/a | Return the square root of z. |
---|
777 | n/a | [clinic start generated code]*/ |
---|
778 | n/a | |
---|
779 | n/a | static Py_complex |
---|
780 | n/a | cmath_sqrt_impl(PyObject *module, Py_complex z) |
---|
781 | n/a | /*[clinic end generated code: output=b6507b3029c339fc input=7088b166fc9a58c7]*/ |
---|
782 | n/a | { |
---|
783 | n/a | /* |
---|
784 | n/a | Method: use symmetries to reduce to the case when x = z.real and y |
---|
785 | n/a | = z.imag are nonnegative. Then the real part of the result is |
---|
786 | n/a | given by |
---|
787 | n/a | |
---|
788 | n/a | s = sqrt((x + hypot(x, y))/2) |
---|
789 | n/a | |
---|
790 | n/a | and the imaginary part is |
---|
791 | n/a | |
---|
792 | n/a | d = (y/2)/s |
---|
793 | n/a | |
---|
794 | n/a | If either x or y is very large then there's a risk of overflow in |
---|
795 | n/a | computation of the expression x + hypot(x, y). We can avoid this |
---|
796 | n/a | by rewriting the formula for s as: |
---|
797 | n/a | |
---|
798 | n/a | s = 2*sqrt(x/8 + hypot(x/8, y/8)) |
---|
799 | n/a | |
---|
800 | n/a | This costs us two extra multiplications/divisions, but avoids the |
---|
801 | n/a | overhead of checking for x and y large. |
---|
802 | n/a | |
---|
803 | n/a | If both x and y are subnormal then hypot(x, y) may also be |
---|
804 | n/a | subnormal, so will lack full precision. We solve this by rescaling |
---|
805 | n/a | x and y by a sufficiently large power of 2 to ensure that x and y |
---|
806 | n/a | are normal. |
---|
807 | n/a | */ |
---|
808 | n/a | |
---|
809 | n/a | |
---|
810 | n/a | Py_complex r; |
---|
811 | n/a | double s,d; |
---|
812 | n/a | double ax, ay; |
---|
813 | n/a | |
---|
814 | n/a | SPECIAL_VALUE(z, sqrt_special_values); |
---|
815 | n/a | |
---|
816 | n/a | if (z.real == 0. && z.imag == 0.) { |
---|
817 | n/a | r.real = 0.; |
---|
818 | n/a | r.imag = z.imag; |
---|
819 | n/a | return r; |
---|
820 | n/a | } |
---|
821 | n/a | |
---|
822 | n/a | ax = fabs(z.real); |
---|
823 | n/a | ay = fabs(z.imag); |
---|
824 | n/a | |
---|
825 | n/a | if (ax < DBL_MIN && ay < DBL_MIN && (ax > 0. || ay > 0.)) { |
---|
826 | n/a | /* here we catch cases where hypot(ax, ay) is subnormal */ |
---|
827 | n/a | ax = ldexp(ax, CM_SCALE_UP); |
---|
828 | n/a | s = ldexp(sqrt(ax + hypot(ax, ldexp(ay, CM_SCALE_UP))), |
---|
829 | n/a | CM_SCALE_DOWN); |
---|
830 | n/a | } else { |
---|
831 | n/a | ax /= 8.; |
---|
832 | n/a | s = 2.*sqrt(ax + hypot(ax, ay/8.)); |
---|
833 | n/a | } |
---|
834 | n/a | d = ay/(2.*s); |
---|
835 | n/a | |
---|
836 | n/a | if (z.real >= 0.) { |
---|
837 | n/a | r.real = s; |
---|
838 | n/a | r.imag = copysign(d, z.imag); |
---|
839 | n/a | } else { |
---|
840 | n/a | r.real = d; |
---|
841 | n/a | r.imag = copysign(s, z.imag); |
---|
842 | n/a | } |
---|
843 | n/a | errno = 0; |
---|
844 | n/a | return r; |
---|
845 | n/a | } |
---|
846 | n/a | |
---|
847 | n/a | |
---|
848 | n/a | /*[clinic input] |
---|
849 | n/a | cmath.tan = cmath.acos |
---|
850 | n/a | |
---|
851 | n/a | Return the tangent of z. |
---|
852 | n/a | [clinic start generated code]*/ |
---|
853 | n/a | |
---|
854 | n/a | static Py_complex |
---|
855 | n/a | cmath_tan_impl(PyObject *module, Py_complex z) |
---|
856 | n/a | /*[clinic end generated code: output=7c5f13158a72eb13 input=fc167e528767888e]*/ |
---|
857 | n/a | { |
---|
858 | n/a | /* tan(z) = -i tanh(iz) */ |
---|
859 | n/a | Py_complex s, r; |
---|
860 | n/a | s.real = -z.imag; |
---|
861 | n/a | s.imag = z.real; |
---|
862 | n/a | s = cmath_tanh_impl(module, s); |
---|
863 | n/a | r.real = s.imag; |
---|
864 | n/a | r.imag = -s.real; |
---|
865 | n/a | return r; |
---|
866 | n/a | } |
---|
867 | n/a | |
---|
868 | n/a | |
---|
869 | n/a | /* tanh(infinity + i*y) needs to be dealt with specially */ |
---|
870 | n/a | static Py_complex tanh_special_values[7][7]; |
---|
871 | n/a | |
---|
872 | n/a | /*[clinic input] |
---|
873 | n/a | cmath.tanh = cmath.acos |
---|
874 | n/a | |
---|
875 | n/a | Return the hyperbolic tangent of z. |
---|
876 | n/a | [clinic start generated code]*/ |
---|
877 | n/a | |
---|
878 | n/a | static Py_complex |
---|
879 | n/a | cmath_tanh_impl(PyObject *module, Py_complex z) |
---|
880 | n/a | /*[clinic end generated code: output=36d547ef7aca116c input=22f67f9dc6d29685]*/ |
---|
881 | n/a | { |
---|
882 | n/a | /* Formula: |
---|
883 | n/a | |
---|
884 | n/a | tanh(x+iy) = (tanh(x)(1+tan(y)^2) + i tan(y)(1-tanh(x))^2) / |
---|
885 | n/a | (1+tan(y)^2 tanh(x)^2) |
---|
886 | n/a | |
---|
887 | n/a | To avoid excessive roundoff error, 1-tanh(x)^2 is better computed |
---|
888 | n/a | as 1/cosh(x)^2. When abs(x) is large, we approximate 1-tanh(x)^2 |
---|
889 | n/a | by 4 exp(-2*x) instead, to avoid possible overflow in the |
---|
890 | n/a | computation of cosh(x). |
---|
891 | n/a | |
---|
892 | n/a | */ |
---|
893 | n/a | |
---|
894 | n/a | Py_complex r; |
---|
895 | n/a | double tx, ty, cx, txty, denom; |
---|
896 | n/a | |
---|
897 | n/a | /* special treatment for tanh(+/-inf + iy) if y is finite and |
---|
898 | n/a | nonzero */ |
---|
899 | n/a | if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) { |
---|
900 | n/a | if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag) |
---|
901 | n/a | && (z.imag != 0.)) { |
---|
902 | n/a | if (z.real > 0) { |
---|
903 | n/a | r.real = 1.0; |
---|
904 | n/a | r.imag = copysign(0., |
---|
905 | n/a | 2.*sin(z.imag)*cos(z.imag)); |
---|
906 | n/a | } |
---|
907 | n/a | else { |
---|
908 | n/a | r.real = -1.0; |
---|
909 | n/a | r.imag = copysign(0., |
---|
910 | n/a | 2.*sin(z.imag)*cos(z.imag)); |
---|
911 | n/a | } |
---|
912 | n/a | } |
---|
913 | n/a | else { |
---|
914 | n/a | r = tanh_special_values[special_type(z.real)] |
---|
915 | n/a | [special_type(z.imag)]; |
---|
916 | n/a | } |
---|
917 | n/a | /* need to set errno = EDOM if z.imag is +/-infinity and |
---|
918 | n/a | z.real is finite */ |
---|
919 | n/a | if (Py_IS_INFINITY(z.imag) && Py_IS_FINITE(z.real)) |
---|
920 | n/a | errno = EDOM; |
---|
921 | n/a | else |
---|
922 | n/a | errno = 0; |
---|
923 | n/a | return r; |
---|
924 | n/a | } |
---|
925 | n/a | |
---|
926 | n/a | /* danger of overflow in 2.*z.imag !*/ |
---|
927 | n/a | if (fabs(z.real) > CM_LOG_LARGE_DOUBLE) { |
---|
928 | n/a | r.real = copysign(1., z.real); |
---|
929 | n/a | r.imag = 4.*sin(z.imag)*cos(z.imag)*exp(-2.*fabs(z.real)); |
---|
930 | n/a | } else { |
---|
931 | n/a | tx = tanh(z.real); |
---|
932 | n/a | ty = tan(z.imag); |
---|
933 | n/a | cx = 1./cosh(z.real); |
---|
934 | n/a | txty = tx*ty; |
---|
935 | n/a | denom = 1. + txty*txty; |
---|
936 | n/a | r.real = tx*(1.+ty*ty)/denom; |
---|
937 | n/a | r.imag = ((ty/denom)*cx)*cx; |
---|
938 | n/a | } |
---|
939 | n/a | errno = 0; |
---|
940 | n/a | return r; |
---|
941 | n/a | } |
---|
942 | n/a | |
---|
943 | n/a | |
---|
944 | n/a | /*[clinic input] |
---|
945 | n/a | cmath.log |
---|
946 | n/a | |
---|
947 | n/a | x: Py_complex |
---|
948 | n/a | y_obj: object = NULL |
---|
949 | n/a | / |
---|
950 | n/a | |
---|
951 | n/a | The logarithm of z to the given base. |
---|
952 | n/a | |
---|
953 | n/a | If the base not specified, returns the natural logarithm (base e) of z. |
---|
954 | n/a | [clinic start generated code]*/ |
---|
955 | n/a | |
---|
956 | n/a | static PyObject * |
---|
957 | n/a | cmath_log_impl(PyObject *module, Py_complex x, PyObject *y_obj) |
---|
958 | n/a | /*[clinic end generated code: output=4effdb7d258e0d94 input=ee0e823a7c6e68ea]*/ |
---|
959 | n/a | { |
---|
960 | n/a | Py_complex y; |
---|
961 | n/a | |
---|
962 | n/a | errno = 0; |
---|
963 | n/a | PyFPE_START_PROTECT("complex function", return 0) |
---|
964 | n/a | x = c_log(x); |
---|
965 | n/a | if (y_obj != NULL) { |
---|
966 | n/a | y = PyComplex_AsCComplex(y_obj); |
---|
967 | n/a | if (PyErr_Occurred()) { |
---|
968 | n/a | return NULL; |
---|
969 | n/a | } |
---|
970 | n/a | y = c_log(y); |
---|
971 | n/a | x = _Py_c_quot(x, y); |
---|
972 | n/a | } |
---|
973 | n/a | PyFPE_END_PROTECT(x) |
---|
974 | n/a | if (errno != 0) |
---|
975 | n/a | return math_error(); |
---|
976 | n/a | return PyComplex_FromCComplex(x); |
---|
977 | n/a | } |
---|
978 | n/a | |
---|
979 | n/a | |
---|
980 | n/a | /* And now the glue to make them available from Python: */ |
---|
981 | n/a | |
---|
982 | n/a | static PyObject * |
---|
983 | n/a | math_error(void) |
---|
984 | n/a | { |
---|
985 | n/a | if (errno == EDOM) |
---|
986 | n/a | PyErr_SetString(PyExc_ValueError, "math domain error"); |
---|
987 | n/a | else if (errno == ERANGE) |
---|
988 | n/a | PyErr_SetString(PyExc_OverflowError, "math range error"); |
---|
989 | n/a | else /* Unexpected math error */ |
---|
990 | n/a | PyErr_SetFromErrno(PyExc_ValueError); |
---|
991 | n/a | return NULL; |
---|
992 | n/a | } |
---|
993 | n/a | |
---|
994 | n/a | |
---|
995 | n/a | /*[clinic input] |
---|
996 | n/a | cmath.phase |
---|
997 | n/a | |
---|
998 | n/a | z: Py_complex |
---|
999 | n/a | / |
---|
1000 | n/a | |
---|
1001 | n/a | Return argument, also known as the phase angle, of a complex. |
---|
1002 | n/a | [clinic start generated code]*/ |
---|
1003 | n/a | |
---|
1004 | n/a | static PyObject * |
---|
1005 | n/a | cmath_phase_impl(PyObject *module, Py_complex z) |
---|
1006 | n/a | /*[clinic end generated code: output=50725086a7bfd253 input=5cf75228ba94b69d]*/ |
---|
1007 | n/a | { |
---|
1008 | n/a | double phi; |
---|
1009 | n/a | |
---|
1010 | n/a | errno = 0; |
---|
1011 | n/a | PyFPE_START_PROTECT("arg function", return 0) |
---|
1012 | n/a | phi = c_atan2(z); |
---|
1013 | n/a | PyFPE_END_PROTECT(phi) |
---|
1014 | n/a | if (errno != 0) |
---|
1015 | n/a | return math_error(); |
---|
1016 | n/a | else |
---|
1017 | n/a | return PyFloat_FromDouble(phi); |
---|
1018 | n/a | } |
---|
1019 | n/a | |
---|
1020 | n/a | /*[clinic input] |
---|
1021 | n/a | cmath.polar |
---|
1022 | n/a | |
---|
1023 | n/a | z: Py_complex |
---|
1024 | n/a | / |
---|
1025 | n/a | |
---|
1026 | n/a | Convert a complex from rectangular coordinates to polar coordinates. |
---|
1027 | n/a | |
---|
1028 | n/a | r is the distance from 0 and phi the phase angle. |
---|
1029 | n/a | [clinic start generated code]*/ |
---|
1030 | n/a | |
---|
1031 | n/a | static PyObject * |
---|
1032 | n/a | cmath_polar_impl(PyObject *module, Py_complex z) |
---|
1033 | n/a | /*[clinic end generated code: output=d0a8147c41dbb654 input=26c353574fd1a861]*/ |
---|
1034 | n/a | { |
---|
1035 | n/a | double r, phi; |
---|
1036 | n/a | |
---|
1037 | n/a | errno = 0; |
---|
1038 | n/a | PyFPE_START_PROTECT("polar function", return 0) |
---|
1039 | n/a | phi = c_atan2(z); /* should not cause any exception */ |
---|
1040 | n/a | r = _Py_c_abs(z); /* sets errno to ERANGE on overflow */ |
---|
1041 | n/a | PyFPE_END_PROTECT(r) |
---|
1042 | n/a | if (errno != 0) |
---|
1043 | n/a | return math_error(); |
---|
1044 | n/a | else |
---|
1045 | n/a | return Py_BuildValue("dd", r, phi); |
---|
1046 | n/a | } |
---|
1047 | n/a | |
---|
1048 | n/a | /* |
---|
1049 | n/a | rect() isn't covered by the C99 standard, but it's not too hard to |
---|
1050 | n/a | figure out 'spirit of C99' rules for special value handing: |
---|
1051 | n/a | |
---|
1052 | n/a | rect(x, t) should behave like exp(log(x) + it) for positive-signed x |
---|
1053 | n/a | rect(x, t) should behave like -exp(log(-x) + it) for negative-signed x |
---|
1054 | n/a | rect(nan, t) should behave like exp(nan + it), except that rect(nan, 0) |
---|
1055 | n/a | gives nan +- i0 with the sign of the imaginary part unspecified. |
---|
1056 | n/a | |
---|
1057 | n/a | */ |
---|
1058 | n/a | |
---|
1059 | n/a | static Py_complex rect_special_values[7][7]; |
---|
1060 | n/a | |
---|
1061 | n/a | /*[clinic input] |
---|
1062 | n/a | cmath.rect |
---|
1063 | n/a | |
---|
1064 | n/a | r: double |
---|
1065 | n/a | phi: double |
---|
1066 | n/a | / |
---|
1067 | n/a | |
---|
1068 | n/a | Convert from polar coordinates to rectangular coordinates. |
---|
1069 | n/a | [clinic start generated code]*/ |
---|
1070 | n/a | |
---|
1071 | n/a | static PyObject * |
---|
1072 | n/a | cmath_rect_impl(PyObject *module, double r, double phi) |
---|
1073 | n/a | /*[clinic end generated code: output=385a0690925df2d5 input=24c5646d147efd69]*/ |
---|
1074 | n/a | { |
---|
1075 | n/a | Py_complex z; |
---|
1076 | n/a | errno = 0; |
---|
1077 | n/a | PyFPE_START_PROTECT("rect function", return 0) |
---|
1078 | n/a | |
---|
1079 | n/a | /* deal with special values */ |
---|
1080 | n/a | if (!Py_IS_FINITE(r) || !Py_IS_FINITE(phi)) { |
---|
1081 | n/a | /* if r is +/-infinity and phi is finite but nonzero then |
---|
1082 | n/a | result is (+-INF +-INF i), but we need to compute cos(phi) |
---|
1083 | n/a | and sin(phi) to figure out the signs. */ |
---|
1084 | n/a | if (Py_IS_INFINITY(r) && (Py_IS_FINITE(phi) |
---|
1085 | n/a | && (phi != 0.))) { |
---|
1086 | n/a | if (r > 0) { |
---|
1087 | n/a | z.real = copysign(INF, cos(phi)); |
---|
1088 | n/a | z.imag = copysign(INF, sin(phi)); |
---|
1089 | n/a | } |
---|
1090 | n/a | else { |
---|
1091 | n/a | z.real = -copysign(INF, cos(phi)); |
---|
1092 | n/a | z.imag = -copysign(INF, sin(phi)); |
---|
1093 | n/a | } |
---|
1094 | n/a | } |
---|
1095 | n/a | else { |
---|
1096 | n/a | z = rect_special_values[special_type(r)] |
---|
1097 | n/a | [special_type(phi)]; |
---|
1098 | n/a | } |
---|
1099 | n/a | /* need to set errno = EDOM if r is a nonzero number and phi |
---|
1100 | n/a | is infinite */ |
---|
1101 | n/a | if (r != 0. && !Py_IS_NAN(r) && Py_IS_INFINITY(phi)) |
---|
1102 | n/a | errno = EDOM; |
---|
1103 | n/a | else |
---|
1104 | n/a | errno = 0; |
---|
1105 | n/a | } |
---|
1106 | n/a | else if (phi == 0.0) { |
---|
1107 | n/a | /* Workaround for buggy results with phi=-0.0 on OS X 10.8. See |
---|
1108 | n/a | bugs.python.org/issue18513. */ |
---|
1109 | n/a | z.real = r; |
---|
1110 | n/a | z.imag = r * phi; |
---|
1111 | n/a | errno = 0; |
---|
1112 | n/a | } |
---|
1113 | n/a | else { |
---|
1114 | n/a | z.real = r * cos(phi); |
---|
1115 | n/a | z.imag = r * sin(phi); |
---|
1116 | n/a | errno = 0; |
---|
1117 | n/a | } |
---|
1118 | n/a | |
---|
1119 | n/a | PyFPE_END_PROTECT(z) |
---|
1120 | n/a | if (errno != 0) |
---|
1121 | n/a | return math_error(); |
---|
1122 | n/a | else |
---|
1123 | n/a | return PyComplex_FromCComplex(z); |
---|
1124 | n/a | } |
---|
1125 | n/a | |
---|
1126 | n/a | /*[clinic input] |
---|
1127 | n/a | cmath.isfinite = cmath.polar |
---|
1128 | n/a | |
---|
1129 | n/a | Return True if both the real and imaginary parts of z are finite, else False. |
---|
1130 | n/a | [clinic start generated code]*/ |
---|
1131 | n/a | |
---|
1132 | n/a | static PyObject * |
---|
1133 | n/a | cmath_isfinite_impl(PyObject *module, Py_complex z) |
---|
1134 | n/a | /*[clinic end generated code: output=ac76611e2c774a36 input=848e7ee701895815]*/ |
---|
1135 | n/a | { |
---|
1136 | n/a | return PyBool_FromLong(Py_IS_FINITE(z.real) && Py_IS_FINITE(z.imag)); |
---|
1137 | n/a | } |
---|
1138 | n/a | |
---|
1139 | n/a | /*[clinic input] |
---|
1140 | n/a | cmath.isnan = cmath.polar |
---|
1141 | n/a | |
---|
1142 | n/a | Checks if the real or imaginary part of z not a number (NaN). |
---|
1143 | n/a | [clinic start generated code]*/ |
---|
1144 | n/a | |
---|
1145 | n/a | static PyObject * |
---|
1146 | n/a | cmath_isnan_impl(PyObject *module, Py_complex z) |
---|
1147 | n/a | /*[clinic end generated code: output=e7abf6e0b28beab7 input=71799f5d284c9baf]*/ |
---|
1148 | n/a | { |
---|
1149 | n/a | return PyBool_FromLong(Py_IS_NAN(z.real) || Py_IS_NAN(z.imag)); |
---|
1150 | n/a | } |
---|
1151 | n/a | |
---|
1152 | n/a | /*[clinic input] |
---|
1153 | n/a | cmath.isinf = cmath.polar |
---|
1154 | n/a | |
---|
1155 | n/a | Checks if the real or imaginary part of z is infinite. |
---|
1156 | n/a | [clinic start generated code]*/ |
---|
1157 | n/a | |
---|
1158 | n/a | static PyObject * |
---|
1159 | n/a | cmath_isinf_impl(PyObject *module, Py_complex z) |
---|
1160 | n/a | /*[clinic end generated code: output=502a75a79c773469 input=363df155c7181329]*/ |
---|
1161 | n/a | { |
---|
1162 | n/a | return PyBool_FromLong(Py_IS_INFINITY(z.real) || |
---|
1163 | n/a | Py_IS_INFINITY(z.imag)); |
---|
1164 | n/a | } |
---|
1165 | n/a | |
---|
1166 | n/a | /*[clinic input] |
---|
1167 | n/a | cmath.isclose -> bool |
---|
1168 | n/a | |
---|
1169 | n/a | a: Py_complex |
---|
1170 | n/a | b: Py_complex |
---|
1171 | n/a | * |
---|
1172 | n/a | rel_tol: double = 1e-09 |
---|
1173 | n/a | maximum difference for being considered "close", relative to the |
---|
1174 | n/a | magnitude of the input values |
---|
1175 | n/a | abs_tol: double = 0.0 |
---|
1176 | n/a | maximum difference for being considered "close", regardless of the |
---|
1177 | n/a | magnitude of the input values |
---|
1178 | n/a | |
---|
1179 | n/a | Determine whether two complex numbers are close in value. |
---|
1180 | n/a | |
---|
1181 | n/a | Return True if a is close in value to b, and False otherwise. |
---|
1182 | n/a | |
---|
1183 | n/a | For the values to be considered close, the difference between them must be |
---|
1184 | n/a | smaller than at least one of the tolerances. |
---|
1185 | n/a | |
---|
1186 | n/a | -inf, inf and NaN behave similarly to the IEEE 754 Standard. That is, NaN is |
---|
1187 | n/a | not close to anything, even itself. inf and -inf are only close to themselves. |
---|
1188 | n/a | [clinic start generated code]*/ |
---|
1189 | n/a | |
---|
1190 | n/a | static int |
---|
1191 | n/a | cmath_isclose_impl(PyObject *module, Py_complex a, Py_complex b, |
---|
1192 | n/a | double rel_tol, double abs_tol) |
---|
1193 | n/a | /*[clinic end generated code: output=8a2486cc6e0014d1 input=df9636d7de1d4ac3]*/ |
---|
1194 | n/a | { |
---|
1195 | n/a | double diff; |
---|
1196 | n/a | |
---|
1197 | n/a | /* sanity check on the inputs */ |
---|
1198 | n/a | if (rel_tol < 0.0 || abs_tol < 0.0 ) { |
---|
1199 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
1200 | n/a | "tolerances must be non-negative"); |
---|
1201 | n/a | return -1; |
---|
1202 | n/a | } |
---|
1203 | n/a | |
---|
1204 | n/a | if ( (a.real == b.real) && (a.imag == b.imag) ) { |
---|
1205 | n/a | /* short circuit exact equality -- needed to catch two infinities of |
---|
1206 | n/a | the same sign. And perhaps speeds things up a bit sometimes. |
---|
1207 | n/a | */ |
---|
1208 | n/a | return 1; |
---|
1209 | n/a | } |
---|
1210 | n/a | |
---|
1211 | n/a | /* This catches the case of two infinities of opposite sign, or |
---|
1212 | n/a | one infinity and one finite number. Two infinities of opposite |
---|
1213 | n/a | sign would otherwise have an infinite relative tolerance. |
---|
1214 | n/a | Two infinities of the same sign are caught by the equality check |
---|
1215 | n/a | above. |
---|
1216 | n/a | */ |
---|
1217 | n/a | |
---|
1218 | n/a | if (Py_IS_INFINITY(a.real) || Py_IS_INFINITY(a.imag) || |
---|
1219 | n/a | Py_IS_INFINITY(b.real) || Py_IS_INFINITY(b.imag)) { |
---|
1220 | n/a | return 0; |
---|
1221 | n/a | } |
---|
1222 | n/a | |
---|
1223 | n/a | /* now do the regular computation |
---|
1224 | n/a | this is essentially the "weak" test from the Boost library |
---|
1225 | n/a | */ |
---|
1226 | n/a | |
---|
1227 | n/a | diff = _Py_c_abs(_Py_c_diff(a, b)); |
---|
1228 | n/a | |
---|
1229 | n/a | return (((diff <= rel_tol * _Py_c_abs(b)) || |
---|
1230 | n/a | (diff <= rel_tol * _Py_c_abs(a))) || |
---|
1231 | n/a | (diff <= abs_tol)); |
---|
1232 | n/a | } |
---|
1233 | n/a | |
---|
1234 | n/a | PyDoc_STRVAR(module_doc, |
---|
1235 | n/a | "This module is always available. It provides access to mathematical\n" |
---|
1236 | n/a | "functions for complex numbers."); |
---|
1237 | n/a | |
---|
1238 | n/a | static PyMethodDef cmath_methods[] = { |
---|
1239 | n/a | CMATH_ACOS_METHODDEF |
---|
1240 | n/a | CMATH_ACOSH_METHODDEF |
---|
1241 | n/a | CMATH_ASIN_METHODDEF |
---|
1242 | n/a | CMATH_ASINH_METHODDEF |
---|
1243 | n/a | CMATH_ATAN_METHODDEF |
---|
1244 | n/a | CMATH_ATANH_METHODDEF |
---|
1245 | n/a | CMATH_COS_METHODDEF |
---|
1246 | n/a | CMATH_COSH_METHODDEF |
---|
1247 | n/a | CMATH_EXP_METHODDEF |
---|
1248 | n/a | CMATH_ISCLOSE_METHODDEF |
---|
1249 | n/a | CMATH_ISFINITE_METHODDEF |
---|
1250 | n/a | CMATH_ISINF_METHODDEF |
---|
1251 | n/a | CMATH_ISNAN_METHODDEF |
---|
1252 | n/a | CMATH_LOG_METHODDEF |
---|
1253 | n/a | CMATH_LOG10_METHODDEF |
---|
1254 | n/a | CMATH_PHASE_METHODDEF |
---|
1255 | n/a | CMATH_POLAR_METHODDEF |
---|
1256 | n/a | CMATH_RECT_METHODDEF |
---|
1257 | n/a | CMATH_SIN_METHODDEF |
---|
1258 | n/a | CMATH_SINH_METHODDEF |
---|
1259 | n/a | CMATH_SQRT_METHODDEF |
---|
1260 | n/a | CMATH_TAN_METHODDEF |
---|
1261 | n/a | CMATH_TANH_METHODDEF |
---|
1262 | n/a | {NULL, NULL} /* sentinel */ |
---|
1263 | n/a | }; |
---|
1264 | n/a | |
---|
1265 | n/a | |
---|
1266 | n/a | static struct PyModuleDef cmathmodule = { |
---|
1267 | n/a | PyModuleDef_HEAD_INIT, |
---|
1268 | n/a | "cmath", |
---|
1269 | n/a | module_doc, |
---|
1270 | n/a | -1, |
---|
1271 | n/a | cmath_methods, |
---|
1272 | n/a | NULL, |
---|
1273 | n/a | NULL, |
---|
1274 | n/a | NULL, |
---|
1275 | n/a | NULL |
---|
1276 | n/a | }; |
---|
1277 | n/a | |
---|
1278 | n/a | PyMODINIT_FUNC |
---|
1279 | n/a | PyInit_cmath(void) |
---|
1280 | n/a | { |
---|
1281 | n/a | PyObject *m; |
---|
1282 | n/a | |
---|
1283 | n/a | m = PyModule_Create(&cmathmodule); |
---|
1284 | n/a | if (m == NULL) |
---|
1285 | n/a | return NULL; |
---|
1286 | n/a | |
---|
1287 | n/a | PyModule_AddObject(m, "pi", |
---|
1288 | n/a | PyFloat_FromDouble(Py_MATH_PI)); |
---|
1289 | n/a | PyModule_AddObject(m, "e", PyFloat_FromDouble(Py_MATH_E)); |
---|
1290 | n/a | PyModule_AddObject(m, "tau", PyFloat_FromDouble(Py_MATH_TAU)); /* 2pi */ |
---|
1291 | n/a | PyModule_AddObject(m, "inf", PyFloat_FromDouble(m_inf())); |
---|
1292 | n/a | PyModule_AddObject(m, "infj", PyComplex_FromCComplex(c_infj())); |
---|
1293 | n/a | #if !defined(PY_NO_SHORT_FLOAT_REPR) || defined(Py_NAN) |
---|
1294 | n/a | PyModule_AddObject(m, "nan", PyFloat_FromDouble(m_nan())); |
---|
1295 | n/a | PyModule_AddObject(m, "nanj", PyComplex_FromCComplex(c_nanj())); |
---|
1296 | n/a | #endif |
---|
1297 | n/a | |
---|
1298 | n/a | /* initialize special value tables */ |
---|
1299 | n/a | |
---|
1300 | n/a | #define INIT_SPECIAL_VALUES(NAME, BODY) { Py_complex* p = (Py_complex*)NAME; BODY } |
---|
1301 | n/a | #define C(REAL, IMAG) p->real = REAL; p->imag = IMAG; ++p; |
---|
1302 | n/a | |
---|
1303 | n/a | INIT_SPECIAL_VALUES(acos_special_values, { |
---|
1304 | n/a | C(P34,INF) C(P,INF) C(P,INF) C(P,-INF) C(P,-INF) C(P34,-INF) C(N,INF) |
---|
1305 | n/a | C(P12,INF) C(U,U) C(U,U) C(U,U) C(U,U) C(P12,-INF) C(N,N) |
---|
1306 | n/a | C(P12,INF) C(U,U) C(P12,0.) C(P12,-0.) C(U,U) C(P12,-INF) C(P12,N) |
---|
1307 | n/a | C(P12,INF) C(U,U) C(P12,0.) C(P12,-0.) C(U,U) C(P12,-INF) C(P12,N) |
---|
1308 | n/a | C(P12,INF) C(U,U) C(U,U) C(U,U) C(U,U) C(P12,-INF) C(N,N) |
---|
1309 | n/a | C(P14,INF) C(0.,INF) C(0.,INF) C(0.,-INF) C(0.,-INF) C(P14,-INF) C(N,INF) |
---|
1310 | n/a | C(N,INF) C(N,N) C(N,N) C(N,N) C(N,N) C(N,-INF) C(N,N) |
---|
1311 | n/a | }) |
---|
1312 | n/a | |
---|
1313 | n/a | INIT_SPECIAL_VALUES(acosh_special_values, { |
---|
1314 | n/a | C(INF,-P34) C(INF,-P) C(INF,-P) C(INF,P) C(INF,P) C(INF,P34) C(INF,N) |
---|
1315 | n/a | C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N) |
---|
1316 | n/a | C(INF,-P12) C(U,U) C(0.,-P12) C(0.,P12) C(U,U) C(INF,P12) C(N,N) |
---|
1317 | n/a | C(INF,-P12) C(U,U) C(0.,-P12) C(0.,P12) C(U,U) C(INF,P12) C(N,N) |
---|
1318 | n/a | C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N) |
---|
1319 | n/a | C(INF,-P14) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,P14) C(INF,N) |
---|
1320 | n/a | C(INF,N) C(N,N) C(N,N) C(N,N) C(N,N) C(INF,N) C(N,N) |
---|
1321 | n/a | }) |
---|
1322 | n/a | |
---|
1323 | n/a | INIT_SPECIAL_VALUES(asinh_special_values, { |
---|
1324 | n/a | C(-INF,-P14) C(-INF,-0.) C(-INF,-0.) C(-INF,0.) C(-INF,0.) C(-INF,P14) C(-INF,N) |
---|
1325 | n/a | C(-INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(-INF,P12) C(N,N) |
---|
1326 | n/a | C(-INF,-P12) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(-INF,P12) C(N,N) |
---|
1327 | n/a | C(INF,-P12) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(INF,P12) C(N,N) |
---|
1328 | n/a | C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N) |
---|
1329 | n/a | C(INF,-P14) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,P14) C(INF,N) |
---|
1330 | n/a | C(INF,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(INF,N) C(N,N) |
---|
1331 | n/a | }) |
---|
1332 | n/a | |
---|
1333 | n/a | INIT_SPECIAL_VALUES(atanh_special_values, { |
---|
1334 | n/a | C(-0.,-P12) C(-0.,-P12) C(-0.,-P12) C(-0.,P12) C(-0.,P12) C(-0.,P12) C(-0.,N) |
---|
1335 | n/a | C(-0.,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(-0.,P12) C(N,N) |
---|
1336 | n/a | C(-0.,-P12) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(-0.,P12) C(-0.,N) |
---|
1337 | n/a | C(0.,-P12) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,P12) C(0.,N) |
---|
1338 | n/a | C(0.,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(0.,P12) C(N,N) |
---|
1339 | n/a | C(0.,-P12) C(0.,-P12) C(0.,-P12) C(0.,P12) C(0.,P12) C(0.,P12) C(0.,N) |
---|
1340 | n/a | C(0.,-P12) C(N,N) C(N,N) C(N,N) C(N,N) C(0.,P12) C(N,N) |
---|
1341 | n/a | }) |
---|
1342 | n/a | |
---|
1343 | n/a | INIT_SPECIAL_VALUES(cosh_special_values, { |
---|
1344 | n/a | C(INF,N) C(U,U) C(INF,0.) C(INF,-0.) C(U,U) C(INF,N) C(INF,N) |
---|
1345 | n/a | C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N) |
---|
1346 | n/a | C(N,0.) C(U,U) C(1.,0.) C(1.,-0.) C(U,U) C(N,0.) C(N,0.) |
---|
1347 | n/a | C(N,0.) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(N,0.) C(N,0.) |
---|
1348 | n/a | C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N) |
---|
1349 | n/a | C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N) |
---|
1350 | n/a | C(N,N) C(N,N) C(N,0.) C(N,0.) C(N,N) C(N,N) C(N,N) |
---|
1351 | n/a | }) |
---|
1352 | n/a | |
---|
1353 | n/a | INIT_SPECIAL_VALUES(exp_special_values, { |
---|
1354 | n/a | C(0.,0.) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,0.) C(0.,0.) |
---|
1355 | n/a | C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N) |
---|
1356 | n/a | C(N,N) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(N,N) C(N,N) |
---|
1357 | n/a | C(N,N) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(N,N) C(N,N) |
---|
1358 | n/a | C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N) |
---|
1359 | n/a | C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N) |
---|
1360 | n/a | C(N,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(N,N) C(N,N) |
---|
1361 | n/a | }) |
---|
1362 | n/a | |
---|
1363 | n/a | INIT_SPECIAL_VALUES(log_special_values, { |
---|
1364 | n/a | C(INF,-P34) C(INF,-P) C(INF,-P) C(INF,P) C(INF,P) C(INF,P34) C(INF,N) |
---|
1365 | n/a | C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N) |
---|
1366 | n/a | C(INF,-P12) C(U,U) C(-INF,-P) C(-INF,P) C(U,U) C(INF,P12) C(N,N) |
---|
1367 | n/a | C(INF,-P12) C(U,U) C(-INF,-0.) C(-INF,0.) C(U,U) C(INF,P12) C(N,N) |
---|
1368 | n/a | C(INF,-P12) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,P12) C(N,N) |
---|
1369 | n/a | C(INF,-P14) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,P14) C(INF,N) |
---|
1370 | n/a | C(INF,N) C(N,N) C(N,N) C(N,N) C(N,N) C(INF,N) C(N,N) |
---|
1371 | n/a | }) |
---|
1372 | n/a | |
---|
1373 | n/a | INIT_SPECIAL_VALUES(sinh_special_values, { |
---|
1374 | n/a | C(INF,N) C(U,U) C(-INF,-0.) C(-INF,0.) C(U,U) C(INF,N) C(INF,N) |
---|
1375 | n/a | C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N) |
---|
1376 | n/a | C(0.,N) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(0.,N) C(0.,N) |
---|
1377 | n/a | C(0.,N) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,N) C(0.,N) |
---|
1378 | n/a | C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N) |
---|
1379 | n/a | C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N) |
---|
1380 | n/a | C(N,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(N,N) C(N,N) |
---|
1381 | n/a | }) |
---|
1382 | n/a | |
---|
1383 | n/a | INIT_SPECIAL_VALUES(sqrt_special_values, { |
---|
1384 | n/a | C(INF,-INF) C(0.,-INF) C(0.,-INF) C(0.,INF) C(0.,INF) C(INF,INF) C(N,INF) |
---|
1385 | n/a | C(INF,-INF) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,INF) C(N,N) |
---|
1386 | n/a | C(INF,-INF) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(INF,INF) C(N,N) |
---|
1387 | n/a | C(INF,-INF) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(INF,INF) C(N,N) |
---|
1388 | n/a | C(INF,-INF) C(U,U) C(U,U) C(U,U) C(U,U) C(INF,INF) C(N,N) |
---|
1389 | n/a | C(INF,-INF) C(INF,-0.) C(INF,-0.) C(INF,0.) C(INF,0.) C(INF,INF) C(INF,N) |
---|
1390 | n/a | C(INF,-INF) C(N,N) C(N,N) C(N,N) C(N,N) C(INF,INF) C(N,N) |
---|
1391 | n/a | }) |
---|
1392 | n/a | |
---|
1393 | n/a | INIT_SPECIAL_VALUES(tanh_special_values, { |
---|
1394 | n/a | C(-1.,0.) C(U,U) C(-1.,-0.) C(-1.,0.) C(U,U) C(-1.,0.) C(-1.,0.) |
---|
1395 | n/a | C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N) |
---|
1396 | n/a | C(N,N) C(U,U) C(-0.,-0.) C(-0.,0.) C(U,U) C(N,N) C(N,N) |
---|
1397 | n/a | C(N,N) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(N,N) C(N,N) |
---|
1398 | n/a | C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N) |
---|
1399 | n/a | C(1.,0.) C(U,U) C(1.,-0.) C(1.,0.) C(U,U) C(1.,0.) C(1.,0.) |
---|
1400 | n/a | C(N,N) C(N,N) C(N,-0.) C(N,0.) C(N,N) C(N,N) C(N,N) |
---|
1401 | n/a | }) |
---|
1402 | n/a | |
---|
1403 | n/a | INIT_SPECIAL_VALUES(rect_special_values, { |
---|
1404 | n/a | C(INF,N) C(U,U) C(-INF,0.) C(-INF,-0.) C(U,U) C(INF,N) C(INF,N) |
---|
1405 | n/a | C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N) |
---|
1406 | n/a | C(0.,0.) C(U,U) C(-0.,0.) C(-0.,-0.) C(U,U) C(0.,0.) C(0.,0.) |
---|
1407 | n/a | C(0.,0.) C(U,U) C(0.,-0.) C(0.,0.) C(U,U) C(0.,0.) C(0.,0.) |
---|
1408 | n/a | C(N,N) C(U,U) C(U,U) C(U,U) C(U,U) C(N,N) C(N,N) |
---|
1409 | n/a | C(INF,N) C(U,U) C(INF,-0.) C(INF,0.) C(U,U) C(INF,N) C(INF,N) |
---|
1410 | n/a | C(N,N) C(N,N) C(N,0.) C(N,0.) C(N,N) C(N,N) C(N,N) |
---|
1411 | n/a | }) |
---|
1412 | n/a | return m; |
---|
1413 | n/a | } |
---|