blob: cc7b1f0b2dae14b01f9c99ebe2268a345725b2c4 [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 */
Tim Peters8cb82bd2001-02-20 20:36:38 +000027static Py_complex c_one = {1., 0.};
Guido van Rossum9e720e31996-07-21 02:31:35 +000028static Py_complex c_half = {0.5, 0.};
29static Py_complex c_i = {0., 1.};
Tim Peters8cb82bd2001-02-20 20:36:38 +000030static Py_complex c_halfi = {0., 0.5};
Guido van Rossum71aa32f1996-01-12 01:34:57 +000031
32/* forward declarations */
Thomas Wouterseb61b6e2000-07-24 11:17:40 +000033staticforward Py_complex c_log(Py_complex);
34staticforward Py_complex c_prodi(Py_complex);
35staticforward Py_complex c_sqrt(Py_complex);
Guido van Rossum71aa32f1996-01-12 01:34:57 +000036
37
Tim Peters14e26402001-02-20 20:15:19 +000038static Py_complex
39c_acos(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +000040{
41 return c_neg(c_prodi(c_log(c_sum(x,c_prod(c_i,
Tim Peters8cb82bd2001-02-20 20:36:38 +000042 c_sqrt(c_diff(c_one,c_prod(x,x))))))));
Guido van Rossum71aa32f1996-01-12 01:34:57 +000043}
44
Tim Peters14e26402001-02-20 20:15:19 +000045static char c_acos_doc[] =
46"acos(x)\n"
47"\n"
48"Return the arc cosine of x.";
Guido van Rossumc6e22901998-12-04 19:26:43 +000049
50
Tim Peters14e26402001-02-20 20:15:19 +000051static Py_complex
52c_acosh(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +000053{
Guido van Rossumf385c5e2000-06-30 02:29:22 +000054 Py_complex z;
55 z = c_sqrt(c_half);
Tim Peters8cb82bd2001-02-20 20:36:38 +000056 z = c_log(c_prod(z, c_sum(c_sqrt(c_sum(x,c_one)),
57 c_sqrt(c_diff(x,c_one)))));
Guido van Rossumf385c5e2000-06-30 02:29:22 +000058 return c_sum(z, z);
Guido van Rossum71aa32f1996-01-12 01:34:57 +000059}
60
Tim Peters14e26402001-02-20 20:15:19 +000061static char c_acosh_doc[] =
62"acosh(x)\n"
63"\n"
64"Return the hyperbolic arccosine of x.";
Guido van Rossumc6e22901998-12-04 19:26:43 +000065
66
Tim Peters14e26402001-02-20 20:15:19 +000067static Py_complex
68c_asin(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +000069{
Guido van Rossumf385c5e2000-06-30 02:29:22 +000070 Py_complex z;
71 z = c_sqrt(c_half);
Tim Peters14e26402001-02-20 20:15:19 +000072 z = c_log(c_prod(z, c_sum(c_sqrt(c_sum(x, c_i)),
73 c_sqrt(c_diff(x, c_i)))));
Guido van Rossumf385c5e2000-06-30 02:29:22 +000074 return c_sum(z, z);
Guido van Rossum71aa32f1996-01-12 01:34:57 +000075}
76
Tim Peters14e26402001-02-20 20:15:19 +000077static char c_asin_doc[] =
78"asin(x)\n"
79"\n"
80"Return the arc sine of x.";
Guido van Rossumc6e22901998-12-04 19:26:43 +000081
82
Tim Peters14e26402001-02-20 20:15:19 +000083static Py_complex
84c_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;
Tim Peters8cb82bd2001-02-20 20:36:38 +000088 z = c_sum(c_one, c_prod(x, x));
Guido van Rossumf385c5e2000-06-30 02:29:22 +000089 return c_log(c_sum(c_sqrt(z), x));
Guido van Rossum71aa32f1996-01-12 01:34:57 +000090}
91
Tim Peters14e26402001-02-20 20:15:19 +000092static char c_asinh_doc[] =
93"asinh(x)\n"
94"\n"
95"Return the hyperbolic arc sine of x.";
Guido van Rossumc6e22901998-12-04 19:26:43 +000096
97
Tim Peters14e26402001-02-20 20:15:19 +000098static Py_complex
99c_atan(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000100{
Tim Peters8cb82bd2001-02-20 20:36:38 +0000101 return c_prod(c_halfi,c_log(c_quot(c_sum(c_i,x),c_diff(c_i,x))));
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000102}
103
Tim Peters14e26402001-02-20 20:15:19 +0000104static char c_atan_doc[] =
105"atan(x)\n"
106"\n"
107"Return the arc tangent of x.";
Guido van Rossumc6e22901998-12-04 19:26:43 +0000108
109
Tim Peters14e26402001-02-20 20:15:19 +0000110static Py_complex
111c_atanh(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000112{
Tim Peters8cb82bd2001-02-20 20:36:38 +0000113 return c_prod(c_half,c_log(c_quot(c_sum(c_one,x),c_diff(c_one,x))));
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000114}
115
Tim Peters14e26402001-02-20 20:15:19 +0000116static char c_atanh_doc[] =
117"atanh(x)\n"
118"\n"
119"Return the hyperbolic arc tangent of x.";
Guido van Rossumc6e22901998-12-04 19:26:43 +0000120
121
Tim Peters14e26402001-02-20 20:15:19 +0000122static Py_complex
123c_cos(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000124{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000125 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000126 r.real = cos(x.real)*cosh(x.imag);
127 r.imag = -sin(x.real)*sinh(x.imag);
128 return r;
129}
130
Tim Peters14e26402001-02-20 20:15:19 +0000131static char c_cos_doc[] =
132"cos(x)\n"
133"n"
134"Return the cosine of x.";
Guido van Rossumc6e22901998-12-04 19:26:43 +0000135
136
Tim Peters14e26402001-02-20 20:15:19 +0000137static Py_complex
138c_cosh(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000139{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000140 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000141 r.real = cos(x.imag)*cosh(x.real);
142 r.imag = sin(x.imag)*sinh(x.real);
143 return r;
144}
145
Tim Peters14e26402001-02-20 20:15:19 +0000146static char c_cosh_doc[] =
147"cosh(x)\n"
148"n"
149"Return the hyperbolic cosine of x.";
Guido van Rossumc6e22901998-12-04 19:26:43 +0000150
151
Tim Peters14e26402001-02-20 20:15:19 +0000152static Py_complex
153c_exp(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000154{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000155 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000156 double l = exp(x.real);
157 r.real = l*cos(x.imag);
158 r.imag = l*sin(x.imag);
159 return r;
160}
161
Tim Peters14e26402001-02-20 20:15:19 +0000162static char c_exp_doc[] =
163"exp(x)\n"
164"\n"
165"Return the exponential value e**x.";
Guido van Rossumc6e22901998-12-04 19:26:43 +0000166
167
Tim Peters14e26402001-02-20 20:15:19 +0000168static Py_complex
169c_log(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000170{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000171 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000172 double l = hypot(x.real,x.imag);
173 r.imag = atan2(x.imag, x.real);
174 r.real = log(l);
175 return r;
176}
177
Tim Peters14e26402001-02-20 20:15:19 +0000178static char c_log_doc[] =
179"log(x)\n"
180"\n"
181"Return the natural logarithm of x.";
Guido van Rossumc6e22901998-12-04 19:26:43 +0000182
183
Tim Peters14e26402001-02-20 20:15:19 +0000184static Py_complex
185c_log10(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000186{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000187 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000188 double l = hypot(x.real,x.imag);
189 r.imag = atan2(x.imag, x.real)/log(10.);
190 r.real = log10(l);
191 return r;
192}
193
Tim Peters14e26402001-02-20 20:15:19 +0000194static char c_log10_doc[] =
195"log10(x)\n"
196"\n"
197"Return the base-10 logarithm of x.";
Guido van Rossumc6e22901998-12-04 19:26:43 +0000198
199
200/* internal function not available from Python */
Tim Peters14e26402001-02-20 20:15:19 +0000201static Py_complex
202c_prodi(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000203{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000204 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000205 r.real = -x.imag;
206 r.imag = x.real;
207 return r;
208}
209
Guido van Rossumc6e22901998-12-04 19:26:43 +0000210
Tim Peters14e26402001-02-20 20:15:19 +0000211static Py_complex
212c_sin(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000213{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000214 Py_complex r;
Tim Peters14e26402001-02-20 20:15:19 +0000215 r.real = sin(x.real) * cosh(x.imag);
216 r.imag = cos(x.real) * sinh(x.imag);
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000217 return r;
218}
219
Tim Peters14e26402001-02-20 20:15:19 +0000220static char c_sin_doc[] =
221"sin(x)\n"
222"\n"
223"Return the sine of x.";
Guido van Rossumc6e22901998-12-04 19:26:43 +0000224
225
Tim Peters14e26402001-02-20 20:15:19 +0000226static Py_complex
227c_sinh(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000228{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000229 Py_complex r;
Tim Peters14e26402001-02-20 20:15:19 +0000230 r.real = cos(x.imag) * sinh(x.real);
231 r.imag = sin(x.imag) * cosh(x.real);
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000232 return r;
233}
234
Tim Peters14e26402001-02-20 20:15:19 +0000235static char c_sinh_doc[] =
236"sinh(x)\n"
237"\n"
238"Return the hyperbolic sine of x.";
Guido van Rossumc6e22901998-12-04 19:26:43 +0000239
240
Tim Peters14e26402001-02-20 20:15:19 +0000241static Py_complex
242c_sqrt(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000243{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000244 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000245 double s,d;
246 if (x.real == 0. && x.imag == 0.)
247 r = x;
248 else {
249 s = sqrt(0.5*(fabs(x.real) + hypot(x.real,x.imag)));
250 d = 0.5*x.imag/s;
251 if (x.real > 0.) {
252 r.real = s;
253 r.imag = d;
254 }
255 else if (x.imag >= 0.) {
256 r.real = d;
257 r.imag = s;
258 }
259 else {
260 r.real = -d;
261 r.imag = -s;
262 }
263 }
264 return r;
265}
266
Tim Peters14e26402001-02-20 20:15:19 +0000267static char c_sqrt_doc[] =
268"sqrt(x)\n"
269"\n"
270"Return the square root of x.";
Guido van Rossumc6e22901998-12-04 19:26:43 +0000271
272
Tim Peters14e26402001-02-20 20:15:19 +0000273static Py_complex
274c_tan(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000275{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000276 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000277 double sr,cr,shi,chi;
278 double rs,is,rc,ic;
279 double d;
280 sr = sin(x.real);
281 cr = cos(x.real);
282 shi = sinh(x.imag);
283 chi = cosh(x.imag);
Tim Peters14e26402001-02-20 20:15:19 +0000284 rs = sr * chi;
285 is = cr * shi;
286 rc = cr * chi;
287 ic = -sr * shi;
288 d = rc*rc + ic * ic;
289 r.real = (rs*rc + is*ic) / d;
290 r.imag = (is*rc - rs*ic) / d;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000291 return r;
292}
293
Tim Peters14e26402001-02-20 20:15:19 +0000294static char c_tan_doc[] =
295"tan(x)\n"
296"\n"
297"Return the tangent of x.";
Guido van Rossumc6e22901998-12-04 19:26:43 +0000298
299
Tim Peters14e26402001-02-20 20:15:19 +0000300static Py_complex
301c_tanh(Py_complex x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000302{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000303 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000304 double si,ci,shr,chr;
305 double rs,is,rc,ic;
306 double d;
307 si = sin(x.imag);
308 ci = cos(x.imag);
309 shr = sinh(x.real);
310 chr = cosh(x.real);
Tim Peters14e26402001-02-20 20:15:19 +0000311 rs = ci * shr;
312 is = si * chr;
313 rc = ci * chr;
314 ic = si * shr;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000315 d = rc*rc + ic*ic;
Tim Peters14e26402001-02-20 20:15:19 +0000316 r.real = (rs*rc + is*ic) / d;
317 r.imag = (is*rc - rs*ic) / d;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000318 return r;
319}
320
Tim Peters14e26402001-02-20 20:15:19 +0000321static char c_tanh_doc[] =
322"tanh(x)\n"
323"\n"
324"Return the hyperbolic tangent of x.";
Guido van Rossumc6e22901998-12-04 19:26:43 +0000325
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000326
327/* And now the glue to make them available from Python: */
328
Roger E. Masse24070ca1996-12-09 22:59:53 +0000329static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000330math_error(void)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000331{
332 if (errno == EDOM)
Roger E. Masse24070ca1996-12-09 22:59:53 +0000333 PyErr_SetString(PyExc_ValueError, "math domain error");
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000334 else if (errno == ERANGE)
Roger E. Masse24070ca1996-12-09 22:59:53 +0000335 PyErr_SetString(PyExc_OverflowError, "math range error");
336 else /* Unexpected math error */
Tim Peters14e26402001-02-20 20:15:19 +0000337 PyErr_SetFromErrno(PyExc_ValueError);
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000338 return NULL;
339}
340
Roger E. Masse24070ca1996-12-09 22:59:53 +0000341static PyObject *
Peter Schneider-Kampf1ca8982000-07-10 09:31:34 +0000342math_1(PyObject *args, Py_complex (*func)(Py_complex))
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000343{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000344 Py_complex x;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000345 if (!PyArg_ParseTuple(args, "D", &x))
346 return NULL;
347 errno = 0;
Guido van Rossum52fa3a61997-02-14 22:59:58 +0000348 PyFPE_START_PROTECT("complex function", return 0)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000349 x = (*func)(x);
Guido van Rossum45b83911997-03-14 04:32:50 +0000350 PyFPE_END_PROTECT(x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000351 CHECK(x.real);
352 CHECK(x.imag);
353 if (errno != 0)
354 return math_error();
355 else
Roger E. Masse24070ca1996-12-09 22:59:53 +0000356 return PyComplex_FromCComplex(x);
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000357}
358
359#define FUNC1(stubname, func) \
Peter Schneider-Kampf1ca8982000-07-10 09:31:34 +0000360 static PyObject * stubname(PyObject *self, PyObject *args) { \
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000361 return math_1(args, func); \
362 }
363
364FUNC1(cmath_acos, c_acos)
365FUNC1(cmath_acosh, c_acosh)
366FUNC1(cmath_asin, c_asin)
367FUNC1(cmath_asinh, c_asinh)
368FUNC1(cmath_atan, c_atan)
369FUNC1(cmath_atanh, c_atanh)
370FUNC1(cmath_cos, c_cos)
371FUNC1(cmath_cosh, c_cosh)
372FUNC1(cmath_exp, c_exp)
373FUNC1(cmath_log, c_log)
374FUNC1(cmath_log10, c_log10)
375FUNC1(cmath_sin, c_sin)
376FUNC1(cmath_sinh, c_sinh)
377FUNC1(cmath_sqrt, c_sqrt)
378FUNC1(cmath_tan, c_tan)
379FUNC1(cmath_tanh, c_tanh)
380
381
Tim Peters14e26402001-02-20 20:15:19 +0000382static char module_doc[] =
383"This module is always available. It provides access to mathematical\n"
384"functions for complex numbers.";
Guido van Rossumc6e22901998-12-04 19:26:43 +0000385
Roger E. Masse24070ca1996-12-09 22:59:53 +0000386static PyMethodDef cmath_methods[] = {
Tim Peters14e26402001-02-20 20:15:19 +0000387 {"acos", cmath_acos, METH_VARARGS, c_acos_doc},
388 {"acosh", cmath_acosh, METH_VARARGS, c_acosh_doc},
389 {"asin", cmath_asin, METH_VARARGS, c_asin_doc},
390 {"asinh", cmath_asinh, METH_VARARGS, c_asinh_doc},
391 {"atan", cmath_atan, METH_VARARGS, c_atan_doc},
392 {"atanh", cmath_atanh, METH_VARARGS, c_atanh_doc},
393 {"cos", cmath_cos, METH_VARARGS, c_cos_doc},
394 {"cosh", cmath_cosh, METH_VARARGS, c_cosh_doc},
395 {"exp", cmath_exp, METH_VARARGS, c_exp_doc},
396 {"log", cmath_log, METH_VARARGS, c_log_doc},
397 {"log10", cmath_log10, METH_VARARGS, c_log10_doc},
398 {"sin", cmath_sin, METH_VARARGS, c_sin_doc},
399 {"sinh", cmath_sinh, METH_VARARGS, c_sinh_doc},
400 {"sqrt", cmath_sqrt, METH_VARARGS, c_sqrt_doc},
401 {"tan", cmath_tan, METH_VARARGS, c_tan_doc},
402 {"tanh", cmath_tanh, METH_VARARGS, c_tanh_doc},
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000403 {NULL, NULL} /* sentinel */
404};
405
Guido van Rossum3886bb61998-12-04 18:50:17 +0000406DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000407initcmath(void)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000408{
Roger E. Masse24070ca1996-12-09 22:59:53 +0000409 PyObject *m, *d, *v;
Tim Peters14e26402001-02-20 20:15:19 +0000410
Guido van Rossumc6e22901998-12-04 19:26:43 +0000411 m = Py_InitModule3("cmath", cmath_methods, module_doc);
Roger E. Masse24070ca1996-12-09 22:59:53 +0000412 d = PyModule_GetDict(m);
413 PyDict_SetItemString(d, "pi",
414 v = PyFloat_FromDouble(atan(1.0) * 4.0));
415 Py_DECREF(v);
416 PyDict_SetItemString(d, "e", v = PyFloat_FromDouble(exp(1.0)));
417 Py_DECREF(v);
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000418}