|
3334
|
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 */
|
|
3771
|
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};
|
|
|
36 static Py_complex c_mi = {0., -1.};
|
|
|
37 static Py_complex c_pi2 = {M_PI/2., 0.};
|
|
3334
|
38
|
|
|
39 /* forward declarations */
|
|
3771
|
40 staticforward Py_complex c_log();
|
|
|
41 staticforward Py_complex c_prodi();
|
|
|
42 staticforward Py_complex c_sqrt();
|
|
3334
|
43
|
|
|
44
|
|
3771
|
45 static Py_complex c_acos(x)
|
|
|
46 Py_complex x;
|
|
3334
|
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
|
|
3771
|
52 static Py_complex c_acosh(x)
|
|
|
53 Py_complex x;
|
|
3334
|
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
|
|
3771
|
59 static Py_complex c_asin(x)
|
|
|
60 Py_complex x;
|
|
3334
|
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
|
|
3771
|
66 static Py_complex c_asinh(x)
|
|
|
67 Py_complex x;
|
|
3334
|
68 {
|
|
|
69 return c_neg(c_log(c_diff(c_sqrt(c_sum(c_1,c_prod(x,x))),x)));
|
|
|
70 }
|
|
|
71
|
|
3771
|
72 static Py_complex c_atan(x)
|
|
|
73 Py_complex x;
|
|
3334
|
74 {
|
|
|
75 return c_prod(c_i2,c_log(c_quot(c_sum(c_i,x),c_diff(c_i,x))));
|
|
|
76 }
|
|
|
77
|
|
3771
|
78 static Py_complex c_atanh(x)
|
|
|
79 Py_complex x;
|
|
3334
|
80 {
|
|
|
81 return c_prod(c_half,c_log(c_quot(c_sum(c_1,x),c_diff(c_1,x))));
|
|
|
82 }
|
|
|
83
|
|
3771
|
84 static Py_complex c_cos(x)
|
|
|
85 Py_complex x;
|
|
3334
|
86 {
|
|
3771
|
87 Py_complex r;
|
|
3334
|
88 r.real = cos(x.real)*cosh(x.imag);
|
|
|
89 r.imag = -sin(x.real)*sinh(x.imag);
|
|
|
90 return r;
|
|
|
91 }
|
|
|
92
|
|
3771
|
93 static Py_complex c_cosh(x)
|
|
|
94 Py_complex x;
|
|
3334
|
95 {
|
|
3771
|
96 Py_complex r;
|
|
3334
|
97 r.real = cos(x.imag)*cosh(x.real);
|
|
|
98 r.imag = sin(x.imag)*sinh(x.real);
|
|
|
99 return r;
|
|
|
100 }
|
|
|
101
|
|
3771
|
102 static Py_complex c_exp(x)
|
|
|
103 Py_complex x;
|
|
3334
|
104 {
|
|
3771
|
105 Py_complex r;
|
|
3334
|
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
|
|
3771
|
112 static Py_complex c_log(x)
|
|
|
113 Py_complex x;
|
|
3334
|
114 {
|
|
3771
|
115 Py_complex r;
|
|
3334
|
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
|
|
3771
|
122 static Py_complex c_log10(x)
|
|
|
123 Py_complex x;
|
|
3334
|
124 {
|
|
3771
|
125 Py_complex r;
|
|
3334
|
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
|
|
3771
|
132 static Py_complex c_prodi(x)
|
|
|
133 Py_complex x;
|
|
3334
|
134 {
|
|
3771
|
135 Py_complex r;
|
|
3334
|
136 r.real = -x.imag;
|
|
|
137 r.imag = x.real;
|
|
|
138 return r;
|
|
|
139 }
|
|
|
140
|
|
3771
|
141 static Py_complex c_sin(x)
|
|
|
142 Py_complex x;
|
|
3334
|
143 {
|
|
3771
|
144 Py_complex r;
|
|
3334
|
145 r.real = sin(x.real)*cosh(x.imag);
|
|
|
146 r.imag = cos(x.real)*sinh(x.imag);
|
|
|
147 return r;
|
|
|
148 }
|
|
|
149
|
|
3771
|
150 static Py_complex c_sinh(x)
|
|
|
151 Py_complex x;
|
|
3334
|
152 {
|
|
3771
|
153 Py_complex r;
|
|
3334
|
154 r.real = cos(x.imag)*sinh(x.real);
|
|
|
155 r.imag = sin(x.imag)*cosh(x.real);
|
|
|
156 return r;
|
|
|
157 }
|
|
|
158
|
|
3771
|
159 static Py_complex c_sqrt(x)
|
|
|
160 Py_complex x;
|
|
3334
|
161 {
|
|
3771
|
162 Py_complex r;
|
|
3334
|
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
|
|
3771
|
185 static Py_complex c_tan(x)
|
|
|
186 Py_complex x;
|
|
3334
|
187 {
|
|
3771
|
188 Py_complex r;
|
|
3334
|
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
|
|
3771
|
206 static Py_complex c_tanh(x)
|
|
|
207 Py_complex x;
|
|
3334
|
208 {
|
|
3771
|
209 Py_complex r;
|
|
3334
|
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
|
|
|
230 static object *
|
|
|
231 math_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
|
|
|
242 static object *
|
|
|
243 math_1(args, func)
|
|
|
244 object *args;
|
|
3771
|
245 Py_complex (*func) FPROTO((Py_complex));
|
|
3334
|
246 {
|
|
3771
|
247 Py_complex x;
|
|
3334
|
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
|
|
|
265 FUNC1(cmath_acos, c_acos)
|
|
|
266 FUNC1(cmath_acosh, c_acosh)
|
|
|
267 FUNC1(cmath_asin, c_asin)
|
|
|
268 FUNC1(cmath_asinh, c_asinh)
|
|
|
269 FUNC1(cmath_atan, c_atan)
|
|
|
270 FUNC1(cmath_atanh, c_atanh)
|
|
|
271 FUNC1(cmath_cos, c_cos)
|
|
|
272 FUNC1(cmath_cosh, c_cosh)
|
|
|
273 FUNC1(cmath_exp, c_exp)
|
|
|
274 FUNC1(cmath_log, c_log)
|
|
|
275 FUNC1(cmath_log10, c_log10)
|
|
|
276 FUNC1(cmath_sin, c_sin)
|
|
|
277 FUNC1(cmath_sinh, c_sinh)
|
|
|
278 FUNC1(cmath_sqrt, c_sqrt)
|
|
|
279 FUNC1(cmath_tan, c_tan)
|
|
|
280 FUNC1(cmath_tanh, c_tanh)
|
|
|
281
|
|
|
282
|
|
|
283 static 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
|
|
|
303 void
|
|
|
304 initcmath()
|
|
|
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 }
|