Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 1 | /* Complex math module */ |
| 2 | |
| 3 | /* much code borrowed from mathmodule.c */ |
| 4 | |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 5 | #include "Python.h" |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 7 | #include "mymath.h" |
| 8 | |
| 9 | #ifdef i860 |
| 10 | /* Cray APP has bogus definition of HUGE_VAL in <math.h> */ |
| 11 | #undef HUGE_VAL |
| 12 | #endif |
| 13 | |
| 14 | #ifdef HUGE_VAL |
| 15 | #define CHECK(x) if (errno != 0) ; \ |
| 16 | else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \ |
| 17 | else errno = ERANGE |
| 18 | #else |
| 19 | #define CHECK(x) /* Don't know how to check */ |
| 20 | #endif |
| 21 | |
| 22 | #ifndef M_PI |
| 23 | #define M_PI (3.141592653589793239) |
| 24 | #endif |
| 25 | |
| 26 | /* First, the C functions that do the real work */ |
| 27 | |
| 28 | /* constants */ |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 29 | static Py_complex c_1 = {1., 0.}; |
| 30 | static Py_complex c_half = {0.5, 0.}; |
| 31 | static Py_complex c_i = {0., 1.}; |
| 32 | static Py_complex c_i2 = {0., 0.5}; |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 33 | #if 0 |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 34 | static Py_complex c_mi = {0., -1.}; |
| 35 | static Py_complex c_pi2 = {M_PI/2., 0.}; |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 36 | #endif |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 37 | |
| 38 | /* forward declarations */ |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 39 | staticforward Py_complex c_log(); |
| 40 | staticforward Py_complex c_prodi(); |
| 41 | staticforward Py_complex c_sqrt(); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 42 | |
| 43 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 44 | static Py_complex c_acos(x) |
| 45 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 46 | { |
| 47 | return c_neg(c_prodi(c_log(c_sum(x,c_prod(c_i, |
| 48 | c_sqrt(c_diff(c_1,c_prod(x,x)))))))); |
| 49 | } |
| 50 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 51 | static Py_complex c_acosh(x) |
| 52 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 53 | { |
| 54 | return c_log(c_sum(x,c_prod(c_i, |
| 55 | c_sqrt(c_diff(c_1,c_prod(x,x)))))); |
| 56 | } |
| 57 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 58 | static Py_complex c_asin(x) |
| 59 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 60 | { |
| 61 | return c_neg(c_prodi(c_log(c_sum(c_prod(c_i,x), |
| 62 | c_sqrt(c_diff(c_1,c_prod(x,x))))))); |
| 63 | } |
| 64 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 65 | static Py_complex c_asinh(x) |
| 66 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 67 | { |
| 68 | return c_neg(c_log(c_diff(c_sqrt(c_sum(c_1,c_prod(x,x))),x))); |
| 69 | } |
| 70 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 71 | static Py_complex c_atan(x) |
| 72 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 73 | { |
| 74 | return c_prod(c_i2,c_log(c_quot(c_sum(c_i,x),c_diff(c_i,x)))); |
| 75 | } |
| 76 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 77 | static Py_complex c_atanh(x) |
| 78 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 79 | { |
| 80 | return c_prod(c_half,c_log(c_quot(c_sum(c_1,x),c_diff(c_1,x)))); |
| 81 | } |
| 82 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 83 | static Py_complex c_cos(x) |
| 84 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 85 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 86 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 87 | r.real = cos(x.real)*cosh(x.imag); |
| 88 | r.imag = -sin(x.real)*sinh(x.imag); |
| 89 | return r; |
| 90 | } |
| 91 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 92 | static Py_complex c_cosh(x) |
| 93 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 94 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 95 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 96 | r.real = cos(x.imag)*cosh(x.real); |
| 97 | r.imag = sin(x.imag)*sinh(x.real); |
| 98 | return r; |
| 99 | } |
| 100 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 101 | static Py_complex c_exp(x) |
| 102 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 103 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 104 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 105 | double l = exp(x.real); |
| 106 | r.real = l*cos(x.imag); |
| 107 | r.imag = l*sin(x.imag); |
| 108 | return r; |
| 109 | } |
| 110 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 111 | static Py_complex c_log(x) |
| 112 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 113 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 114 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 115 | double l = hypot(x.real,x.imag); |
| 116 | r.imag = atan2(x.imag, x.real); |
| 117 | r.real = log(l); |
| 118 | return r; |
| 119 | } |
| 120 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 121 | static Py_complex c_log10(x) |
| 122 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 123 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 124 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 125 | double l = hypot(x.real,x.imag); |
| 126 | r.imag = atan2(x.imag, x.real)/log(10.); |
| 127 | r.real = log10(l); |
| 128 | return r; |
| 129 | } |
| 130 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 131 | static Py_complex c_prodi(x) |
| 132 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 133 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 134 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 135 | r.real = -x.imag; |
| 136 | r.imag = x.real; |
| 137 | return r; |
| 138 | } |
| 139 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 140 | static Py_complex c_sin(x) |
| 141 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 142 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 143 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 144 | r.real = sin(x.real)*cosh(x.imag); |
| 145 | r.imag = cos(x.real)*sinh(x.imag); |
| 146 | return r; |
| 147 | } |
| 148 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 149 | static Py_complex c_sinh(x) |
| 150 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 151 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 152 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 153 | r.real = cos(x.imag)*sinh(x.real); |
| 154 | r.imag = sin(x.imag)*cosh(x.real); |
| 155 | return r; |
| 156 | } |
| 157 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 158 | static Py_complex c_sqrt(x) |
| 159 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 160 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 161 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 162 | double s,d; |
| 163 | if (x.real == 0. && x.imag == 0.) |
| 164 | r = x; |
| 165 | else { |
| 166 | s = sqrt(0.5*(fabs(x.real) + hypot(x.real,x.imag))); |
| 167 | d = 0.5*x.imag/s; |
| 168 | if (x.real > 0.) { |
| 169 | r.real = s; |
| 170 | r.imag = d; |
| 171 | } |
| 172 | else if (x.imag >= 0.) { |
| 173 | r.real = d; |
| 174 | r.imag = s; |
| 175 | } |
| 176 | else { |
| 177 | r.real = -d; |
| 178 | r.imag = -s; |
| 179 | } |
| 180 | } |
| 181 | return r; |
| 182 | } |
| 183 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 184 | static Py_complex c_tan(x) |
| 185 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 186 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 187 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 188 | double sr,cr,shi,chi; |
| 189 | double rs,is,rc,ic; |
| 190 | double d; |
| 191 | sr = sin(x.real); |
| 192 | cr = cos(x.real); |
| 193 | shi = sinh(x.imag); |
| 194 | chi = cosh(x.imag); |
| 195 | rs = sr*chi; |
| 196 | is = cr*shi; |
| 197 | rc = cr*chi; |
| 198 | ic = -sr*shi; |
| 199 | d = rc*rc + ic*ic; |
| 200 | r.real = (rs*rc+is*ic)/d; |
| 201 | r.imag = (is*rc-rs*ic)/d; |
| 202 | return r; |
| 203 | } |
| 204 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 205 | static Py_complex c_tanh(x) |
| 206 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 207 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 208 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 209 | double si,ci,shr,chr; |
| 210 | double rs,is,rc,ic; |
| 211 | double d; |
| 212 | si = sin(x.imag); |
| 213 | ci = cos(x.imag); |
| 214 | shr = sinh(x.real); |
| 215 | chr = cosh(x.real); |
| 216 | rs = ci*shr; |
| 217 | is = si*chr; |
| 218 | rc = ci*chr; |
| 219 | ic = si*shr; |
| 220 | d = rc*rc + ic*ic; |
| 221 | r.real = (rs*rc+is*ic)/d; |
| 222 | r.imag = (is*rc-rs*ic)/d; |
| 223 | return r; |
| 224 | } |
| 225 | |
| 226 | |
| 227 | /* And now the glue to make them available from Python: */ |
| 228 | |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 229 | static PyObject * |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 230 | math_error() |
| 231 | { |
| 232 | if (errno == EDOM) |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 233 | PyErr_SetString(PyExc_ValueError, "math domain error"); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 234 | else if (errno == ERANGE) |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 235 | PyErr_SetString(PyExc_OverflowError, "math range error"); |
| 236 | else /* Unexpected math error */ |
| 237 | PyErr_SetFromErrno(PyExc_ValueError); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 238 | return NULL; |
| 239 | } |
| 240 | |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 241 | static PyObject * |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 242 | math_1(args, func) |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 243 | PyObject *args; |
| 244 | Py_complex (*func) Py_FPROTO((Py_complex)); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 245 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 246 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 247 | if (!PyArg_ParseTuple(args, "D", &x)) |
| 248 | return NULL; |
| 249 | errno = 0; |
| 250 | x = (*func)(x); |
| 251 | CHECK(x.real); |
| 252 | CHECK(x.imag); |
| 253 | if (errno != 0) |
| 254 | return math_error(); |
| 255 | else |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 256 | return PyComplex_FromCComplex(x); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | #define FUNC1(stubname, func) \ |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 260 | static PyObject * stubname(self, args) PyObject *self, *args; { \ |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 261 | return math_1(args, func); \ |
| 262 | } |
| 263 | |
| 264 | FUNC1(cmath_acos, c_acos) |
| 265 | FUNC1(cmath_acosh, c_acosh) |
| 266 | FUNC1(cmath_asin, c_asin) |
| 267 | FUNC1(cmath_asinh, c_asinh) |
| 268 | FUNC1(cmath_atan, c_atan) |
| 269 | FUNC1(cmath_atanh, c_atanh) |
| 270 | FUNC1(cmath_cos, c_cos) |
| 271 | FUNC1(cmath_cosh, c_cosh) |
| 272 | FUNC1(cmath_exp, c_exp) |
| 273 | FUNC1(cmath_log, c_log) |
| 274 | FUNC1(cmath_log10, c_log10) |
| 275 | FUNC1(cmath_sin, c_sin) |
| 276 | FUNC1(cmath_sinh, c_sinh) |
| 277 | FUNC1(cmath_sqrt, c_sqrt) |
| 278 | FUNC1(cmath_tan, c_tan) |
| 279 | FUNC1(cmath_tanh, c_tanh) |
| 280 | |
| 281 | |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 282 | static PyMethodDef cmath_methods[] = { |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 283 | {"acos", cmath_acos, 1}, |
| 284 | {"acosh", cmath_acosh, 1}, |
| 285 | {"asin", cmath_asin, 1}, |
| 286 | {"asinh", cmath_asinh, 1}, |
| 287 | {"atan", cmath_atan, 1}, |
| 288 | {"atanh", cmath_atanh, 1}, |
| 289 | {"cos", cmath_cos, 1}, |
| 290 | {"cosh", cmath_cosh, 1}, |
| 291 | {"exp", cmath_exp, 1}, |
| 292 | {"log", cmath_log, 1}, |
| 293 | {"log10", cmath_log10, 1}, |
| 294 | {"sin", cmath_sin, 1}, |
| 295 | {"sinh", cmath_sinh, 1}, |
| 296 | {"sqrt", cmath_sqrt, 1}, |
| 297 | {"tan", cmath_tan, 1}, |
| 298 | {"tanh", cmath_tanh, 1}, |
| 299 | {NULL, NULL} /* sentinel */ |
| 300 | }; |
| 301 | |
| 302 | void |
| 303 | initcmath() |
| 304 | { |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 305 | PyObject *m, *d, *v; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 306 | |
| 307 | m = Py_InitModule("cmath", cmath_methods); |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 308 | d = PyModule_GetDict(m); |
| 309 | PyDict_SetItemString(d, "pi", |
| 310 | v = PyFloat_FromDouble(atan(1.0) * 4.0)); |
| 311 | Py_DECREF(v); |
| 312 | PyDict_SetItemString(d, "e", v = PyFloat_FromDouble(exp(1.0))); |
| 313 | Py_DECREF(v); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 314 | } |