1 | n/a | |
---|
2 | n/a | /* Complex object implementation */ |
---|
3 | n/a | |
---|
4 | n/a | /* Borrows heavily from floatobject.c */ |
---|
5 | n/a | |
---|
6 | n/a | /* Submitted by Jim Hugunin */ |
---|
7 | n/a | |
---|
8 | n/a | #include "Python.h" |
---|
9 | n/a | #include "structmember.h" |
---|
10 | n/a | |
---|
11 | n/a | /* elementary operations on complex numbers */ |
---|
12 | n/a | |
---|
13 | n/a | static Py_complex c_1 = {1., 0.}; |
---|
14 | n/a | |
---|
15 | n/a | Py_complex |
---|
16 | n/a | _Py_c_sum(Py_complex a, Py_complex b) |
---|
17 | n/a | { |
---|
18 | n/a | Py_complex r; |
---|
19 | n/a | r.real = a.real + b.real; |
---|
20 | n/a | r.imag = a.imag + b.imag; |
---|
21 | n/a | return r; |
---|
22 | n/a | } |
---|
23 | n/a | |
---|
24 | n/a | Py_complex |
---|
25 | n/a | _Py_c_diff(Py_complex a, Py_complex b) |
---|
26 | n/a | { |
---|
27 | n/a | Py_complex r; |
---|
28 | n/a | r.real = a.real - b.real; |
---|
29 | n/a | r.imag = a.imag - b.imag; |
---|
30 | n/a | return r; |
---|
31 | n/a | } |
---|
32 | n/a | |
---|
33 | n/a | Py_complex |
---|
34 | n/a | _Py_c_neg(Py_complex a) |
---|
35 | n/a | { |
---|
36 | n/a | Py_complex r; |
---|
37 | n/a | r.real = -a.real; |
---|
38 | n/a | r.imag = -a.imag; |
---|
39 | n/a | return r; |
---|
40 | n/a | } |
---|
41 | n/a | |
---|
42 | n/a | Py_complex |
---|
43 | n/a | _Py_c_prod(Py_complex a, Py_complex b) |
---|
44 | n/a | { |
---|
45 | n/a | Py_complex r; |
---|
46 | n/a | r.real = a.real*b.real - a.imag*b.imag; |
---|
47 | n/a | r.imag = a.real*b.imag + a.imag*b.real; |
---|
48 | n/a | return r; |
---|
49 | n/a | } |
---|
50 | n/a | |
---|
51 | n/a | Py_complex |
---|
52 | n/a | _Py_c_quot(Py_complex a, Py_complex b) |
---|
53 | n/a | { |
---|
54 | n/a | /****************************************************************** |
---|
55 | n/a | This was the original algorithm. It's grossly prone to spurious |
---|
56 | n/a | overflow and underflow errors. It also merrily divides by 0 despite |
---|
57 | n/a | checking for that(!). The code still serves a doc purpose here, as |
---|
58 | n/a | the algorithm following is a simple by-cases transformation of this |
---|
59 | n/a | one: |
---|
60 | n/a | |
---|
61 | n/a | Py_complex r; |
---|
62 | n/a | double d = b.real*b.real + b.imag*b.imag; |
---|
63 | n/a | if (d == 0.) |
---|
64 | n/a | errno = EDOM; |
---|
65 | n/a | r.real = (a.real*b.real + a.imag*b.imag)/d; |
---|
66 | n/a | r.imag = (a.imag*b.real - a.real*b.imag)/d; |
---|
67 | n/a | return r; |
---|
68 | n/a | ******************************************************************/ |
---|
69 | n/a | |
---|
70 | n/a | /* This algorithm is better, and is pretty obvious: first divide the |
---|
71 | n/a | * numerators and denominator by whichever of {b.real, b.imag} has |
---|
72 | n/a | * larger magnitude. The earliest reference I found was to CACM |
---|
73 | n/a | * Algorithm 116 (Complex Division, Robert L. Smith, Stanford |
---|
74 | n/a | * University). As usual, though, we're still ignoring all IEEE |
---|
75 | n/a | * endcases. |
---|
76 | n/a | */ |
---|
77 | n/a | Py_complex r; /* the result */ |
---|
78 | n/a | const double abs_breal = b.real < 0 ? -b.real : b.real; |
---|
79 | n/a | const double abs_bimag = b.imag < 0 ? -b.imag : b.imag; |
---|
80 | n/a | |
---|
81 | n/a | if (abs_breal >= abs_bimag) { |
---|
82 | n/a | /* divide tops and bottom by b.real */ |
---|
83 | n/a | if (abs_breal == 0.0) { |
---|
84 | n/a | errno = EDOM; |
---|
85 | n/a | r.real = r.imag = 0.0; |
---|
86 | n/a | } |
---|
87 | n/a | else { |
---|
88 | n/a | const double ratio = b.imag / b.real; |
---|
89 | n/a | const double denom = b.real + b.imag * ratio; |
---|
90 | n/a | r.real = (a.real + a.imag * ratio) / denom; |
---|
91 | n/a | r.imag = (a.imag - a.real * ratio) / denom; |
---|
92 | n/a | } |
---|
93 | n/a | } |
---|
94 | n/a | else if (abs_bimag >= abs_breal) { |
---|
95 | n/a | /* divide tops and bottom by b.imag */ |
---|
96 | n/a | const double ratio = b.real / b.imag; |
---|
97 | n/a | const double denom = b.real * ratio + b.imag; |
---|
98 | n/a | assert(b.imag != 0.0); |
---|
99 | n/a | r.real = (a.real * ratio + a.imag) / denom; |
---|
100 | n/a | r.imag = (a.imag * ratio - a.real) / denom; |
---|
101 | n/a | } |
---|
102 | n/a | else { |
---|
103 | n/a | /* At least one of b.real or b.imag is a NaN */ |
---|
104 | n/a | r.real = r.imag = Py_NAN; |
---|
105 | n/a | } |
---|
106 | n/a | return r; |
---|
107 | n/a | } |
---|
108 | n/a | |
---|
109 | n/a | Py_complex |
---|
110 | n/a | _Py_c_pow(Py_complex a, Py_complex b) |
---|
111 | n/a | { |
---|
112 | n/a | Py_complex r; |
---|
113 | n/a | double vabs,len,at,phase; |
---|
114 | n/a | if (b.real == 0. && b.imag == 0.) { |
---|
115 | n/a | r.real = 1.; |
---|
116 | n/a | r.imag = 0.; |
---|
117 | n/a | } |
---|
118 | n/a | else if (a.real == 0. && a.imag == 0.) { |
---|
119 | n/a | if (b.imag != 0. || b.real < 0.) |
---|
120 | n/a | errno = EDOM; |
---|
121 | n/a | r.real = 0.; |
---|
122 | n/a | r.imag = 0.; |
---|
123 | n/a | } |
---|
124 | n/a | else { |
---|
125 | n/a | vabs = hypot(a.real,a.imag); |
---|
126 | n/a | len = pow(vabs,b.real); |
---|
127 | n/a | at = atan2(a.imag, a.real); |
---|
128 | n/a | phase = at*b.real; |
---|
129 | n/a | if (b.imag != 0.0) { |
---|
130 | n/a | len /= exp(at*b.imag); |
---|
131 | n/a | phase += b.imag*log(vabs); |
---|
132 | n/a | } |
---|
133 | n/a | r.real = len*cos(phase); |
---|
134 | n/a | r.imag = len*sin(phase); |
---|
135 | n/a | } |
---|
136 | n/a | return r; |
---|
137 | n/a | } |
---|
138 | n/a | |
---|
139 | n/a | static Py_complex |
---|
140 | n/a | c_powu(Py_complex x, long n) |
---|
141 | n/a | { |
---|
142 | n/a | Py_complex r, p; |
---|
143 | n/a | long mask = 1; |
---|
144 | n/a | r = c_1; |
---|
145 | n/a | p = x; |
---|
146 | n/a | while (mask > 0 && n >= mask) { |
---|
147 | n/a | if (n & mask) |
---|
148 | n/a | r = _Py_c_prod(r,p); |
---|
149 | n/a | mask <<= 1; |
---|
150 | n/a | p = _Py_c_prod(p,p); |
---|
151 | n/a | } |
---|
152 | n/a | return r; |
---|
153 | n/a | } |
---|
154 | n/a | |
---|
155 | n/a | static Py_complex |
---|
156 | n/a | c_powi(Py_complex x, long n) |
---|
157 | n/a | { |
---|
158 | n/a | Py_complex cn; |
---|
159 | n/a | |
---|
160 | n/a | if (n > 100 || n < -100) { |
---|
161 | n/a | cn.real = (double) n; |
---|
162 | n/a | cn.imag = 0.; |
---|
163 | n/a | return _Py_c_pow(x,cn); |
---|
164 | n/a | } |
---|
165 | n/a | else if (n > 0) |
---|
166 | n/a | return c_powu(x,n); |
---|
167 | n/a | else |
---|
168 | n/a | return _Py_c_quot(c_1, c_powu(x,-n)); |
---|
169 | n/a | |
---|
170 | n/a | } |
---|
171 | n/a | |
---|
172 | n/a | double |
---|
173 | n/a | _Py_c_abs(Py_complex z) |
---|
174 | n/a | { |
---|
175 | n/a | /* sets errno = ERANGE on overflow; otherwise errno = 0 */ |
---|
176 | n/a | double result; |
---|
177 | n/a | |
---|
178 | n/a | if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) { |
---|
179 | n/a | /* C99 rules: if either the real or the imaginary part is an |
---|
180 | n/a | infinity, return infinity, even if the other part is a |
---|
181 | n/a | NaN. */ |
---|
182 | n/a | if (Py_IS_INFINITY(z.real)) { |
---|
183 | n/a | result = fabs(z.real); |
---|
184 | n/a | errno = 0; |
---|
185 | n/a | return result; |
---|
186 | n/a | } |
---|
187 | n/a | if (Py_IS_INFINITY(z.imag)) { |
---|
188 | n/a | result = fabs(z.imag); |
---|
189 | n/a | errno = 0; |
---|
190 | n/a | return result; |
---|
191 | n/a | } |
---|
192 | n/a | /* either the real or imaginary part is a NaN, |
---|
193 | n/a | and neither is infinite. Result should be NaN. */ |
---|
194 | n/a | return Py_NAN; |
---|
195 | n/a | } |
---|
196 | n/a | result = hypot(z.real, z.imag); |
---|
197 | n/a | if (!Py_IS_FINITE(result)) |
---|
198 | n/a | errno = ERANGE; |
---|
199 | n/a | else |
---|
200 | n/a | errno = 0; |
---|
201 | n/a | return result; |
---|
202 | n/a | } |
---|
203 | n/a | |
---|
204 | n/a | static PyObject * |
---|
205 | n/a | complex_subtype_from_c_complex(PyTypeObject *type, Py_complex cval) |
---|
206 | n/a | { |
---|
207 | n/a | PyObject *op; |
---|
208 | n/a | |
---|
209 | n/a | op = type->tp_alloc(type, 0); |
---|
210 | n/a | if (op != NULL) |
---|
211 | n/a | ((PyComplexObject *)op)->cval = cval; |
---|
212 | n/a | return op; |
---|
213 | n/a | } |
---|
214 | n/a | |
---|
215 | n/a | PyObject * |
---|
216 | n/a | PyComplex_FromCComplex(Py_complex cval) |
---|
217 | n/a | { |
---|
218 | n/a | PyComplexObject *op; |
---|
219 | n/a | |
---|
220 | n/a | /* Inline PyObject_New */ |
---|
221 | n/a | op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject)); |
---|
222 | n/a | if (op == NULL) |
---|
223 | n/a | return PyErr_NoMemory(); |
---|
224 | n/a | (void)PyObject_INIT(op, &PyComplex_Type); |
---|
225 | n/a | op->cval = cval; |
---|
226 | n/a | return (PyObject *) op; |
---|
227 | n/a | } |
---|
228 | n/a | |
---|
229 | n/a | static PyObject * |
---|
230 | n/a | complex_subtype_from_doubles(PyTypeObject *type, double real, double imag) |
---|
231 | n/a | { |
---|
232 | n/a | Py_complex c; |
---|
233 | n/a | c.real = real; |
---|
234 | n/a | c.imag = imag; |
---|
235 | n/a | return complex_subtype_from_c_complex(type, c); |
---|
236 | n/a | } |
---|
237 | n/a | |
---|
238 | n/a | PyObject * |
---|
239 | n/a | PyComplex_FromDoubles(double real, double imag) |
---|
240 | n/a | { |
---|
241 | n/a | Py_complex c; |
---|
242 | n/a | c.real = real; |
---|
243 | n/a | c.imag = imag; |
---|
244 | n/a | return PyComplex_FromCComplex(c); |
---|
245 | n/a | } |
---|
246 | n/a | |
---|
247 | n/a | double |
---|
248 | n/a | PyComplex_RealAsDouble(PyObject *op) |
---|
249 | n/a | { |
---|
250 | n/a | if (PyComplex_Check(op)) { |
---|
251 | n/a | return ((PyComplexObject *)op)->cval.real; |
---|
252 | n/a | } |
---|
253 | n/a | else { |
---|
254 | n/a | return PyFloat_AsDouble(op); |
---|
255 | n/a | } |
---|
256 | n/a | } |
---|
257 | n/a | |
---|
258 | n/a | double |
---|
259 | n/a | PyComplex_ImagAsDouble(PyObject *op) |
---|
260 | n/a | { |
---|
261 | n/a | if (PyComplex_Check(op)) { |
---|
262 | n/a | return ((PyComplexObject *)op)->cval.imag; |
---|
263 | n/a | } |
---|
264 | n/a | else { |
---|
265 | n/a | return 0.0; |
---|
266 | n/a | } |
---|
267 | n/a | } |
---|
268 | n/a | |
---|
269 | n/a | static PyObject * |
---|
270 | n/a | try_complex_special_method(PyObject *op) { |
---|
271 | n/a | PyObject *f; |
---|
272 | n/a | _Py_IDENTIFIER(__complex__); |
---|
273 | n/a | |
---|
274 | n/a | f = _PyObject_LookupSpecial(op, &PyId___complex__); |
---|
275 | n/a | if (f) { |
---|
276 | n/a | PyObject *res = _PyObject_CallNoArg(f); |
---|
277 | n/a | Py_DECREF(f); |
---|
278 | n/a | if (res != NULL && !PyComplex_Check(res)) { |
---|
279 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
280 | n/a | "__complex__ should return a complex object"); |
---|
281 | n/a | Py_DECREF(res); |
---|
282 | n/a | return NULL; |
---|
283 | n/a | } |
---|
284 | n/a | return res; |
---|
285 | n/a | } |
---|
286 | n/a | return NULL; |
---|
287 | n/a | } |
---|
288 | n/a | |
---|
289 | n/a | Py_complex |
---|
290 | n/a | PyComplex_AsCComplex(PyObject *op) |
---|
291 | n/a | { |
---|
292 | n/a | Py_complex cv; |
---|
293 | n/a | PyObject *newop = NULL; |
---|
294 | n/a | |
---|
295 | n/a | assert(op); |
---|
296 | n/a | /* If op is already of type PyComplex_Type, return its value */ |
---|
297 | n/a | if (PyComplex_Check(op)) { |
---|
298 | n/a | return ((PyComplexObject *)op)->cval; |
---|
299 | n/a | } |
---|
300 | n/a | /* If not, use op's __complex__ method, if it exists */ |
---|
301 | n/a | |
---|
302 | n/a | /* return -1 on failure */ |
---|
303 | n/a | cv.real = -1.; |
---|
304 | n/a | cv.imag = 0.; |
---|
305 | n/a | |
---|
306 | n/a | newop = try_complex_special_method(op); |
---|
307 | n/a | |
---|
308 | n/a | if (newop) { |
---|
309 | n/a | cv = ((PyComplexObject *)newop)->cval; |
---|
310 | n/a | Py_DECREF(newop); |
---|
311 | n/a | return cv; |
---|
312 | n/a | } |
---|
313 | n/a | else if (PyErr_Occurred()) { |
---|
314 | n/a | return cv; |
---|
315 | n/a | } |
---|
316 | n/a | /* If neither of the above works, interpret op as a float giving the |
---|
317 | n/a | real part of the result, and fill in the imaginary part as 0. */ |
---|
318 | n/a | else { |
---|
319 | n/a | /* PyFloat_AsDouble will return -1 on failure */ |
---|
320 | n/a | cv.real = PyFloat_AsDouble(op); |
---|
321 | n/a | return cv; |
---|
322 | n/a | } |
---|
323 | n/a | } |
---|
324 | n/a | |
---|
325 | n/a | static void |
---|
326 | n/a | complex_dealloc(PyObject *op) |
---|
327 | n/a | { |
---|
328 | n/a | op->ob_type->tp_free(op); |
---|
329 | n/a | } |
---|
330 | n/a | |
---|
331 | n/a | static PyObject * |
---|
332 | n/a | complex_repr(PyComplexObject *v) |
---|
333 | n/a | { |
---|
334 | n/a | int precision = 0; |
---|
335 | n/a | char format_code = 'r'; |
---|
336 | n/a | PyObject *result = NULL; |
---|
337 | n/a | |
---|
338 | n/a | /* If these are non-NULL, they'll need to be freed. */ |
---|
339 | n/a | char *pre = NULL; |
---|
340 | n/a | char *im = NULL; |
---|
341 | n/a | |
---|
342 | n/a | /* These do not need to be freed. re is either an alias |
---|
343 | n/a | for pre or a pointer to a constant. lead and tail |
---|
344 | n/a | are pointers to constants. */ |
---|
345 | n/a | char *re = NULL; |
---|
346 | n/a | char *lead = ""; |
---|
347 | n/a | char *tail = ""; |
---|
348 | n/a | |
---|
349 | n/a | if (v->cval.real == 0. && copysign(1.0, v->cval.real)==1.0) { |
---|
350 | n/a | /* Real part is +0: just output the imaginary part and do not |
---|
351 | n/a | include parens. */ |
---|
352 | n/a | re = ""; |
---|
353 | n/a | im = PyOS_double_to_string(v->cval.imag, format_code, |
---|
354 | n/a | precision, 0, NULL); |
---|
355 | n/a | if (!im) { |
---|
356 | n/a | PyErr_NoMemory(); |
---|
357 | n/a | goto done; |
---|
358 | n/a | } |
---|
359 | n/a | } else { |
---|
360 | n/a | /* Format imaginary part with sign, real part without. Include |
---|
361 | n/a | parens in the result. */ |
---|
362 | n/a | pre = PyOS_double_to_string(v->cval.real, format_code, |
---|
363 | n/a | precision, 0, NULL); |
---|
364 | n/a | if (!pre) { |
---|
365 | n/a | PyErr_NoMemory(); |
---|
366 | n/a | goto done; |
---|
367 | n/a | } |
---|
368 | n/a | re = pre; |
---|
369 | n/a | |
---|
370 | n/a | im = PyOS_double_to_string(v->cval.imag, format_code, |
---|
371 | n/a | precision, Py_DTSF_SIGN, NULL); |
---|
372 | n/a | if (!im) { |
---|
373 | n/a | PyErr_NoMemory(); |
---|
374 | n/a | goto done; |
---|
375 | n/a | } |
---|
376 | n/a | lead = "("; |
---|
377 | n/a | tail = ")"; |
---|
378 | n/a | } |
---|
379 | n/a | result = PyUnicode_FromFormat("%s%s%sj%s", lead, re, im, tail); |
---|
380 | n/a | done: |
---|
381 | n/a | PyMem_Free(im); |
---|
382 | n/a | PyMem_Free(pre); |
---|
383 | n/a | |
---|
384 | n/a | return result; |
---|
385 | n/a | } |
---|
386 | n/a | |
---|
387 | n/a | static Py_hash_t |
---|
388 | n/a | complex_hash(PyComplexObject *v) |
---|
389 | n/a | { |
---|
390 | n/a | Py_uhash_t hashreal, hashimag, combined; |
---|
391 | n/a | hashreal = (Py_uhash_t)_Py_HashDouble(v->cval.real); |
---|
392 | n/a | if (hashreal == (Py_uhash_t)-1) |
---|
393 | n/a | return -1; |
---|
394 | n/a | hashimag = (Py_uhash_t)_Py_HashDouble(v->cval.imag); |
---|
395 | n/a | if (hashimag == (Py_uhash_t)-1) |
---|
396 | n/a | return -1; |
---|
397 | n/a | /* Note: if the imaginary part is 0, hashimag is 0 now, |
---|
398 | n/a | * so the following returns hashreal unchanged. This is |
---|
399 | n/a | * important because numbers of different types that |
---|
400 | n/a | * compare equal must have the same hash value, so that |
---|
401 | n/a | * hash(x + 0*j) must equal hash(x). |
---|
402 | n/a | */ |
---|
403 | n/a | combined = hashreal + _PyHASH_IMAG * hashimag; |
---|
404 | n/a | if (combined == (Py_uhash_t)-1) |
---|
405 | n/a | combined = (Py_uhash_t)-2; |
---|
406 | n/a | return (Py_hash_t)combined; |
---|
407 | n/a | } |
---|
408 | n/a | |
---|
409 | n/a | /* This macro may return! */ |
---|
410 | n/a | #define TO_COMPLEX(obj, c) \ |
---|
411 | n/a | if (PyComplex_Check(obj)) \ |
---|
412 | n/a | c = ((PyComplexObject *)(obj))->cval; \ |
---|
413 | n/a | else if (to_complex(&(obj), &(c)) < 0) \ |
---|
414 | n/a | return (obj) |
---|
415 | n/a | |
---|
416 | n/a | static int |
---|
417 | n/a | to_complex(PyObject **pobj, Py_complex *pc) |
---|
418 | n/a | { |
---|
419 | n/a | PyObject *obj = *pobj; |
---|
420 | n/a | |
---|
421 | n/a | pc->real = pc->imag = 0.0; |
---|
422 | n/a | if (PyLong_Check(obj)) { |
---|
423 | n/a | pc->real = PyLong_AsDouble(obj); |
---|
424 | n/a | if (pc->real == -1.0 && PyErr_Occurred()) { |
---|
425 | n/a | *pobj = NULL; |
---|
426 | n/a | return -1; |
---|
427 | n/a | } |
---|
428 | n/a | return 0; |
---|
429 | n/a | } |
---|
430 | n/a | if (PyFloat_Check(obj)) { |
---|
431 | n/a | pc->real = PyFloat_AsDouble(obj); |
---|
432 | n/a | return 0; |
---|
433 | n/a | } |
---|
434 | n/a | Py_INCREF(Py_NotImplemented); |
---|
435 | n/a | *pobj = Py_NotImplemented; |
---|
436 | n/a | return -1; |
---|
437 | n/a | } |
---|
438 | n/a | |
---|
439 | n/a | |
---|
440 | n/a | static PyObject * |
---|
441 | n/a | complex_add(PyObject *v, PyObject *w) |
---|
442 | n/a | { |
---|
443 | n/a | Py_complex result; |
---|
444 | n/a | Py_complex a, b; |
---|
445 | n/a | TO_COMPLEX(v, a); |
---|
446 | n/a | TO_COMPLEX(w, b); |
---|
447 | n/a | PyFPE_START_PROTECT("complex_add", return 0) |
---|
448 | n/a | result = _Py_c_sum(a, b); |
---|
449 | n/a | PyFPE_END_PROTECT(result) |
---|
450 | n/a | return PyComplex_FromCComplex(result); |
---|
451 | n/a | } |
---|
452 | n/a | |
---|
453 | n/a | static PyObject * |
---|
454 | n/a | complex_sub(PyObject *v, PyObject *w) |
---|
455 | n/a | { |
---|
456 | n/a | Py_complex result; |
---|
457 | n/a | Py_complex a, b; |
---|
458 | n/a | TO_COMPLEX(v, a); |
---|
459 | n/a | TO_COMPLEX(w, b); |
---|
460 | n/a | PyFPE_START_PROTECT("complex_sub", return 0) |
---|
461 | n/a | result = _Py_c_diff(a, b); |
---|
462 | n/a | PyFPE_END_PROTECT(result) |
---|
463 | n/a | return PyComplex_FromCComplex(result); |
---|
464 | n/a | } |
---|
465 | n/a | |
---|
466 | n/a | static PyObject * |
---|
467 | n/a | complex_mul(PyObject *v, PyObject *w) |
---|
468 | n/a | { |
---|
469 | n/a | Py_complex result; |
---|
470 | n/a | Py_complex a, b; |
---|
471 | n/a | TO_COMPLEX(v, a); |
---|
472 | n/a | TO_COMPLEX(w, b); |
---|
473 | n/a | PyFPE_START_PROTECT("complex_mul", return 0) |
---|
474 | n/a | result = _Py_c_prod(a, b); |
---|
475 | n/a | PyFPE_END_PROTECT(result) |
---|
476 | n/a | return PyComplex_FromCComplex(result); |
---|
477 | n/a | } |
---|
478 | n/a | |
---|
479 | n/a | static PyObject * |
---|
480 | n/a | complex_div(PyObject *v, PyObject *w) |
---|
481 | n/a | { |
---|
482 | n/a | Py_complex quot; |
---|
483 | n/a | Py_complex a, b; |
---|
484 | n/a | TO_COMPLEX(v, a); |
---|
485 | n/a | TO_COMPLEX(w, b); |
---|
486 | n/a | PyFPE_START_PROTECT("complex_div", return 0) |
---|
487 | n/a | errno = 0; |
---|
488 | n/a | quot = _Py_c_quot(a, b); |
---|
489 | n/a | PyFPE_END_PROTECT(quot) |
---|
490 | n/a | if (errno == EDOM) { |
---|
491 | n/a | PyErr_SetString(PyExc_ZeroDivisionError, "complex division by zero"); |
---|
492 | n/a | return NULL; |
---|
493 | n/a | } |
---|
494 | n/a | return PyComplex_FromCComplex(quot); |
---|
495 | n/a | } |
---|
496 | n/a | |
---|
497 | n/a | static PyObject * |
---|
498 | n/a | complex_remainder(PyObject *v, PyObject *w) |
---|
499 | n/a | { |
---|
500 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
501 | n/a | "can't mod complex numbers."); |
---|
502 | n/a | return NULL; |
---|
503 | n/a | } |
---|
504 | n/a | |
---|
505 | n/a | |
---|
506 | n/a | static PyObject * |
---|
507 | n/a | complex_divmod(PyObject *v, PyObject *w) |
---|
508 | n/a | { |
---|
509 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
510 | n/a | "can't take floor or mod of complex number."); |
---|
511 | n/a | return NULL; |
---|
512 | n/a | } |
---|
513 | n/a | |
---|
514 | n/a | static PyObject * |
---|
515 | n/a | complex_pow(PyObject *v, PyObject *w, PyObject *z) |
---|
516 | n/a | { |
---|
517 | n/a | Py_complex p; |
---|
518 | n/a | Py_complex exponent; |
---|
519 | n/a | long int_exponent; |
---|
520 | n/a | Py_complex a, b; |
---|
521 | n/a | TO_COMPLEX(v, a); |
---|
522 | n/a | TO_COMPLEX(w, b); |
---|
523 | n/a | |
---|
524 | n/a | if (z != Py_None) { |
---|
525 | n/a | PyErr_SetString(PyExc_ValueError, "complex modulo"); |
---|
526 | n/a | return NULL; |
---|
527 | n/a | } |
---|
528 | n/a | PyFPE_START_PROTECT("complex_pow", return 0) |
---|
529 | n/a | errno = 0; |
---|
530 | n/a | exponent = b; |
---|
531 | n/a | int_exponent = (long)exponent.real; |
---|
532 | n/a | if (exponent.imag == 0. && exponent.real == int_exponent) |
---|
533 | n/a | p = c_powi(a, int_exponent); |
---|
534 | n/a | else |
---|
535 | n/a | p = _Py_c_pow(a, exponent); |
---|
536 | n/a | |
---|
537 | n/a | PyFPE_END_PROTECT(p) |
---|
538 | n/a | Py_ADJUST_ERANGE2(p.real, p.imag); |
---|
539 | n/a | if (errno == EDOM) { |
---|
540 | n/a | PyErr_SetString(PyExc_ZeroDivisionError, |
---|
541 | n/a | "0.0 to a negative or complex power"); |
---|
542 | n/a | return NULL; |
---|
543 | n/a | } |
---|
544 | n/a | else if (errno == ERANGE) { |
---|
545 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
546 | n/a | "complex exponentiation"); |
---|
547 | n/a | return NULL; |
---|
548 | n/a | } |
---|
549 | n/a | return PyComplex_FromCComplex(p); |
---|
550 | n/a | } |
---|
551 | n/a | |
---|
552 | n/a | static PyObject * |
---|
553 | n/a | complex_int_div(PyObject *v, PyObject *w) |
---|
554 | n/a | { |
---|
555 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
556 | n/a | "can't take floor of complex number."); |
---|
557 | n/a | return NULL; |
---|
558 | n/a | } |
---|
559 | n/a | |
---|
560 | n/a | static PyObject * |
---|
561 | n/a | complex_neg(PyComplexObject *v) |
---|
562 | n/a | { |
---|
563 | n/a | Py_complex neg; |
---|
564 | n/a | neg.real = -v->cval.real; |
---|
565 | n/a | neg.imag = -v->cval.imag; |
---|
566 | n/a | return PyComplex_FromCComplex(neg); |
---|
567 | n/a | } |
---|
568 | n/a | |
---|
569 | n/a | static PyObject * |
---|
570 | n/a | complex_pos(PyComplexObject *v) |
---|
571 | n/a | { |
---|
572 | n/a | if (PyComplex_CheckExact(v)) { |
---|
573 | n/a | Py_INCREF(v); |
---|
574 | n/a | return (PyObject *)v; |
---|
575 | n/a | } |
---|
576 | n/a | else |
---|
577 | n/a | return PyComplex_FromCComplex(v->cval); |
---|
578 | n/a | } |
---|
579 | n/a | |
---|
580 | n/a | static PyObject * |
---|
581 | n/a | complex_abs(PyComplexObject *v) |
---|
582 | n/a | { |
---|
583 | n/a | double result; |
---|
584 | n/a | |
---|
585 | n/a | PyFPE_START_PROTECT("complex_abs", return 0) |
---|
586 | n/a | result = _Py_c_abs(v->cval); |
---|
587 | n/a | PyFPE_END_PROTECT(result) |
---|
588 | n/a | |
---|
589 | n/a | if (errno == ERANGE) { |
---|
590 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
591 | n/a | "absolute value too large"); |
---|
592 | n/a | return NULL; |
---|
593 | n/a | } |
---|
594 | n/a | return PyFloat_FromDouble(result); |
---|
595 | n/a | } |
---|
596 | n/a | |
---|
597 | n/a | static int |
---|
598 | n/a | complex_bool(PyComplexObject *v) |
---|
599 | n/a | { |
---|
600 | n/a | return v->cval.real != 0.0 || v->cval.imag != 0.0; |
---|
601 | n/a | } |
---|
602 | n/a | |
---|
603 | n/a | static PyObject * |
---|
604 | n/a | complex_richcompare(PyObject *v, PyObject *w, int op) |
---|
605 | n/a | { |
---|
606 | n/a | PyObject *res; |
---|
607 | n/a | Py_complex i; |
---|
608 | n/a | int equal; |
---|
609 | n/a | |
---|
610 | n/a | if (op != Py_EQ && op != Py_NE) { |
---|
611 | n/a | goto Unimplemented; |
---|
612 | n/a | } |
---|
613 | n/a | |
---|
614 | n/a | assert(PyComplex_Check(v)); |
---|
615 | n/a | TO_COMPLEX(v, i); |
---|
616 | n/a | |
---|
617 | n/a | if (PyLong_Check(w)) { |
---|
618 | n/a | /* Check for 0.0 imaginary part first to avoid the rich |
---|
619 | n/a | * comparison when possible. |
---|
620 | n/a | */ |
---|
621 | n/a | if (i.imag == 0.0) { |
---|
622 | n/a | PyObject *j, *sub_res; |
---|
623 | n/a | j = PyFloat_FromDouble(i.real); |
---|
624 | n/a | if (j == NULL) |
---|
625 | n/a | return NULL; |
---|
626 | n/a | |
---|
627 | n/a | sub_res = PyObject_RichCompare(j, w, op); |
---|
628 | n/a | Py_DECREF(j); |
---|
629 | n/a | return sub_res; |
---|
630 | n/a | } |
---|
631 | n/a | else { |
---|
632 | n/a | equal = 0; |
---|
633 | n/a | } |
---|
634 | n/a | } |
---|
635 | n/a | else if (PyFloat_Check(w)) { |
---|
636 | n/a | equal = (i.real == PyFloat_AsDouble(w) && i.imag == 0.0); |
---|
637 | n/a | } |
---|
638 | n/a | else if (PyComplex_Check(w)) { |
---|
639 | n/a | Py_complex j; |
---|
640 | n/a | |
---|
641 | n/a | TO_COMPLEX(w, j); |
---|
642 | n/a | equal = (i.real == j.real && i.imag == j.imag); |
---|
643 | n/a | } |
---|
644 | n/a | else { |
---|
645 | n/a | goto Unimplemented; |
---|
646 | n/a | } |
---|
647 | n/a | |
---|
648 | n/a | if (equal == (op == Py_EQ)) |
---|
649 | n/a | res = Py_True; |
---|
650 | n/a | else |
---|
651 | n/a | res = Py_False; |
---|
652 | n/a | |
---|
653 | n/a | Py_INCREF(res); |
---|
654 | n/a | return res; |
---|
655 | n/a | |
---|
656 | n/a | Unimplemented: |
---|
657 | n/a | Py_RETURN_NOTIMPLEMENTED; |
---|
658 | n/a | } |
---|
659 | n/a | |
---|
660 | n/a | static PyObject * |
---|
661 | n/a | complex_int(PyObject *v) |
---|
662 | n/a | { |
---|
663 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
664 | n/a | "can't convert complex to int"); |
---|
665 | n/a | return NULL; |
---|
666 | n/a | } |
---|
667 | n/a | |
---|
668 | n/a | static PyObject * |
---|
669 | n/a | complex_float(PyObject *v) |
---|
670 | n/a | { |
---|
671 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
672 | n/a | "can't convert complex to float"); |
---|
673 | n/a | return NULL; |
---|
674 | n/a | } |
---|
675 | n/a | |
---|
676 | n/a | static PyObject * |
---|
677 | n/a | complex_conjugate(PyObject *self) |
---|
678 | n/a | { |
---|
679 | n/a | Py_complex c; |
---|
680 | n/a | c = ((PyComplexObject *)self)->cval; |
---|
681 | n/a | c.imag = -c.imag; |
---|
682 | n/a | return PyComplex_FromCComplex(c); |
---|
683 | n/a | } |
---|
684 | n/a | |
---|
685 | n/a | PyDoc_STRVAR(complex_conjugate_doc, |
---|
686 | n/a | "complex.conjugate() -> complex\n" |
---|
687 | n/a | "\n" |
---|
688 | n/a | "Return the complex conjugate of its argument. (3-4j).conjugate() == 3+4j."); |
---|
689 | n/a | |
---|
690 | n/a | static PyObject * |
---|
691 | n/a | complex_getnewargs(PyComplexObject *v) |
---|
692 | n/a | { |
---|
693 | n/a | Py_complex c = v->cval; |
---|
694 | n/a | return Py_BuildValue("(dd)", c.real, c.imag); |
---|
695 | n/a | } |
---|
696 | n/a | |
---|
697 | n/a | PyDoc_STRVAR(complex__format__doc, |
---|
698 | n/a | "complex.__format__() -> str\n" |
---|
699 | n/a | "\n" |
---|
700 | n/a | "Convert to a string according to format_spec."); |
---|
701 | n/a | |
---|
702 | n/a | static PyObject * |
---|
703 | n/a | complex__format__(PyObject* self, PyObject* args) |
---|
704 | n/a | { |
---|
705 | n/a | PyObject *format_spec; |
---|
706 | n/a | _PyUnicodeWriter writer; |
---|
707 | n/a | int ret; |
---|
708 | n/a | |
---|
709 | n/a | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
---|
710 | n/a | return NULL; |
---|
711 | n/a | |
---|
712 | n/a | _PyUnicodeWriter_Init(&writer); |
---|
713 | n/a | ret = _PyComplex_FormatAdvancedWriter( |
---|
714 | n/a | &writer, |
---|
715 | n/a | self, |
---|
716 | n/a | format_spec, 0, PyUnicode_GET_LENGTH(format_spec)); |
---|
717 | n/a | if (ret == -1) { |
---|
718 | n/a | _PyUnicodeWriter_Dealloc(&writer); |
---|
719 | n/a | return NULL; |
---|
720 | n/a | } |
---|
721 | n/a | return _PyUnicodeWriter_Finish(&writer); |
---|
722 | n/a | } |
---|
723 | n/a | |
---|
724 | n/a | #if 0 |
---|
725 | n/a | static PyObject * |
---|
726 | n/a | complex_is_finite(PyObject *self) |
---|
727 | n/a | { |
---|
728 | n/a | Py_complex c; |
---|
729 | n/a | c = ((PyComplexObject *)self)->cval; |
---|
730 | n/a | return PyBool_FromLong((long)(Py_IS_FINITE(c.real) && |
---|
731 | n/a | Py_IS_FINITE(c.imag))); |
---|
732 | n/a | } |
---|
733 | n/a | |
---|
734 | n/a | PyDoc_STRVAR(complex_is_finite_doc, |
---|
735 | n/a | "complex.is_finite() -> bool\n" |
---|
736 | n/a | "\n" |
---|
737 | n/a | "Returns True if the real and the imaginary part is finite."); |
---|
738 | n/a | #endif |
---|
739 | n/a | |
---|
740 | n/a | static PyMethodDef complex_methods[] = { |
---|
741 | n/a | {"conjugate", (PyCFunction)complex_conjugate, METH_NOARGS, |
---|
742 | n/a | complex_conjugate_doc}, |
---|
743 | n/a | #if 0 |
---|
744 | n/a | {"is_finite", (PyCFunction)complex_is_finite, METH_NOARGS, |
---|
745 | n/a | complex_is_finite_doc}, |
---|
746 | n/a | #endif |
---|
747 | n/a | {"__getnewargs__", (PyCFunction)complex_getnewargs, METH_NOARGS}, |
---|
748 | n/a | {"__format__", (PyCFunction)complex__format__, |
---|
749 | n/a | METH_VARARGS, complex__format__doc}, |
---|
750 | n/a | {NULL, NULL} /* sentinel */ |
---|
751 | n/a | }; |
---|
752 | n/a | |
---|
753 | n/a | static PyMemberDef complex_members[] = { |
---|
754 | n/a | {"real", T_DOUBLE, offsetof(PyComplexObject, cval.real), READONLY, |
---|
755 | n/a | "the real part of a complex number"}, |
---|
756 | n/a | {"imag", T_DOUBLE, offsetof(PyComplexObject, cval.imag), READONLY, |
---|
757 | n/a | "the imaginary part of a complex number"}, |
---|
758 | n/a | {0}, |
---|
759 | n/a | }; |
---|
760 | n/a | |
---|
761 | n/a | static PyObject * |
---|
762 | n/a | complex_from_string_inner(const char *s, Py_ssize_t len, void *type) |
---|
763 | n/a | { |
---|
764 | n/a | double x=0.0, y=0.0, z; |
---|
765 | n/a | int got_bracket=0; |
---|
766 | n/a | const char *start; |
---|
767 | n/a | char *end; |
---|
768 | n/a | |
---|
769 | n/a | /* position on first nonblank */ |
---|
770 | n/a | start = s; |
---|
771 | n/a | while (Py_ISSPACE(*s)) |
---|
772 | n/a | s++; |
---|
773 | n/a | if (*s == '(') { |
---|
774 | n/a | /* Skip over possible bracket from repr(). */ |
---|
775 | n/a | got_bracket = 1; |
---|
776 | n/a | s++; |
---|
777 | n/a | while (Py_ISSPACE(*s)) |
---|
778 | n/a | s++; |
---|
779 | n/a | } |
---|
780 | n/a | |
---|
781 | n/a | /* a valid complex string usually takes one of the three forms: |
---|
782 | n/a | |
---|
783 | n/a | <float> - real part only |
---|
784 | n/a | <float>j - imaginary part only |
---|
785 | n/a | <float><signed-float>j - real and imaginary parts |
---|
786 | n/a | |
---|
787 | n/a | where <float> represents any numeric string that's accepted by the |
---|
788 | n/a | float constructor (including 'nan', 'inf', 'infinity', etc.), and |
---|
789 | n/a | <signed-float> is any string of the form <float> whose first |
---|
790 | n/a | character is '+' or '-'. |
---|
791 | n/a | |
---|
792 | n/a | For backwards compatibility, the extra forms |
---|
793 | n/a | |
---|
794 | n/a | <float><sign>j |
---|
795 | n/a | <sign>j |
---|
796 | n/a | j |
---|
797 | n/a | |
---|
798 | n/a | are also accepted, though support for these forms may be removed from |
---|
799 | n/a | a future version of Python. |
---|
800 | n/a | */ |
---|
801 | n/a | |
---|
802 | n/a | /* first look for forms starting with <float> */ |
---|
803 | n/a | z = PyOS_string_to_double(s, &end, NULL); |
---|
804 | n/a | if (z == -1.0 && PyErr_Occurred()) { |
---|
805 | n/a | if (PyErr_ExceptionMatches(PyExc_ValueError)) |
---|
806 | n/a | PyErr_Clear(); |
---|
807 | n/a | else |
---|
808 | n/a | return NULL; |
---|
809 | n/a | } |
---|
810 | n/a | if (end != s) { |
---|
811 | n/a | /* all 4 forms starting with <float> land here */ |
---|
812 | n/a | s = end; |
---|
813 | n/a | if (*s == '+' || *s == '-') { |
---|
814 | n/a | /* <float><signed-float>j | <float><sign>j */ |
---|
815 | n/a | x = z; |
---|
816 | n/a | y = PyOS_string_to_double(s, &end, NULL); |
---|
817 | n/a | if (y == -1.0 && PyErr_Occurred()) { |
---|
818 | n/a | if (PyErr_ExceptionMatches(PyExc_ValueError)) |
---|
819 | n/a | PyErr_Clear(); |
---|
820 | n/a | else |
---|
821 | n/a | return NULL; |
---|
822 | n/a | } |
---|
823 | n/a | if (end != s) |
---|
824 | n/a | /* <float><signed-float>j */ |
---|
825 | n/a | s = end; |
---|
826 | n/a | else { |
---|
827 | n/a | /* <float><sign>j */ |
---|
828 | n/a | y = *s == '+' ? 1.0 : -1.0; |
---|
829 | n/a | s++; |
---|
830 | n/a | } |
---|
831 | n/a | if (!(*s == 'j' || *s == 'J')) |
---|
832 | n/a | goto parse_error; |
---|
833 | n/a | s++; |
---|
834 | n/a | } |
---|
835 | n/a | else if (*s == 'j' || *s == 'J') { |
---|
836 | n/a | /* <float>j */ |
---|
837 | n/a | s++; |
---|
838 | n/a | y = z; |
---|
839 | n/a | } |
---|
840 | n/a | else |
---|
841 | n/a | /* <float> */ |
---|
842 | n/a | x = z; |
---|
843 | n/a | } |
---|
844 | n/a | else { |
---|
845 | n/a | /* not starting with <float>; must be <sign>j or j */ |
---|
846 | n/a | if (*s == '+' || *s == '-') { |
---|
847 | n/a | /* <sign>j */ |
---|
848 | n/a | y = *s == '+' ? 1.0 : -1.0; |
---|
849 | n/a | s++; |
---|
850 | n/a | } |
---|
851 | n/a | else |
---|
852 | n/a | /* j */ |
---|
853 | n/a | y = 1.0; |
---|
854 | n/a | if (!(*s == 'j' || *s == 'J')) |
---|
855 | n/a | goto parse_error; |
---|
856 | n/a | s++; |
---|
857 | n/a | } |
---|
858 | n/a | |
---|
859 | n/a | /* trailing whitespace and closing bracket */ |
---|
860 | n/a | while (Py_ISSPACE(*s)) |
---|
861 | n/a | s++; |
---|
862 | n/a | if (got_bracket) { |
---|
863 | n/a | /* if there was an opening parenthesis, then the corresponding |
---|
864 | n/a | closing parenthesis should be right here */ |
---|
865 | n/a | if (*s != ')') |
---|
866 | n/a | goto parse_error; |
---|
867 | n/a | s++; |
---|
868 | n/a | while (Py_ISSPACE(*s)) |
---|
869 | n/a | s++; |
---|
870 | n/a | } |
---|
871 | n/a | |
---|
872 | n/a | /* we should now be at the end of the string */ |
---|
873 | n/a | if (s-start != len) |
---|
874 | n/a | goto parse_error; |
---|
875 | n/a | |
---|
876 | n/a | return complex_subtype_from_doubles((PyTypeObject *)type, x, y); |
---|
877 | n/a | |
---|
878 | n/a | parse_error: |
---|
879 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
880 | n/a | "complex() arg is a malformed string"); |
---|
881 | n/a | return NULL; |
---|
882 | n/a | } |
---|
883 | n/a | |
---|
884 | n/a | static PyObject * |
---|
885 | n/a | complex_subtype_from_string(PyTypeObject *type, PyObject *v) |
---|
886 | n/a | { |
---|
887 | n/a | const char *s; |
---|
888 | n/a | PyObject *s_buffer = NULL, *result = NULL; |
---|
889 | n/a | Py_ssize_t len; |
---|
890 | n/a | |
---|
891 | n/a | if (PyUnicode_Check(v)) { |
---|
892 | n/a | s_buffer = _PyUnicode_TransformDecimalAndSpaceToASCII(v); |
---|
893 | n/a | if (s_buffer == NULL) { |
---|
894 | n/a | return NULL; |
---|
895 | n/a | } |
---|
896 | n/a | s = PyUnicode_AsUTF8AndSize(s_buffer, &len); |
---|
897 | n/a | if (s == NULL) { |
---|
898 | n/a | goto exit; |
---|
899 | n/a | } |
---|
900 | n/a | } |
---|
901 | n/a | else { |
---|
902 | n/a | PyErr_Format(PyExc_TypeError, |
---|
903 | n/a | "complex() argument must be a string or a number, not '%.200s'", |
---|
904 | n/a | Py_TYPE(v)->tp_name); |
---|
905 | n/a | return NULL; |
---|
906 | n/a | } |
---|
907 | n/a | |
---|
908 | n/a | result = _Py_string_to_number_with_underscores(s, len, "complex", v, type, |
---|
909 | n/a | complex_from_string_inner); |
---|
910 | n/a | exit: |
---|
911 | n/a | Py_DECREF(s_buffer); |
---|
912 | n/a | return result; |
---|
913 | n/a | } |
---|
914 | n/a | |
---|
915 | n/a | static PyObject * |
---|
916 | n/a | complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
917 | n/a | { |
---|
918 | n/a | PyObject *r, *i, *tmp; |
---|
919 | n/a | PyNumberMethods *nbr, *nbi = NULL; |
---|
920 | n/a | Py_complex cr, ci; |
---|
921 | n/a | int own_r = 0; |
---|
922 | n/a | int cr_is_complex = 0; |
---|
923 | n/a | int ci_is_complex = 0; |
---|
924 | n/a | static char *kwlist[] = {"real", "imag", 0}; |
---|
925 | n/a | |
---|
926 | n/a | r = Py_False; |
---|
927 | n/a | i = NULL; |
---|
928 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:complex", kwlist, |
---|
929 | n/a | &r, &i)) |
---|
930 | n/a | return NULL; |
---|
931 | n/a | |
---|
932 | n/a | /* Special-case for a single argument when type(arg) is complex. */ |
---|
933 | n/a | if (PyComplex_CheckExact(r) && i == NULL && |
---|
934 | n/a | type == &PyComplex_Type) { |
---|
935 | n/a | /* Note that we can't know whether it's safe to return |
---|
936 | n/a | a complex *subclass* instance as-is, hence the restriction |
---|
937 | n/a | to exact complexes here. If either the input or the |
---|
938 | n/a | output is a complex subclass, it will be handled below |
---|
939 | n/a | as a non-orthogonal vector. */ |
---|
940 | n/a | Py_INCREF(r); |
---|
941 | n/a | return r; |
---|
942 | n/a | } |
---|
943 | n/a | if (PyUnicode_Check(r)) { |
---|
944 | n/a | if (i != NULL) { |
---|
945 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
946 | n/a | "complex() can't take second arg" |
---|
947 | n/a | " if first is a string"); |
---|
948 | n/a | return NULL; |
---|
949 | n/a | } |
---|
950 | n/a | return complex_subtype_from_string(type, r); |
---|
951 | n/a | } |
---|
952 | n/a | if (i != NULL && PyUnicode_Check(i)) { |
---|
953 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
954 | n/a | "complex() second arg can't be a string"); |
---|
955 | n/a | return NULL; |
---|
956 | n/a | } |
---|
957 | n/a | |
---|
958 | n/a | tmp = try_complex_special_method(r); |
---|
959 | n/a | if (tmp) { |
---|
960 | n/a | r = tmp; |
---|
961 | n/a | own_r = 1; |
---|
962 | n/a | } |
---|
963 | n/a | else if (PyErr_Occurred()) { |
---|
964 | n/a | return NULL; |
---|
965 | n/a | } |
---|
966 | n/a | |
---|
967 | n/a | nbr = r->ob_type->tp_as_number; |
---|
968 | n/a | if (nbr == NULL || nbr->nb_float == NULL) { |
---|
969 | n/a | PyErr_Format(PyExc_TypeError, |
---|
970 | n/a | "complex() first argument must be a string or a number, " |
---|
971 | n/a | "not '%.200s'", |
---|
972 | n/a | Py_TYPE(r)->tp_name); |
---|
973 | n/a | if (own_r) { |
---|
974 | n/a | Py_DECREF(r); |
---|
975 | n/a | } |
---|
976 | n/a | return NULL; |
---|
977 | n/a | } |
---|
978 | n/a | if (i != NULL) { |
---|
979 | n/a | nbi = i->ob_type->tp_as_number; |
---|
980 | n/a | if (nbi == NULL || nbi->nb_float == NULL) { |
---|
981 | n/a | PyErr_Format(PyExc_TypeError, |
---|
982 | n/a | "complex() second argument must be a number, " |
---|
983 | n/a | "not '%.200s'", |
---|
984 | n/a | Py_TYPE(i)->tp_name); |
---|
985 | n/a | if (own_r) { |
---|
986 | n/a | Py_DECREF(r); |
---|
987 | n/a | } |
---|
988 | n/a | return NULL; |
---|
989 | n/a | } |
---|
990 | n/a | } |
---|
991 | n/a | |
---|
992 | n/a | /* If we get this far, then the "real" and "imag" parts should |
---|
993 | n/a | both be treated as numbers, and the constructor should return a |
---|
994 | n/a | complex number equal to (real + imag*1j). |
---|
995 | n/a | |
---|
996 | n/a | Note that we do NOT assume the input to already be in canonical |
---|
997 | n/a | form; the "real" and "imag" parts might themselves be complex |
---|
998 | n/a | numbers, which slightly complicates the code below. */ |
---|
999 | n/a | if (PyComplex_Check(r)) { |
---|
1000 | n/a | /* Note that if r is of a complex subtype, we're only |
---|
1001 | n/a | retaining its real & imag parts here, and the return |
---|
1002 | n/a | value is (properly) of the builtin complex type. */ |
---|
1003 | n/a | cr = ((PyComplexObject*)r)->cval; |
---|
1004 | n/a | cr_is_complex = 1; |
---|
1005 | n/a | if (own_r) { |
---|
1006 | n/a | Py_DECREF(r); |
---|
1007 | n/a | } |
---|
1008 | n/a | } |
---|
1009 | n/a | else { |
---|
1010 | n/a | /* The "real" part really is entirely real, and contributes |
---|
1011 | n/a | nothing in the imaginary direction. |
---|
1012 | n/a | Just treat it as a double. */ |
---|
1013 | n/a | tmp = PyNumber_Float(r); |
---|
1014 | n/a | if (own_r) { |
---|
1015 | n/a | /* r was a newly created complex number, rather |
---|
1016 | n/a | than the original "real" argument. */ |
---|
1017 | n/a | Py_DECREF(r); |
---|
1018 | n/a | } |
---|
1019 | n/a | if (tmp == NULL) |
---|
1020 | n/a | return NULL; |
---|
1021 | n/a | if (!PyFloat_Check(tmp)) { |
---|
1022 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
1023 | n/a | "float(r) didn't return a float"); |
---|
1024 | n/a | Py_DECREF(tmp); |
---|
1025 | n/a | return NULL; |
---|
1026 | n/a | } |
---|
1027 | n/a | cr.real = PyFloat_AsDouble(tmp); |
---|
1028 | n/a | cr.imag = 0.0; /* Shut up compiler warning */ |
---|
1029 | n/a | Py_DECREF(tmp); |
---|
1030 | n/a | } |
---|
1031 | n/a | if (i == NULL) { |
---|
1032 | n/a | ci.real = 0.0; |
---|
1033 | n/a | } |
---|
1034 | n/a | else if (PyComplex_Check(i)) { |
---|
1035 | n/a | ci = ((PyComplexObject*)i)->cval; |
---|
1036 | n/a | ci_is_complex = 1; |
---|
1037 | n/a | } else { |
---|
1038 | n/a | /* The "imag" part really is entirely imaginary, and |
---|
1039 | n/a | contributes nothing in the real direction. |
---|
1040 | n/a | Just treat it as a double. */ |
---|
1041 | n/a | tmp = (*nbi->nb_float)(i); |
---|
1042 | n/a | if (tmp == NULL) |
---|
1043 | n/a | return NULL; |
---|
1044 | n/a | ci.real = PyFloat_AsDouble(tmp); |
---|
1045 | n/a | Py_DECREF(tmp); |
---|
1046 | n/a | } |
---|
1047 | n/a | /* If the input was in canonical form, then the "real" and "imag" |
---|
1048 | n/a | parts are real numbers, so that ci.imag and cr.imag are zero. |
---|
1049 | n/a | We need this correction in case they were not real numbers. */ |
---|
1050 | n/a | |
---|
1051 | n/a | if (ci_is_complex) { |
---|
1052 | n/a | cr.real -= ci.imag; |
---|
1053 | n/a | } |
---|
1054 | n/a | if (cr_is_complex) { |
---|
1055 | n/a | ci.real += cr.imag; |
---|
1056 | n/a | } |
---|
1057 | n/a | return complex_subtype_from_doubles(type, cr.real, ci.real); |
---|
1058 | n/a | } |
---|
1059 | n/a | |
---|
1060 | n/a | PyDoc_STRVAR(complex_doc, |
---|
1061 | n/a | "complex(real[, imag]) -> complex number\n" |
---|
1062 | n/a | "\n" |
---|
1063 | n/a | "Create a complex number from a real part and an optional imaginary part.\n" |
---|
1064 | n/a | "This is equivalent to (real + imag*1j) where imag defaults to 0."); |
---|
1065 | n/a | |
---|
1066 | n/a | static PyNumberMethods complex_as_number = { |
---|
1067 | n/a | (binaryfunc)complex_add, /* nb_add */ |
---|
1068 | n/a | (binaryfunc)complex_sub, /* nb_subtract */ |
---|
1069 | n/a | (binaryfunc)complex_mul, /* nb_multiply */ |
---|
1070 | n/a | (binaryfunc)complex_remainder, /* nb_remainder */ |
---|
1071 | n/a | (binaryfunc)complex_divmod, /* nb_divmod */ |
---|
1072 | n/a | (ternaryfunc)complex_pow, /* nb_power */ |
---|
1073 | n/a | (unaryfunc)complex_neg, /* nb_negative */ |
---|
1074 | n/a | (unaryfunc)complex_pos, /* nb_positive */ |
---|
1075 | n/a | (unaryfunc)complex_abs, /* nb_absolute */ |
---|
1076 | n/a | (inquiry)complex_bool, /* nb_bool */ |
---|
1077 | n/a | 0, /* nb_invert */ |
---|
1078 | n/a | 0, /* nb_lshift */ |
---|
1079 | n/a | 0, /* nb_rshift */ |
---|
1080 | n/a | 0, /* nb_and */ |
---|
1081 | n/a | 0, /* nb_xor */ |
---|
1082 | n/a | 0, /* nb_or */ |
---|
1083 | n/a | complex_int, /* nb_int */ |
---|
1084 | n/a | 0, /* nb_reserved */ |
---|
1085 | n/a | complex_float, /* nb_float */ |
---|
1086 | n/a | 0, /* nb_inplace_add */ |
---|
1087 | n/a | 0, /* nb_inplace_subtract */ |
---|
1088 | n/a | 0, /* nb_inplace_multiply*/ |
---|
1089 | n/a | 0, /* nb_inplace_remainder */ |
---|
1090 | n/a | 0, /* nb_inplace_power */ |
---|
1091 | n/a | 0, /* nb_inplace_lshift */ |
---|
1092 | n/a | 0, /* nb_inplace_rshift */ |
---|
1093 | n/a | 0, /* nb_inplace_and */ |
---|
1094 | n/a | 0, /* nb_inplace_xor */ |
---|
1095 | n/a | 0, /* nb_inplace_or */ |
---|
1096 | n/a | (binaryfunc)complex_int_div, /* nb_floor_divide */ |
---|
1097 | n/a | (binaryfunc)complex_div, /* nb_true_divide */ |
---|
1098 | n/a | 0, /* nb_inplace_floor_divide */ |
---|
1099 | n/a | 0, /* nb_inplace_true_divide */ |
---|
1100 | n/a | }; |
---|
1101 | n/a | |
---|
1102 | n/a | PyTypeObject PyComplex_Type = { |
---|
1103 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
---|
1104 | n/a | "complex", |
---|
1105 | n/a | sizeof(PyComplexObject), |
---|
1106 | n/a | 0, |
---|
1107 | n/a | complex_dealloc, /* tp_dealloc */ |
---|
1108 | n/a | 0, /* tp_print */ |
---|
1109 | n/a | 0, /* tp_getattr */ |
---|
1110 | n/a | 0, /* tp_setattr */ |
---|
1111 | n/a | 0, /* tp_reserved */ |
---|
1112 | n/a | (reprfunc)complex_repr, /* tp_repr */ |
---|
1113 | n/a | &complex_as_number, /* tp_as_number */ |
---|
1114 | n/a | 0, /* tp_as_sequence */ |
---|
1115 | n/a | 0, /* tp_as_mapping */ |
---|
1116 | n/a | (hashfunc)complex_hash, /* tp_hash */ |
---|
1117 | n/a | 0, /* tp_call */ |
---|
1118 | n/a | (reprfunc)complex_repr, /* tp_str */ |
---|
1119 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
---|
1120 | n/a | 0, /* tp_setattro */ |
---|
1121 | n/a | 0, /* tp_as_buffer */ |
---|
1122 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
---|
1123 | n/a | complex_doc, /* tp_doc */ |
---|
1124 | n/a | 0, /* tp_traverse */ |
---|
1125 | n/a | 0, /* tp_clear */ |
---|
1126 | n/a | complex_richcompare, /* tp_richcompare */ |
---|
1127 | n/a | 0, /* tp_weaklistoffset */ |
---|
1128 | n/a | 0, /* tp_iter */ |
---|
1129 | n/a | 0, /* tp_iternext */ |
---|
1130 | n/a | complex_methods, /* tp_methods */ |
---|
1131 | n/a | complex_members, /* tp_members */ |
---|
1132 | n/a | 0, /* tp_getset */ |
---|
1133 | n/a | 0, /* tp_base */ |
---|
1134 | n/a | 0, /* tp_dict */ |
---|
1135 | n/a | 0, /* tp_descr_get */ |
---|
1136 | n/a | 0, /* tp_descr_set */ |
---|
1137 | n/a | 0, /* tp_dictoffset */ |
---|
1138 | n/a | 0, /* tp_init */ |
---|
1139 | n/a | PyType_GenericAlloc, /* tp_alloc */ |
---|
1140 | n/a | complex_new, /* tp_new */ |
---|
1141 | n/a | PyObject_Del, /* tp_free */ |
---|
1142 | n/a | }; |
---|