blob: 69eaf1287014f3b2a90a0186de5909d9d3127f41 [file] [log] [blame]
Guido van Rossum71aa32f1996-01-12 01:34:57 +00001/* Complex math module */
2
3/* much code borrowed from mathmodule.c */
4
Roger E. Masse24070ca1996-12-09 22:59:53 +00005#include "Python.h"
Guido van Rossum71aa32f1996-01-12 01:34:57 +00006
Guido van Rossum71aa32f1996-01-12 01:34:57 +00007#ifdef i860
8/* Cray APP has bogus definition of HUGE_VAL in <math.h> */
9#undef HUGE_VAL
10#endif
11
12#ifdef HUGE_VAL
13#define CHECK(x) if (errno != 0) ; \
14 else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
15 else errno = ERANGE
16#else
17#define CHECK(x) /* Don't know how to check */
18#endif
19
20#ifndef M_PI
21#define M_PI (3.141592653589793239)
22#endif
23
24/* First, the C functions that do the real work */
25
26/* constants */
Guido van Rossum9e720e31996-07-21 02:31:35 +000027static Py_complex c_1 = {1., 0.};
28static Py_complex c_half = {0.5, 0.};
29static Py_complex c_i = {0., 1.};
30static Py_complex c_i2 = {0., 0.5};
Guido van Rossuma376cc51996-12-05 23:43:35 +000031#if 0
Guido van Rossum9e720e31996-07-21 02:31:35 +000032static Py_complex c_mi = {0., -1.};
33static Py_complex c_pi2 = {M_PI/2., 0.};
Guido van Rossuma376cc51996-12-05 23:43:35 +000034#endif
Guido van Rossum71aa32f1996-01-12 01:34:57 +000035
36/* forward declarations */
Thomas Wouterseb61b6e2000-07-24 11:17:40 +000037staticforward Py_complex c_log(Py_complex);
38staticforward Py_complex c_prodi(Py_complex);
39staticforward Py_complex c_sqrt(Py_complex);
Guido van Rossum71aa32f1996-01-12 01:34:57 +000040
41
Peter Schneider-Kampf1ca8982000-07-10 09:31:34 +000042static Py_complex c_acos(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +000043{
44 return c_neg(c_prodi(c_log(c_sum(x,c_prod(c_i,
45 c_sqrt(c_diff(c_1,c_prod(x,x))))))));
46}
47
Guido van Rossumc6e22901998-12-04 19:26:43 +000048static char c_acos_doc [] =
49"acos(x)\n\
50\n\
51Return the arc cosine of x.";
52
53
Peter Schneider-Kampf1ca8982000-07-10 09:31:34 +000054static Py_complex c_acosh(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +000055{
Guido van Rossumf385c5e2000-06-30 02:29:22 +000056 Py_complex z;
57 z = c_sqrt(c_half);
58 z = c_log(c_prod(z, c_sum(c_sqrt(c_sum(x,c_1)),
59 c_sqrt(c_diff(x,c_1)))));
60 return c_sum(z, z);
Guido van Rossum71aa32f1996-01-12 01:34:57 +000061}
62
Guido van Rossumc6e22901998-12-04 19:26:43 +000063static char c_acosh_doc [] =
64"acosh(x)\n\
65\n\
Fred Drake0e11c491999-03-16 14:17:48 +000066Return the hyperbolic arccosine of x.";
Guido van Rossumc6e22901998-12-04 19:26:43 +000067
68
Peter Schneider-Kampf1ca8982000-07-10 09:31:34 +000069static Py_complex c_asin(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +000070{
Guido van Rossumf385c5e2000-06-30 02:29:22 +000071 Py_complex z;
72 z = c_sqrt(c_half);
73 z = c_log(c_prod(z, c_sum(c_sqrt(c_sum(x,c_i)),
74 c_sqrt(c_diff(x,c_i)))));
75 return c_sum(z, z);
Guido van Rossum71aa32f1996-01-12 01:34:57 +000076}
77
Guido van Rossumc6e22901998-12-04 19:26:43 +000078static char c_asin_doc [] =
79"asin(x)\n\
80\n\
81Return the arc sine of x.";
82
83
Peter Schneider-Kampf1ca8982000-07-10 09:31:34 +000084static Py_complex c_asinh(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +000085{
Guido van Rossum11a50711999-01-14 19:11:11 +000086 /* Break up long expression for WATCOM */
87 Py_complex z;
Guido van Rossumf385c5e2000-06-30 02:29:22 +000088 z = c_sum(c_1,c_prod(x, x));
89 return c_log(c_sum(c_sqrt(z), x));
Guido van Rossum71aa32f1996-01-12 01:34:57 +000090}
91
Guido van Rossumc6e22901998-12-04 19:26:43 +000092static char c_asinh_doc [] =
93"asinh(x)\n\
94\n\
95Return the hyperbolic arc sine of x.";
96
97
Peter Schneider-Kampf1ca8982000-07-10 09:31:34 +000098static Py_complex c_atan(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +000099{
100 return c_prod(c_i2,c_log(c_quot(c_sum(c_i,x),c_diff(c_i,x))));
101}
102
Guido van Rossumc6e22901998-12-04 19:26:43 +0000103static char c_atan_doc [] =
104"atan(x)\n\
105\n\
106Return the arc tangent of x.";
107
108
Peter Schneider-Kampf1ca8982000-07-10 09:31:34 +0000109static Py_complex c_atanh(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000110{
111 return c_prod(c_half,c_log(c_quot(c_sum(c_1,x),c_diff(c_1,x))));
112}
113
Guido van Rossumc6e22901998-12-04 19:26:43 +0000114static char c_atanh_doc [] =
115"atanh(x)\n\
116\n\
117Return the hyperbolic arc tangent of x.";
118
119
Peter Schneider-Kampf1ca8982000-07-10 09:31:34 +0000120static Py_complex c_cos(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000121{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000122 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000123 r.real = cos(x.real)*cosh(x.imag);
124 r.imag = -sin(x.real)*sinh(x.imag);
125 return r;
126}
127
Guido van Rossumc6e22901998-12-04 19:26:43 +0000128static char c_cos_doc [] =
129"cos(x)\n\
130\n\
131Return the cosine of x.";
132
133
Peter Schneider-Kampf1ca8982000-07-10 09:31:34 +0000134static Py_complex c_cosh(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000135{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000136 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000137 r.real = cos(x.imag)*cosh(x.real);
138 r.imag = sin(x.imag)*sinh(x.real);
139 return r;
140}
141
Guido van Rossumc6e22901998-12-04 19:26:43 +0000142static char c_cosh_doc [] =
143"cosh(x)\n\
144\n\
145Return the hyperbolic cosine of x.";
146
147
Peter Schneider-Kampf1ca8982000-07-10 09:31:34 +0000148static Py_complex c_exp(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000149{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000150 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000151 double l = exp(x.real);
152 r.real = l*cos(x.imag);
153 r.imag = l*sin(x.imag);
154 return r;
155}
156
Guido van Rossumc6e22901998-12-04 19:26:43 +0000157static char c_exp_doc [] =
158"exp(x)\n\
159\n\
160Return the exponential value e**x.";
161
162
Peter Schneider-Kampf1ca8982000-07-10 09:31:34 +0000163static Py_complex c_log(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000164{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000165 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000166 double l = hypot(x.real,x.imag);
167 r.imag = atan2(x.imag, x.real);
168 r.real = log(l);
169 return r;
170}
171
Guido van Rossumc6e22901998-12-04 19:26:43 +0000172static char c_log_doc [] =
173"log(x)\n\
174\n\
175Return the natural logarithm of x.";
176
177
Peter Schneider-Kampf1ca8982000-07-10 09:31:34 +0000178static Py_complex c_log10(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000179{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000180 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000181 double l = hypot(x.real,x.imag);
182 r.imag = atan2(x.imag, x.real)/log(10.);
183 r.real = log10(l);
184 return r;
185}
186
Guido van Rossumc6e22901998-12-04 19:26:43 +0000187static char c_log10_doc [] =
188"log10(x)\n\
189\n\
190Return the base-10 logarithm of x.";
191
192
193/* internal function not available from Python */
Peter Schneider-Kampf1ca8982000-07-10 09:31:34 +0000194static Py_complex c_prodi(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000195{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000196 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000197 r.real = -x.imag;
198 r.imag = x.real;
199 return r;
200}
201
Guido van Rossumc6e22901998-12-04 19:26:43 +0000202
Peter Schneider-Kampf1ca8982000-07-10 09:31:34 +0000203static Py_complex c_sin(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000204{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000205 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000206 r.real = sin(x.real)*cosh(x.imag);
207 r.imag = cos(x.real)*sinh(x.imag);
208 return r;
209}
210
Guido van Rossumc6e22901998-12-04 19:26:43 +0000211static char c_sin_doc [] =
212"sin(x)\n\
213\n\
214Return the sine of x.";
215
216
Peter Schneider-Kampf1ca8982000-07-10 09:31:34 +0000217static Py_complex c_sinh(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000218{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000219 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000220 r.real = cos(x.imag)*sinh(x.real);
221 r.imag = sin(x.imag)*cosh(x.real);
222 return r;
223}
224
Guido van Rossumc6e22901998-12-04 19:26:43 +0000225static char c_sinh_doc [] =
226"sinh(x)\n\
227\n\
228Return the hyperbolic sine of x.";
229
230
Peter Schneider-Kampf1ca8982000-07-10 09:31:34 +0000231static Py_complex c_sqrt(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000232{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000233 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000234 double s,d;
235 if (x.real == 0. && x.imag == 0.)
236 r = x;
237 else {
238 s = sqrt(0.5*(fabs(x.real) + hypot(x.real,x.imag)));
239 d = 0.5*x.imag/s;
240 if (x.real > 0.) {
241 r.real = s;
242 r.imag = d;
243 }
244 else if (x.imag >= 0.) {
245 r.real = d;
246 r.imag = s;
247 }
248 else {
249 r.real = -d;
250 r.imag = -s;
251 }
252 }
253 return r;
254}
255
Guido van Rossumc6e22901998-12-04 19:26:43 +0000256static char c_sqrt_doc [] =
257"sqrt(x)\n\
258\n\
259Return the square root of x.";
260
261
Peter Schneider-Kampf1ca8982000-07-10 09:31:34 +0000262static Py_complex c_tan(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000263{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000264 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000265 double sr,cr,shi,chi;
266 double rs,is,rc,ic;
267 double d;
268 sr = sin(x.real);
269 cr = cos(x.real);
270 shi = sinh(x.imag);
271 chi = cosh(x.imag);
272 rs = sr*chi;
273 is = cr*shi;
274 rc = cr*chi;
275 ic = -sr*shi;
276 d = rc*rc + ic*ic;
277 r.real = (rs*rc+is*ic)/d;
278 r.imag = (is*rc-rs*ic)/d;
279 return r;
280}
281
Guido van Rossumc6e22901998-12-04 19:26:43 +0000282static char c_tan_doc [] =
283"tan(x)\n\
284\n\
285Return the tangent of x.";
286
287
Peter Schneider-Kampf1ca8982000-07-10 09:31:34 +0000288static Py_complex c_tanh(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000289{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000290 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000291 double si,ci,shr,chr;
292 double rs,is,rc,ic;
293 double d;
294 si = sin(x.imag);
295 ci = cos(x.imag);
296 shr = sinh(x.real);
297 chr = cosh(x.real);
298 rs = ci*shr;
299 is = si*chr;
300 rc = ci*chr;
301 ic = si*shr;
302 d = rc*rc + ic*ic;
303 r.real = (rs*rc+is*ic)/d;
304 r.imag = (is*rc-rs*ic)/d;
305 return r;
306}
307
Guido van Rossumc6e22901998-12-04 19:26:43 +0000308static char c_tanh_doc [] =
309"tanh(x)\n\
310\n\
311Return the hyperbolic tangent of x.";
312
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000313
314/* And now the glue to make them available from Python: */
315
Roger E. Masse24070ca1996-12-09 22:59:53 +0000316static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000317math_error(void)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000318{
319 if (errno == EDOM)
Roger E. Masse24070ca1996-12-09 22:59:53 +0000320 PyErr_SetString(PyExc_ValueError, "math domain error");
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000321 else if (errno == ERANGE)
Roger E. Masse24070ca1996-12-09 22:59:53 +0000322 PyErr_SetString(PyExc_OverflowError, "math range error");
323 else /* Unexpected math error */
324 PyErr_SetFromErrno(PyExc_ValueError);
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000325 return NULL;
326}
327
Roger E. Masse24070ca1996-12-09 22:59:53 +0000328static PyObject *
Peter Schneider-Kampf1ca8982000-07-10 09:31:34 +0000329math_1(PyObject *args, Py_complex (*func)(Py_complex))
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000330{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000331 Py_complex x;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000332 if (!PyArg_ParseTuple(args, "D", &x))
333 return NULL;
334 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000335 PyFPE_START_PROTECT("complex function", return 0)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000336 x = (*func)(x);
Guido van Rossum45b83911997-03-14 04:32:50 +0000337 PyFPE_END_PROTECT(x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000338 CHECK(x.real);
339 CHECK(x.imag);
340 if (errno != 0)
341 return math_error();
342 else
Roger E. Masse24070ca1996-12-09 22:59:53 +0000343 return PyComplex_FromCComplex(x);
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000344}
345
346#define FUNC1(stubname, func) \
Peter Schneider-Kampf1ca8982000-07-10 09:31:34 +0000347 static PyObject * stubname(PyObject *self, PyObject *args) { \
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000348 return math_1(args, func); \
349 }
350
351FUNC1(cmath_acos, c_acos)
352FUNC1(cmath_acosh, c_acosh)
353FUNC1(cmath_asin, c_asin)
354FUNC1(cmath_asinh, c_asinh)
355FUNC1(cmath_atan, c_atan)
356FUNC1(cmath_atanh, c_atanh)
357FUNC1(cmath_cos, c_cos)
358FUNC1(cmath_cosh, c_cosh)
359FUNC1(cmath_exp, c_exp)
360FUNC1(cmath_log, c_log)
361FUNC1(cmath_log10, c_log10)
362FUNC1(cmath_sin, c_sin)
363FUNC1(cmath_sinh, c_sinh)
364FUNC1(cmath_sqrt, c_sqrt)
365FUNC1(cmath_tan, c_tan)
366FUNC1(cmath_tanh, c_tanh)
367
368
Guido van Rossumc6e22901998-12-04 19:26:43 +0000369static char module_doc [] =
370"This module is always available. It provides access to mathematical\n\
371functions for complex numbers.";
372
373
Roger E. Masse24070ca1996-12-09 22:59:53 +0000374static PyMethodDef cmath_methods[] = {
Andrew M. Kuchlinge365fb82000-08-03 02:06:16 +0000375 {"acos", cmath_acos,
376 METH_VARARGS, c_acos_doc},
377 {"acosh", cmath_acosh,
378 METH_VARARGS, c_acosh_doc},
379 {"asin", cmath_asin,
380 METH_VARARGS, c_asin_doc},
381 {"asinh", cmath_asinh,
382 METH_VARARGS, c_asinh_doc},
383 {"atan", cmath_atan,
384 METH_VARARGS, c_atan_doc},
385 {"atanh", cmath_atanh,
386 METH_VARARGS, c_atanh_doc},
387 {"cos", cmath_cos,
388 METH_VARARGS, c_cos_doc},
389 {"cosh", cmath_cosh,
390 METH_VARARGS, c_cosh_doc},
391 {"exp", cmath_exp,
392 METH_VARARGS, c_exp_doc},
393 {"log", cmath_log,
394 METH_VARARGS, c_log_doc},
395 {"log10", cmath_log10,
396 METH_VARARGS, c_log10_doc},
397 {"sin", cmath_sin,
398 METH_VARARGS, c_sin_doc},
399 {"sinh", cmath_sinh,
400 METH_VARARGS, c_sinh_doc},
401 {"sqrt", cmath_sqrt,
402 METH_VARARGS, c_sqrt_doc},
403 {"tan", cmath_tan,
404 METH_VARARGS, c_tan_doc},
405 {"tanh", cmath_tanh,
406 METH_VARARGS, c_tanh_doc},
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000407 {NULL, NULL} /* sentinel */
408};
409
Guido van Rossum3886bb61998-12-04 18:50:17 +0000410DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000411initcmath(void)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000412{
Roger E. Masse24070ca1996-12-09 22:59:53 +0000413 PyObject *m, *d, *v;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000414
Guido van Rossumc6e22901998-12-04 19:26:43 +0000415 m = Py_InitModule3("cmath", cmath_methods, module_doc);
Roger E. Masse24070ca1996-12-09 22:59:53 +0000416 d = PyModule_GetDict(m);
417 PyDict_SetItemString(d, "pi",
418 v = PyFloat_FromDouble(atan(1.0) * 4.0));
419 Py_DECREF(v);
420 PyDict_SetItemString(d, "e", v = PyFloat_FromDouble(exp(1.0)));
421 Py_DECREF(v);
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000422}