blob: 4e1715cb97a8e3dca7848971f6c7ffac0cb878b2 [file] [log] [blame]
Guido van Rossum71aa32f1996-01-12 01:34:57 +00001/* 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 Rossum9e720e31996-07-21 02:31:35 +000032static Py_complex c_1 = {1., 0.};
33static Py_complex c_half = {0.5, 0.};
34static Py_complex c_i = {0., 1.};
35static Py_complex c_i2 = {0., 0.5};
Guido van Rossuma376cc51996-12-05 23:43:35 +000036#if 0
Guido van Rossum9e720e31996-07-21 02:31:35 +000037static Py_complex c_mi = {0., -1.};
38static Py_complex c_pi2 = {M_PI/2., 0.};
Guido van Rossuma376cc51996-12-05 23:43:35 +000039#endif
Guido van Rossum71aa32f1996-01-12 01:34:57 +000040
41/* forward declarations */
Guido van Rossum9e720e31996-07-21 02:31:35 +000042staticforward Py_complex c_log();
43staticforward Py_complex c_prodi();
44staticforward Py_complex c_sqrt();
Guido van Rossum71aa32f1996-01-12 01:34:57 +000045
46
Guido van Rossum9e720e31996-07-21 02:31:35 +000047static Py_complex c_acos(x)
48 Py_complex x;
Guido van Rossum71aa32f1996-01-12 01:34:57 +000049{
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 Rossum9e720e31996-07-21 02:31:35 +000054static Py_complex c_acosh(x)
55 Py_complex x;
Guido van Rossum71aa32f1996-01-12 01:34:57 +000056{
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 Rossum9e720e31996-07-21 02:31:35 +000061static Py_complex c_asin(x)
62 Py_complex x;
Guido van Rossum71aa32f1996-01-12 01:34:57 +000063{
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 Rossum9e720e31996-07-21 02:31:35 +000068static Py_complex c_asinh(x)
69 Py_complex x;
Guido van Rossum71aa32f1996-01-12 01:34:57 +000070{
71 return c_neg(c_log(c_diff(c_sqrt(c_sum(c_1,c_prod(x,x))),x)));
72}
73
Guido van Rossum9e720e31996-07-21 02:31:35 +000074static Py_complex c_atan(x)
75 Py_complex x;
Guido van Rossum71aa32f1996-01-12 01:34:57 +000076{
77 return c_prod(c_i2,c_log(c_quot(c_sum(c_i,x),c_diff(c_i,x))));
78}
79
Guido van Rossum9e720e31996-07-21 02:31:35 +000080static Py_complex c_atanh(x)
81 Py_complex x;
Guido van Rossum71aa32f1996-01-12 01:34:57 +000082{
83 return c_prod(c_half,c_log(c_quot(c_sum(c_1,x),c_diff(c_1,x))));
84}
85
Guido van Rossum9e720e31996-07-21 02:31:35 +000086static Py_complex c_cos(x)
87 Py_complex x;
Guido van Rossum71aa32f1996-01-12 01:34:57 +000088{
Guido van Rossum9e720e31996-07-21 02:31:35 +000089 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +000090 r.real = cos(x.real)*cosh(x.imag);
91 r.imag = -sin(x.real)*sinh(x.imag);
92 return r;
93}
94
Guido van Rossum9e720e31996-07-21 02:31:35 +000095static Py_complex c_cosh(x)
96 Py_complex x;
Guido van Rossum71aa32f1996-01-12 01:34:57 +000097{
Guido van Rossum9e720e31996-07-21 02:31:35 +000098 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +000099 r.real = cos(x.imag)*cosh(x.real);
100 r.imag = sin(x.imag)*sinh(x.real);
101 return r;
102}
103
Guido van Rossum9e720e31996-07-21 02:31:35 +0000104static Py_complex c_exp(x)
105 Py_complex x;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000106{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000107 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000108 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 Rossum9e720e31996-07-21 02:31:35 +0000114static Py_complex c_log(x)
115 Py_complex x;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000116{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000117 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000118 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 Rossum9e720e31996-07-21 02:31:35 +0000124static Py_complex c_log10(x)
125 Py_complex x;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000126{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000127 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000128 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 Rossum9e720e31996-07-21 02:31:35 +0000134static Py_complex c_prodi(x)
135 Py_complex x;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000136{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000137 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000138 r.real = -x.imag;
139 r.imag = x.real;
140 return r;
141}
142
Guido van Rossum9e720e31996-07-21 02:31:35 +0000143static Py_complex c_sin(x)
144 Py_complex x;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000145{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000146 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000147 r.real = sin(x.real)*cosh(x.imag);
148 r.imag = cos(x.real)*sinh(x.imag);
149 return r;
150}
151
Guido van Rossum9e720e31996-07-21 02:31:35 +0000152static Py_complex c_sinh(x)
153 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 r.real = cos(x.imag)*sinh(x.real);
157 r.imag = sin(x.imag)*cosh(x.real);
158 return r;
159}
160
Guido van Rossum9e720e31996-07-21 02:31:35 +0000161static Py_complex c_sqrt(x)
162 Py_complex x;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000163{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000164 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000165 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 Rossum9e720e31996-07-21 02:31:35 +0000187static Py_complex c_tan(x)
188 Py_complex x;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000189{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000190 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000191 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 Rossum9e720e31996-07-21 02:31:35 +0000208static Py_complex c_tanh(x)
209 Py_complex x;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000210{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000211 Py_complex r;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000212 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
232static object *
233math_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
244static object *
245math_1(args, func)
246 object *args;
Guido van Rossum9e720e31996-07-21 02:31:35 +0000247 Py_complex (*func) FPROTO((Py_complex));
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000248{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000249 Py_complex x;
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000250 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
267FUNC1(cmath_acos, c_acos)
268FUNC1(cmath_acosh, c_acosh)
269FUNC1(cmath_asin, c_asin)
270FUNC1(cmath_asinh, c_asinh)
271FUNC1(cmath_atan, c_atan)
272FUNC1(cmath_atanh, c_atanh)
273FUNC1(cmath_cos, c_cos)
274FUNC1(cmath_cosh, c_cosh)
275FUNC1(cmath_exp, c_exp)
276FUNC1(cmath_log, c_log)
277FUNC1(cmath_log10, c_log10)
278FUNC1(cmath_sin, c_sin)
279FUNC1(cmath_sinh, c_sinh)
280FUNC1(cmath_sqrt, c_sqrt)
281FUNC1(cmath_tan, c_tan)
282FUNC1(cmath_tanh, c_tanh)
283
284
285static 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
305void
306initcmath()
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}