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