blob: 9c28623afca67514b4e07837ced31f4c70e6b5e6 [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 */
32static complex c_1 = {1., 0.};
33static complex c_half = {0.5, 0.};
34static complex c_i = {0., 1.};
35static complex c_i2 = {0., 0.5};
36static complex c_mi = {0., -1.};
37static complex c_pi2 = {M_PI/2., 0.};
38
39/* forward declarations */
Guido van Rossum9c8a0c41996-05-24 21:12:28 +000040staticforward complex c_log();
41staticforward complex c_prodi();
42staticforward complex c_sqrt();
Guido van Rossum71aa32f1996-01-12 01:34:57 +000043
44
Guido van Rossum9c8a0c41996-05-24 21:12:28 +000045static complex c_acos(x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +000046 complex x;
47{
48 return c_neg(c_prodi(c_log(c_sum(x,c_prod(c_i,
49 c_sqrt(c_diff(c_1,c_prod(x,x))))))));
50}
51
Guido van Rossum9c8a0c41996-05-24 21:12:28 +000052static complex c_acosh(x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +000053 complex x;
54{
55 return c_log(c_sum(x,c_prod(c_i,
56 c_sqrt(c_diff(c_1,c_prod(x,x))))));
57}
58
Guido van Rossum9c8a0c41996-05-24 21:12:28 +000059static complex c_asin(x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +000060 complex x;
61{
62 return c_neg(c_prodi(c_log(c_sum(c_prod(c_i,x),
63 c_sqrt(c_diff(c_1,c_prod(x,x)))))));
64}
65
Guido van Rossum9c8a0c41996-05-24 21:12:28 +000066static complex c_asinh(x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +000067 complex x;
68{
69 return c_neg(c_log(c_diff(c_sqrt(c_sum(c_1,c_prod(x,x))),x)));
70}
71
Guido van Rossum9c8a0c41996-05-24 21:12:28 +000072static complex c_atan(x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +000073 complex x;
74{
75 return c_prod(c_i2,c_log(c_quot(c_sum(c_i,x),c_diff(c_i,x))));
76}
77
Guido van Rossum9c8a0c41996-05-24 21:12:28 +000078static complex c_atanh(x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +000079 complex x;
80{
81 return c_prod(c_half,c_log(c_quot(c_sum(c_1,x),c_diff(c_1,x))));
82}
83
Guido van Rossum9c8a0c41996-05-24 21:12:28 +000084static complex c_cos(x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +000085 complex x;
86{
87 complex r;
88 r.real = cos(x.real)*cosh(x.imag);
89 r.imag = -sin(x.real)*sinh(x.imag);
90 return r;
91}
92
Guido van Rossum9c8a0c41996-05-24 21:12:28 +000093static complex c_cosh(x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +000094 complex x;
95{
96 complex r;
97 r.real = cos(x.imag)*cosh(x.real);
98 r.imag = sin(x.imag)*sinh(x.real);
99 return r;
100}
101
Guido van Rossum9c8a0c41996-05-24 21:12:28 +0000102static complex c_exp(x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000103 complex x;
104{
105 complex r;
106 double l = exp(x.real);
107 r.real = l*cos(x.imag);
108 r.imag = l*sin(x.imag);
109 return r;
110}
111
Guido van Rossum9c8a0c41996-05-24 21:12:28 +0000112static complex c_log(x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000113 complex x;
114{
115 complex r;
116 double l = hypot(x.real,x.imag);
117 r.imag = atan2(x.imag, x.real);
118 r.real = log(l);
119 return r;
120}
121
Guido van Rossum9c8a0c41996-05-24 21:12:28 +0000122static complex c_log10(x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000123 complex x;
124{
125 complex r;
126 double l = hypot(x.real,x.imag);
127 r.imag = atan2(x.imag, x.real)/log(10.);
128 r.real = log10(l);
129 return r;
130}
131
Guido van Rossum9c8a0c41996-05-24 21:12:28 +0000132static complex c_prodi(x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000133 complex x;
134{
135 complex r;
136 r.real = -x.imag;
137 r.imag = x.real;
138 return r;
139}
140
Guido van Rossum9c8a0c41996-05-24 21:12:28 +0000141static complex c_sin(x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000142 complex x;
143{
144 complex r;
145 r.real = sin(x.real)*cosh(x.imag);
146 r.imag = cos(x.real)*sinh(x.imag);
147 return r;
148}
149
Guido van Rossum9c8a0c41996-05-24 21:12:28 +0000150static complex c_sinh(x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000151 complex x;
152{
153 complex r;
154 r.real = cos(x.imag)*sinh(x.real);
155 r.imag = sin(x.imag)*cosh(x.real);
156 return r;
157}
158
Guido van Rossum9c8a0c41996-05-24 21:12:28 +0000159static complex c_sqrt(x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000160 complex x;
161{
162 complex r;
163 double s,d;
164 if (x.real == 0. && x.imag == 0.)
165 r = x;
166 else {
167 s = sqrt(0.5*(fabs(x.real) + hypot(x.real,x.imag)));
168 d = 0.5*x.imag/s;
169 if (x.real > 0.) {
170 r.real = s;
171 r.imag = d;
172 }
173 else if (x.imag >= 0.) {
174 r.real = d;
175 r.imag = s;
176 }
177 else {
178 r.real = -d;
179 r.imag = -s;
180 }
181 }
182 return r;
183}
184
Guido van Rossum9c8a0c41996-05-24 21:12:28 +0000185static complex c_tan(x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000186 complex x;
187{
188 complex r;
189 double sr,cr,shi,chi;
190 double rs,is,rc,ic;
191 double d;
192 sr = sin(x.real);
193 cr = cos(x.real);
194 shi = sinh(x.imag);
195 chi = cosh(x.imag);
196 rs = sr*chi;
197 is = cr*shi;
198 rc = cr*chi;
199 ic = -sr*shi;
200 d = rc*rc + ic*ic;
201 r.real = (rs*rc+is*ic)/d;
202 r.imag = (is*rc-rs*ic)/d;
203 return r;
204}
205
Guido van Rossum9c8a0c41996-05-24 21:12:28 +0000206static complex c_tanh(x)
Guido van Rossum71aa32f1996-01-12 01:34:57 +0000207 complex x;
208{
209 complex r;
210 double si,ci,shr,chr;
211 double rs,is,rc,ic;
212 double d;
213 si = sin(x.imag);
214 ci = cos(x.imag);
215 shr = sinh(x.real);
216 chr = cosh(x.real);
217 rs = ci*shr;
218 is = si*chr;
219 rc = ci*chr;
220 ic = si*shr;
221 d = rc*rc + ic*ic;
222 r.real = (rs*rc+is*ic)/d;
223 r.imag = (is*rc-rs*ic)/d;
224 return r;
225}
226
227
228/* And now the glue to make them available from Python: */
229
230static object *
231math_error()
232{
233 if (errno == EDOM)
234 err_setstr(ValueError, "math domain error");
235 else if (errno == ERANGE)
236 err_setstr(OverflowError, "math range error");
237 else
238 err_errno(ValueError); /* Unexpected math error */
239 return NULL;
240}
241
242static object *
243math_1(args, func)
244 object *args;
245 complex (*func) FPROTO((complex));
246{
247 complex x;
248 if (!PyArg_ParseTuple(args, "D", &x))
249 return NULL;
250 errno = 0;
251 x = (*func)(x);
252 CHECK(x.real);
253 CHECK(x.imag);
254 if (errno != 0)
255 return math_error();
256 else
257 return newcomplexobject(x);
258}
259
260#define FUNC1(stubname, func) \
261 static object * stubname(self, args) object *self, *args; { \
262 return math_1(args, func); \
263 }
264
265FUNC1(cmath_acos, c_acos)
266FUNC1(cmath_acosh, c_acosh)
267FUNC1(cmath_asin, c_asin)
268FUNC1(cmath_asinh, c_asinh)
269FUNC1(cmath_atan, c_atan)
270FUNC1(cmath_atanh, c_atanh)
271FUNC1(cmath_cos, c_cos)
272FUNC1(cmath_cosh, c_cosh)
273FUNC1(cmath_exp, c_exp)
274FUNC1(cmath_log, c_log)
275FUNC1(cmath_log10, c_log10)
276FUNC1(cmath_sin, c_sin)
277FUNC1(cmath_sinh, c_sinh)
278FUNC1(cmath_sqrt, c_sqrt)
279FUNC1(cmath_tan, c_tan)
280FUNC1(cmath_tanh, c_tanh)
281
282
283static struct methodlist cmath_methods[] = {
284 {"acos", cmath_acos, 1},
285 {"acosh", cmath_acosh, 1},
286 {"asin", cmath_asin, 1},
287 {"asinh", cmath_asinh, 1},
288 {"atan", cmath_atan, 1},
289 {"atanh", cmath_atanh, 1},
290 {"cos", cmath_cos, 1},
291 {"cosh", cmath_cosh, 1},
292 {"exp", cmath_exp, 1},
293 {"log", cmath_log, 1},
294 {"log10", cmath_log10, 1},
295 {"sin", cmath_sin, 1},
296 {"sinh", cmath_sinh, 1},
297 {"sqrt", cmath_sqrt, 1},
298 {"tan", cmath_tan, 1},
299 {"tanh", cmath_tanh, 1},
300 {NULL, NULL} /* sentinel */
301};
302
303void
304initcmath()
305{
306 object *m, *d, *v;
307
308 m = Py_InitModule("cmath", cmath_methods);
309 d = getmoduledict(m);
310 dictinsert(d, "pi", v = newfloatobject(atan(1.0) * 4.0));
311 DECREF(v);
312 dictinsert(d, "e", v = newfloatobject(exp(1.0)));
313 DECREF(v);
314}