Clarify portability and main program.
[python/dscho.git] / Modules / cmathmodule.c
blob4e08722bccac54f83874f6376f5ca502f6038bdc
1 /* Complex math module */
3 /* much code borrowed from mathmodule.c */
5 #include "Python.h"
7 #include "mymath.h"
9 #ifdef i860
10 /* Cray APP has bogus definition of HUGE_VAL in <math.h> */
11 #undef HUGE_VAL
12 #endif
14 #ifdef HUGE_VAL
15 #define CHECK(x) if (errno != 0) ; \
16 else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
17 else errno = ERANGE
18 #else
19 #define CHECK(x) /* Don't know how to check */
20 #endif
22 #ifndef M_PI
23 #define M_PI (3.141592653589793239)
24 #endif
26 /* First, the C functions that do the real work */
28 /* constants */
29 static Py_complex c_1 = {1., 0.};
30 static Py_complex c_half = {0.5, 0.};
31 static Py_complex c_i = {0., 1.};
32 static Py_complex c_i2 = {0., 0.5};
33 #if 0
34 static Py_complex c_mi = {0., -1.};
35 static Py_complex c_pi2 = {M_PI/2., 0.};
36 #endif
38 /* forward declarations */
39 staticforward Py_complex c_log();
40 staticforward Py_complex c_prodi();
41 staticforward Py_complex c_sqrt();
44 static Py_complex c_acos(x)
45 Py_complex x;
47 return c_neg(c_prodi(c_log(c_sum(x,c_prod(c_i,
48 c_sqrt(c_diff(c_1,c_prod(x,x))))))));
51 static Py_complex c_acosh(x)
52 Py_complex x;
54 return c_log(c_sum(x,c_prod(c_i,
55 c_sqrt(c_diff(c_1,c_prod(x,x))))));
58 static Py_complex c_asin(x)
59 Py_complex x;
61 return c_neg(c_prodi(c_log(c_sum(c_prod(c_i,x),
62 c_sqrt(c_diff(c_1,c_prod(x,x)))))));
65 static Py_complex c_asinh(x)
66 Py_complex x;
68 return c_neg(c_log(c_diff(c_sqrt(c_sum(c_1,c_prod(x,x))),x)));
71 static Py_complex c_atan(x)
72 Py_complex x;
74 return c_prod(c_i2,c_log(c_quot(c_sum(c_i,x),c_diff(c_i,x))));
77 static Py_complex c_atanh(x)
78 Py_complex x;
80 return c_prod(c_half,c_log(c_quot(c_sum(c_1,x),c_diff(c_1,x))));
83 static Py_complex c_cos(x)
84 Py_complex x;
86 Py_complex r;
87 r.real = cos(x.real)*cosh(x.imag);
88 r.imag = -sin(x.real)*sinh(x.imag);
89 return r;
92 static Py_complex c_cosh(x)
93 Py_complex x;
95 Py_complex r;
96 r.real = cos(x.imag)*cosh(x.real);
97 r.imag = sin(x.imag)*sinh(x.real);
98 return r;
101 static Py_complex c_exp(x)
102 Py_complex x;
104 Py_complex r;
105 double l = exp(x.real);
106 r.real = l*cos(x.imag);
107 r.imag = l*sin(x.imag);
108 return r;
111 static Py_complex c_log(x)
112 Py_complex x;
114 Py_complex r;
115 double l = hypot(x.real,x.imag);
116 r.imag = atan2(x.imag, x.real);
117 r.real = log(l);
118 return r;
121 static Py_complex c_log10(x)
122 Py_complex x;
124 Py_complex r;
125 double l = hypot(x.real,x.imag);
126 r.imag = atan2(x.imag, x.real)/log(10.);
127 r.real = log10(l);
128 return r;
131 static Py_complex c_prodi(x)
132 Py_complex x;
134 Py_complex r;
135 r.real = -x.imag;
136 r.imag = x.real;
137 return r;
140 static Py_complex c_sin(x)
141 Py_complex x;
143 Py_complex r;
144 r.real = sin(x.real)*cosh(x.imag);
145 r.imag = cos(x.real)*sinh(x.imag);
146 return r;
149 static Py_complex c_sinh(x)
150 Py_complex x;
152 Py_complex r;
153 r.real = cos(x.imag)*sinh(x.real);
154 r.imag = sin(x.imag)*cosh(x.real);
155 return r;
158 static Py_complex c_sqrt(x)
159 Py_complex x;
161 Py_complex r;
162 double s,d;
163 if (x.real == 0. && x.imag == 0.)
164 r = x;
165 else {
166 s = sqrt(0.5*(fabs(x.real) + hypot(x.real,x.imag)));
167 d = 0.5*x.imag/s;
168 if (x.real > 0.) {
169 r.real = s;
170 r.imag = d;
172 else if (x.imag >= 0.) {
173 r.real = d;
174 r.imag = s;
176 else {
177 r.real = -d;
178 r.imag = -s;
181 return r;
184 static Py_complex c_tan(x)
185 Py_complex x;
187 Py_complex r;
188 double sr,cr,shi,chi;
189 double rs,is,rc,ic;
190 double d;
191 sr = sin(x.real);
192 cr = cos(x.real);
193 shi = sinh(x.imag);
194 chi = cosh(x.imag);
195 rs = sr*chi;
196 is = cr*shi;
197 rc = cr*chi;
198 ic = -sr*shi;
199 d = rc*rc + ic*ic;
200 r.real = (rs*rc+is*ic)/d;
201 r.imag = (is*rc-rs*ic)/d;
202 return r;
205 static Py_complex c_tanh(x)
206 Py_complex x;
208 Py_complex r;
209 double si,ci,shr,chr;
210 double rs,is,rc,ic;
211 double d;
212 si = sin(x.imag);
213 ci = cos(x.imag);
214 shr = sinh(x.real);
215 chr = cosh(x.real);
216 rs = ci*shr;
217 is = si*chr;
218 rc = ci*chr;
219 ic = si*shr;
220 d = rc*rc + ic*ic;
221 r.real = (rs*rc+is*ic)/d;
222 r.imag = (is*rc-rs*ic)/d;
223 return r;
227 /* And now the glue to make them available from Python: */
229 static PyObject *
230 math_error()
232 if (errno == EDOM)
233 PyErr_SetString(PyExc_ValueError, "math domain error");
234 else if (errno == ERANGE)
235 PyErr_SetString(PyExc_OverflowError, "math range error");
236 else /* Unexpected math error */
237 PyErr_SetFromErrno(PyExc_ValueError);
238 return NULL;
241 static PyObject *
242 math_1(args, func)
243 PyObject *args;
244 Py_complex (*func) Py_FPROTO((Py_complex));
246 Py_complex x;
247 if (!PyArg_ParseTuple(args, "D", &x))
248 return NULL;
249 errno = 0;
250 PyFPE_START_PROTECT("complex function", return 0)
251 x = (*func)(x);
252 PyFPE_END_PROTECT(x)
253 CHECK(x.real);
254 CHECK(x.imag);
255 if (errno != 0)
256 return math_error();
257 else
258 return PyComplex_FromCComplex(x);
261 #define FUNC1(stubname, func) \
262 static PyObject * stubname(self, args) PyObject *self, *args; { \
263 return math_1(args, func); \
266 FUNC1(cmath_acos, c_acos)
267 FUNC1(cmath_acosh, c_acosh)
268 FUNC1(cmath_asin, c_asin)
269 FUNC1(cmath_asinh, c_asinh)
270 FUNC1(cmath_atan, c_atan)
271 FUNC1(cmath_atanh, c_atanh)
272 FUNC1(cmath_cos, c_cos)
273 FUNC1(cmath_cosh, c_cosh)
274 FUNC1(cmath_exp, c_exp)
275 FUNC1(cmath_log, c_log)
276 FUNC1(cmath_log10, c_log10)
277 FUNC1(cmath_sin, c_sin)
278 FUNC1(cmath_sinh, c_sinh)
279 FUNC1(cmath_sqrt, c_sqrt)
280 FUNC1(cmath_tan, c_tan)
281 FUNC1(cmath_tanh, c_tanh)
284 static PyMethodDef cmath_methods[] = {
285 {"acos", cmath_acos, 1},
286 {"acosh", cmath_acosh, 1},
287 {"asin", cmath_asin, 1},
288 {"asinh", cmath_asinh, 1},
289 {"atan", cmath_atan, 1},
290 {"atanh", cmath_atanh, 1},
291 {"cos", cmath_cos, 1},
292 {"cosh", cmath_cosh, 1},
293 {"exp", cmath_exp, 1},
294 {"log", cmath_log, 1},
295 {"log10", cmath_log10, 1},
296 {"sin", cmath_sin, 1},
297 {"sinh", cmath_sinh, 1},
298 {"sqrt", cmath_sqrt, 1},
299 {"tan", cmath_tan, 1},
300 {"tanh", cmath_tanh, 1},
301 {NULL, NULL} /* sentinel */
304 void
305 initcmath()
307 PyObject *m, *d, *v;
309 m = Py_InitModule("cmath", cmath_methods);
310 d = PyModule_GetDict(m);
311 PyDict_SetItemString(d, "pi",
312 v = PyFloat_FromDouble(atan(1.0) * 4.0));
313 Py_DECREF(v);
314 PyDict_SetItemString(d, "e", v = PyFloat_FromDouble(exp(1.0)));
315 Py_DECREF(v);