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