Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 11 | /* Complex object implementation */ |
| 12 | |
| 13 | /* Borrows heavily from floatobject.c */ |
| 14 | |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 15 | /* Submitted by Jim Hugunin */ |
| 16 | |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 17 | #ifndef WITHOUT_COMPLEX |
| 18 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 19 | #include "Python.h" |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 20 | #include "mymath.h" |
| 21 | |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 22 | #ifdef HAVE_LIMITS_H |
| 23 | #include <limits.h> |
| 24 | #endif |
| 25 | |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 26 | |
| 27 | /* elementary operations on complex numbers */ |
| 28 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 29 | static Py_complex c_1 = {1., 0.}; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 30 | |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 31 | Py_complex c_sum(Py_complex a, Py_complex b) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 32 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 33 | Py_complex r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 34 | r.real = a.real + b.real; |
| 35 | r.imag = a.imag + b.imag; |
| 36 | return r; |
| 37 | } |
| 38 | |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 39 | Py_complex c_diff(Py_complex a, Py_complex b) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 40 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 41 | Py_complex r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 42 | r.real = a.real - b.real; |
| 43 | r.imag = a.imag - b.imag; |
| 44 | return r; |
| 45 | } |
| 46 | |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 47 | Py_complex c_neg(Py_complex a) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 48 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 49 | Py_complex r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 50 | r.real = -a.real; |
| 51 | r.imag = -a.imag; |
| 52 | return r; |
| 53 | } |
| 54 | |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 55 | Py_complex c_prod(Py_complex a, Py_complex b) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 56 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 57 | Py_complex r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 58 | r.real = a.real*b.real - a.imag*b.imag; |
| 59 | r.imag = a.real*b.imag + a.imag*b.real; |
| 60 | return r; |
| 61 | } |
| 62 | |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 63 | Py_complex c_quot(Py_complex a, Py_complex b) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 64 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 65 | Py_complex r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 66 | double d = b.real*b.real + b.imag*b.imag; |
| 67 | if (d == 0.) |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 68 | errno = EDOM; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 69 | r.real = (a.real*b.real + a.imag*b.imag)/d; |
| 70 | r.imag = (a.imag*b.real - a.real*b.imag)/d; |
| 71 | return r; |
| 72 | } |
| 73 | |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 74 | Py_complex c_pow(Py_complex a, Py_complex b) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 75 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 76 | Py_complex r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 77 | double vabs,len,at,phase; |
| 78 | if (b.real == 0. && b.imag == 0.) { |
| 79 | r.real = 1.; |
| 80 | r.imag = 0.; |
| 81 | } |
| 82 | else if (a.real == 0. && a.imag == 0.) { |
| 83 | if (b.imag != 0. || b.real < 0.) |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 84 | errno = ERANGE; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 85 | r.real = 0.; |
| 86 | r.imag = 0.; |
| 87 | } |
| 88 | else { |
| 89 | vabs = hypot(a.real,a.imag); |
| 90 | len = pow(vabs,b.real); |
| 91 | at = atan2(a.imag, a.real); |
| 92 | phase = at*b.real; |
| 93 | if (b.imag != 0.0) { |
| 94 | len /= exp(at*b.imag); |
| 95 | phase += b.imag*log(vabs); |
| 96 | } |
| 97 | r.real = len*cos(phase); |
| 98 | r.imag = len*sin(phase); |
| 99 | } |
| 100 | return r; |
| 101 | } |
| 102 | |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 103 | static Py_complex c_powu(Py_complex x, long n) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 104 | { |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 105 | Py_complex r, p; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 106 | long mask = 1; |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 107 | r = c_1; |
| 108 | p = x; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 109 | while (mask > 0 && n >= mask) { |
| 110 | if (n & mask) |
| 111 | r = c_prod(r,p); |
| 112 | mask <<= 1; |
| 113 | p = c_prod(p,p); |
| 114 | } |
| 115 | return r; |
| 116 | } |
| 117 | |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 118 | static Py_complex c_powi(Py_complex x, long n) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 119 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 120 | Py_complex cn; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 121 | |
| 122 | if (n > 100 || n < -100) { |
| 123 | cn.real = (double) n; |
| 124 | cn.imag = 0.; |
| 125 | return c_pow(x,cn); |
| 126 | } |
| 127 | else if (n > 0) |
| 128 | return c_powu(x,n); |
| 129 | else |
| 130 | return c_quot(c_1,c_powu(x,-n)); |
| 131 | |
| 132 | } |
| 133 | |
| 134 | PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 135 | PyComplex_FromCComplex(Py_complex cval) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 136 | { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 137 | register PyComplexObject *op; |
| 138 | |
| 139 | /* PyObject_New is inlined */ |
| 140 | op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject)); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 141 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 142 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 143 | PyObject_INIT(op, &PyComplex_Type); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 144 | op->cval = cval; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 145 | return (PyObject *) op; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 149 | PyComplex_FromDoubles(double real, double imag) |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 150 | { |
| 151 | Py_complex c; |
| 152 | c.real = real; |
| 153 | c.imag = imag; |
| 154 | return PyComplex_FromCComplex(c); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | double |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 158 | PyComplex_RealAsDouble(PyObject *op) |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 159 | { |
| 160 | if (PyComplex_Check(op)) { |
| 161 | return ((PyComplexObject *)op)->cval.real; |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 162 | } |
| 163 | else { |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 164 | return PyFloat_AsDouble(op); |
| 165 | } |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | double |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 169 | PyComplex_ImagAsDouble(PyObject *op) |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 170 | { |
| 171 | if (PyComplex_Check(op)) { |
| 172 | return ((PyComplexObject *)op)->cval.imag; |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 173 | } |
| 174 | else { |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 175 | return 0.0; |
| 176 | } |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 179 | Py_complex |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 180 | PyComplex_AsCComplex(PyObject *op) |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 181 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 182 | Py_complex cv; |
Guido van Rossum | cf3d108 | 1996-01-12 01:21:14 +0000 | [diff] [blame] | 183 | if (PyComplex_Check(op)) { |
| 184 | return ((PyComplexObject *)op)->cval; |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 185 | } |
| 186 | else { |
Guido van Rossum | cf3d108 | 1996-01-12 01:21:14 +0000 | [diff] [blame] | 187 | cv.real = PyFloat_AsDouble(op); |
| 188 | cv.imag = 0.; |
| 189 | return cv; |
| 190 | } |
| 191 | } |
| 192 | |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 193 | static void |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 194 | complex_dealloc(PyObject *op) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 195 | { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 196 | PyObject_DEL(op); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | |
Guido van Rossum | 363078a | 1996-05-24 20:45:01 +0000 | [diff] [blame] | 200 | static void |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 201 | complex_buf_repr(char *buf, PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 202 | { |
| 203 | if (v->cval.real == 0.) |
Guido van Rossum | 7241879 | 1996-01-25 16:21:31 +0000 | [diff] [blame] | 204 | sprintf(buf, "%.12gj", v->cval.imag); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 205 | else |
Guido van Rossum | 7241879 | 1996-01-25 16:21:31 +0000 | [diff] [blame] | 206 | sprintf(buf, "(%.12g%+.12gj)", v->cval.real, v->cval.imag); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | static int |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 210 | complex_print(PyComplexObject *v, FILE *fp, int flags) |
| 211 | /* flags -- not used but required by interface */ |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 212 | { |
| 213 | char buf[100]; |
| 214 | complex_buf_repr(buf, v); |
| 215 | fputs(buf, fp); |
| 216 | return 0; |
| 217 | } |
| 218 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 219 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 220 | complex_repr(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 221 | { |
| 222 | char buf[100]; |
| 223 | complex_buf_repr(buf, v); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 224 | return PyString_FromString(buf); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | static int |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 228 | complex_compare(PyComplexObject *v, PyComplexObject *w) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 229 | { |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 230 | /* Note: "greater" and "smaller" have no meaning for complex numbers, |
| 231 | but Python requires that they be defined nevertheless. */ |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 232 | Py_complex i, j; |
| 233 | i = v->cval; |
| 234 | j = w->cval; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 235 | if (i.real == j.real && i.imag == j.imag) |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 236 | return 0; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 237 | else if (i.real != j.real) |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 238 | return (i.real < j.real) ? -1 : 1; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 239 | else |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 240 | return (i.imag < j.imag) ? -1 : 1; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | static long |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 244 | complex_hash(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 245 | { |
| 246 | double intpart, fractpart; |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 247 | long x; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 248 | /* This is designed so that Python numbers with the same |
| 249 | value hash to the same value, otherwise comparisons |
| 250 | of mapping keys will turn out weird */ |
| 251 | |
| 252 | #ifdef MPW /* MPW C modf expects pointer to extended as second argument */ |
| 253 | { |
| 254 | extended e; |
| 255 | fractpart = modf(v->cval.real, &e); |
| 256 | intpart = e; |
| 257 | } |
| 258 | #else |
| 259 | fractpart = modf(v->cval.real, &intpart); |
| 260 | #endif |
| 261 | |
Guido van Rossum | 919cf1a | 1997-01-11 19:26:21 +0000 | [diff] [blame] | 262 | if (fractpart == 0.0 && v->cval.imag == 0.0) { |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 263 | if (intpart > LONG_MAX || -intpart > LONG_MAX) { |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 264 | /* Convert to long int and use its hash... */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 265 | PyObject *w = PyLong_FromDouble(v->cval.real); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 266 | if (w == NULL) |
| 267 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 268 | x = PyObject_Hash(w); |
| 269 | Py_DECREF(w); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 270 | return x; |
| 271 | } |
| 272 | x = (long)intpart; |
| 273 | } |
| 274 | else { |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 275 | x = _Py_HashDouble(v->cval.real); |
| 276 | if (x == -1) |
| 277 | return -1; |
Guido van Rossum | 919cf1a | 1997-01-11 19:26:21 +0000 | [diff] [blame] | 278 | |
| 279 | if (v->cval.imag != 0.0) { /* Hash the imaginary part */ |
| 280 | /* XXX Note that this hashes complex(x, y) |
| 281 | to the same value as complex(y, x). |
| 282 | Still better than it used to be :-) */ |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 283 | long y = _Py_HashDouble(v->cval.imag); |
| 284 | if (y == -1) |
| 285 | return -1; |
| 286 | x += y; |
Guido van Rossum | 919cf1a | 1997-01-11 19:26:21 +0000 | [diff] [blame] | 287 | } |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 288 | } |
| 289 | if (x == -1) |
| 290 | x = -2; |
| 291 | return x; |
| 292 | } |
| 293 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 294 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 295 | complex_add(PyComplexObject *v, PyComplexObject *w) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 296 | { |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 297 | Py_complex result; |
| 298 | PyFPE_START_PROTECT("complex_add", return 0) |
| 299 | result = c_sum(v->cval,w->cval); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 300 | PyFPE_END_PROTECT(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 301 | return PyComplex_FromCComplex(result); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 304 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 305 | complex_sub(PyComplexObject *v, PyComplexObject *w) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 306 | { |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 307 | Py_complex result; |
| 308 | PyFPE_START_PROTECT("complex_sub", return 0) |
| 309 | result = c_diff(v->cval,w->cval); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 310 | PyFPE_END_PROTECT(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 311 | return PyComplex_FromCComplex(result); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 314 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 315 | complex_mul(PyComplexObject *v, PyComplexObject *w) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 316 | { |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 317 | Py_complex result; |
| 318 | PyFPE_START_PROTECT("complex_mul", return 0) |
| 319 | result = c_prod(v->cval,w->cval); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 320 | PyFPE_END_PROTECT(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 321 | return PyComplex_FromCComplex(result); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 324 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 325 | complex_div(PyComplexObject *v, PyComplexObject *w) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 326 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 327 | Py_complex quot; |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 328 | PyFPE_START_PROTECT("complex_div", return 0) |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 329 | errno = 0; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 330 | quot = c_quot(v->cval,w->cval); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 331 | PyFPE_END_PROTECT(quot) |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 332 | if (errno == EDOM) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 333 | PyErr_SetString(PyExc_ZeroDivisionError, "complex division"); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 334 | return NULL; |
| 335 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 336 | return PyComplex_FromCComplex(quot); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 339 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 340 | complex_remainder(PyComplexObject *v, PyComplexObject *w) |
Guido van Rossum | ee09fc1 | 1996-09-11 13:55:55 +0000 | [diff] [blame] | 341 | { |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 342 | Py_complex div, mod; |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 343 | errno = 0; |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 344 | div = c_quot(v->cval,w->cval); /* The raw divisor value. */ |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 345 | if (errno == EDOM) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 346 | PyErr_SetString(PyExc_ZeroDivisionError, "complex remainder"); |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 347 | return NULL; |
| 348 | } |
| 349 | div.real = floor(div.real); /* Use the floor of the real part. */ |
| 350 | div.imag = 0.0; |
| 351 | mod = c_diff(v->cval, c_prod(w->cval, div)); |
| 352 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 353 | return PyComplex_FromCComplex(mod); |
Guido van Rossum | ee09fc1 | 1996-09-11 13:55:55 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Guido van Rossum | ee09fc1 | 1996-09-11 13:55:55 +0000 | [diff] [blame] | 356 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 357 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 358 | complex_divmod(PyComplexObject *v, PyComplexObject *w) |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 359 | { |
| 360 | Py_complex div, mod; |
| 361 | PyObject *d, *m, *z; |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 362 | errno = 0; |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 363 | div = c_quot(v->cval,w->cval); /* The raw divisor value. */ |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 364 | if (errno == EDOM) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 365 | PyErr_SetString(PyExc_ZeroDivisionError, "complex divmod()"); |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 366 | return NULL; |
| 367 | } |
| 368 | div.real = floor(div.real); /* Use the floor of the real part. */ |
| 369 | div.imag = 0.0; |
| 370 | mod = c_diff(v->cval, c_prod(w->cval, div)); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 371 | d = PyComplex_FromCComplex(div); |
| 372 | m = PyComplex_FromCComplex(mod); |
| 373 | z = Py_BuildValue("(OO)", d, m); |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 374 | Py_XDECREF(d); |
| 375 | Py_XDECREF(m); |
| 376 | return z; |
| 377 | } |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 378 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 379 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 380 | complex_pow(PyComplexObject *v, PyObject *w, PyComplexObject *z) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 381 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 382 | Py_complex p; |
| 383 | Py_complex exponent; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 384 | long int_exponent; |
| 385 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 386 | if ((PyObject *)z!=Py_None) { |
| 387 | PyErr_SetString(PyExc_ValueError, "complex modulo"); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 388 | return NULL; |
| 389 | } |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 390 | PyFPE_START_PROTECT("complex_pow", return 0) |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 391 | errno = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 392 | exponent = ((PyComplexObject*)w)->cval; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 393 | int_exponent = (long)exponent.real; |
| 394 | if (exponent.imag == 0. && exponent.real == int_exponent) |
| 395 | p = c_powi(v->cval,int_exponent); |
| 396 | else |
| 397 | p = c_pow(v->cval,exponent); |
| 398 | |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 399 | PyFPE_END_PROTECT(p) |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 400 | if (errno == ERANGE) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 401 | PyErr_SetString(PyExc_ValueError, |
| 402 | "0.0 to a negative or complex power"); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 403 | return NULL; |
| 404 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 405 | return PyComplex_FromCComplex(p); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 408 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 409 | complex_neg(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 410 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 411 | Py_complex neg; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 412 | neg.real = -v->cval.real; |
| 413 | neg.imag = -v->cval.imag; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 414 | return PyComplex_FromCComplex(neg); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 417 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 418 | complex_pos(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 419 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 420 | Py_INCREF(v); |
| 421 | return (PyObject *)v; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 424 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 425 | complex_abs(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 426 | { |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 427 | double result; |
| 428 | PyFPE_START_PROTECT("complex_abs", return 0) |
| 429 | result = hypot(v->cval.real,v->cval.imag); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 430 | PyFPE_END_PROTECT(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 431 | return PyFloat_FromDouble(result); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | static int |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 435 | complex_nonzero(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 436 | { |
Guido van Rossum | 3bbef60 | 1999-01-25 19:42:19 +0000 | [diff] [blame] | 437 | return v->cval.real != 0.0 || v->cval.imag != 0.0; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | static int |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 441 | complex_coerce(PyObject **pv, PyObject **pw) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 442 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 443 | Py_complex cval; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 444 | cval.imag = 0.; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 445 | if (PyInt_Check(*pw)) { |
| 446 | cval.real = (double)PyInt_AsLong(*pw); |
| 447 | *pw = PyComplex_FromCComplex(cval); |
| 448 | Py_INCREF(*pv); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 449 | return 0; |
| 450 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 451 | else if (PyLong_Check(*pw)) { |
| 452 | cval.real = PyLong_AsDouble(*pw); |
| 453 | *pw = PyComplex_FromCComplex(cval); |
| 454 | Py_INCREF(*pv); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 455 | return 0; |
| 456 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 457 | else if (PyFloat_Check(*pw)) { |
| 458 | cval.real = PyFloat_AsDouble(*pw); |
| 459 | *pw = PyComplex_FromCComplex(cval); |
| 460 | Py_INCREF(*pv); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 461 | return 0; |
| 462 | } |
| 463 | return 1; /* Can't do it */ |
| 464 | } |
| 465 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 466 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 467 | complex_int(PyObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 468 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 469 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | d4ab3cd | 1996-09-11 22:54:37 +0000 | [diff] [blame] | 470 | "can't convert complex to int; use e.g. int(abs(z))"); |
| 471 | return NULL; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 474 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 475 | complex_long(PyObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 476 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 477 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | d4ab3cd | 1996-09-11 22:54:37 +0000 | [diff] [blame] | 478 | "can't convert complex to long; use e.g. long(abs(z))"); |
| 479 | return NULL; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 482 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 483 | complex_float(PyObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 484 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 485 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | d4ab3cd | 1996-09-11 22:54:37 +0000 | [diff] [blame] | 486 | "can't convert complex to float; use e.g. abs(z)"); |
| 487 | return NULL; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 490 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 491 | complex_conjugate(PyObject *self, PyObject *args) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 492 | { |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 493 | Py_complex c; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 494 | if (!PyArg_ParseTuple(args, ":conjugate")) |
Guido van Rossum | 8530ef6 | 1998-05-07 16:29:10 +0000 | [diff] [blame] | 495 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 496 | c = ((PyComplexObject *)self)->cval; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 497 | c.imag = -c.imag; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 498 | return PyComplex_FromCComplex(c); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | static PyMethodDef complex_methods[] = { |
Guido van Rossum | 8530ef6 | 1998-05-07 16:29:10 +0000 | [diff] [blame] | 502 | {"conjugate", complex_conjugate, 1}, |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 503 | {NULL, NULL} /* sentinel */ |
| 504 | }; |
| 505 | |
| 506 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 507 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 508 | complex_getattr(PyComplexObject *self, char *name) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 509 | { |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 510 | if (strcmp(name, "real") == 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 511 | return (PyObject *)PyFloat_FromDouble(self->cval.real); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 512 | else if (strcmp(name, "imag") == 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 513 | return (PyObject *)PyFloat_FromDouble(self->cval.imag); |
Guido van Rossum | c054d70 | 1997-04-01 03:12:33 +0000 | [diff] [blame] | 514 | else if (strcmp(name, "__members__") == 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 515 | return Py_BuildValue("[ss]", "imag", "real"); |
| 516 | return Py_FindMethod(complex_methods, (PyObject *)self, name); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 519 | static PyNumberMethods complex_as_number = { |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 520 | (binaryfunc)complex_add, /*nb_add*/ |
| 521 | (binaryfunc)complex_sub, /*nb_subtract*/ |
| 522 | (binaryfunc)complex_mul, /*nb_multiply*/ |
| 523 | (binaryfunc)complex_div, /*nb_divide*/ |
Guido van Rossum | ee09fc1 | 1996-09-11 13:55:55 +0000 | [diff] [blame] | 524 | (binaryfunc)complex_remainder, /*nb_remainder*/ |
| 525 | (binaryfunc)complex_divmod, /*nb_divmod*/ |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 526 | (ternaryfunc)complex_pow, /*nb_power*/ |
| 527 | (unaryfunc)complex_neg, /*nb_negative*/ |
| 528 | (unaryfunc)complex_pos, /*nb_positive*/ |
| 529 | (unaryfunc)complex_abs, /*nb_absolute*/ |
| 530 | (inquiry)complex_nonzero, /*nb_nonzero*/ |
| 531 | 0, /*nb_invert*/ |
| 532 | 0, /*nb_lshift*/ |
| 533 | 0, /*nb_rshift*/ |
| 534 | 0, /*nb_and*/ |
| 535 | 0, /*nb_xor*/ |
| 536 | 0, /*nb_or*/ |
| 537 | (coercion)complex_coerce, /*nb_coerce*/ |
| 538 | (unaryfunc)complex_int, /*nb_int*/ |
| 539 | (unaryfunc)complex_long, /*nb_long*/ |
| 540 | (unaryfunc)complex_float, /*nb_float*/ |
| 541 | 0, /*nb_oct*/ |
| 542 | 0, /*nb_hex*/ |
| 543 | }; |
| 544 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 545 | PyTypeObject PyComplex_Type = { |
| 546 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 547 | 0, |
| 548 | "complex", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 549 | sizeof(PyComplexObject), |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 550 | 0, |
| 551 | (destructor)complex_dealloc, /*tp_dealloc*/ |
| 552 | (printfunc)complex_print, /*tp_print*/ |
| 553 | (getattrfunc)complex_getattr, /*tp_getattr*/ |
| 554 | 0, /*tp_setattr*/ |
| 555 | (cmpfunc)complex_compare, /*tp_compare*/ |
| 556 | (reprfunc)complex_repr, /*tp_repr*/ |
| 557 | &complex_as_number, /*tp_as_number*/ |
| 558 | 0, /*tp_as_sequence*/ |
| 559 | 0, /*tp_as_mapping*/ |
| 560 | (hashfunc)complex_hash, /*tp_hash*/ |
| 561 | }; |
| 562 | |
| 563 | #endif |