Mercurial > cpython-lru_cache-weakref
annotate Modules/cmathmodule.c @ 7278:59d2761ba1cf legacy-trunk
Remove obsolete pagenumbering / pagestyle cruft.
| author | Fred Drake <fdrake@acm.org> |
|---|---|
| date | Fri, 13 Feb 1998 07:08:56 +0000 |
| parents | 74c12704a0bd |
| children | 38023a425e48 |
| rev | line source |
|---|---|
| 3334 | 1 /* Complex math module */ |
| 2 | |
| 3 /* much code borrowed from mathmodule.c */ | |
| 4 | |
|
4639
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
5 #include "Python.h" |
| 3334 | 6 |
| 7 #include "mymath.h" | |
| 8 | |
| 9 #ifdef i860 | |
| 10 /* Cray APP has bogus definition of HUGE_VAL in <math.h> */ | |
| 11 #undef HUGE_VAL | |
| 12 #endif | |
| 13 | |
| 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 | |
| 21 | |
| 22 #ifndef M_PI | |
| 23 #define M_PI (3.141592653589793239) | |
| 24 #endif | |
| 25 | |
| 26 /* First, the C functions that do the real work */ | |
| 27 | |
| 28 /* constants */ | |
| 3771 | 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}; | |
| 4600 | 33 #if 0 |
| 3771 | 34 static Py_complex c_mi = {0., -1.}; |
| 35 static Py_complex c_pi2 = {M_PI/2., 0.}; | |
| 4600 | 36 #endif |
| 3334 | 37 |
| 38 /* forward declarations */ | |
| 3771 | 39 staticforward Py_complex c_log(); |
| 40 staticforward Py_complex c_prodi(); | |
| 41 staticforward Py_complex c_sqrt(); | |
| 3334 | 42 |
| 43 | |
| 3771 | 44 static Py_complex c_acos(x) |
| 45 Py_complex x; | |
| 3334 | 46 { |
| 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)))))))); | |
| 49 } | |
| 50 | |
| 3771 | 51 static Py_complex c_acosh(x) |
| 52 Py_complex x; | |
| 3334 | 53 { |
| 54 return c_log(c_sum(x,c_prod(c_i, | |
| 55 c_sqrt(c_diff(c_1,c_prod(x,x)))))); | |
| 56 } | |
| 57 | |
| 3771 | 58 static Py_complex c_asin(x) |
| 59 Py_complex x; | |
| 3334 | 60 { |
| 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))))))); | |
| 63 } | |
| 64 | |
| 3771 | 65 static Py_complex c_asinh(x) |
| 66 Py_complex x; | |
| 3334 | 67 { |
| 68 return c_neg(c_log(c_diff(c_sqrt(c_sum(c_1,c_prod(x,x))),x))); | |
| 69 } | |
| 70 | |
| 3771 | 71 static Py_complex c_atan(x) |
| 72 Py_complex x; | |
| 3334 | 73 { |
| 74 return c_prod(c_i2,c_log(c_quot(c_sum(c_i,x),c_diff(c_i,x)))); | |
| 75 } | |
| 76 | |
| 3771 | 77 static Py_complex c_atanh(x) |
| 78 Py_complex x; | |
| 3334 | 79 { |
| 80 return c_prod(c_half,c_log(c_quot(c_sum(c_1,x),c_diff(c_1,x)))); | |
| 81 } | |
| 82 | |
| 3771 | 83 static Py_complex c_cos(x) |
| 84 Py_complex x; | |
| 3334 | 85 { |
| 3771 | 86 Py_complex r; |
| 3334 | 87 r.real = cos(x.real)*cosh(x.imag); |
| 88 r.imag = -sin(x.real)*sinh(x.imag); | |
| 89 return r; | |
| 90 } | |
| 91 | |
| 3771 | 92 static Py_complex c_cosh(x) |
| 93 Py_complex x; | |
| 3334 | 94 { |
| 3771 | 95 Py_complex r; |
| 3334 | 96 r.real = cos(x.imag)*cosh(x.real); |
| 97 r.imag = sin(x.imag)*sinh(x.real); | |
| 98 return r; | |
| 99 } | |
| 100 | |
| 3771 | 101 static Py_complex c_exp(x) |
| 102 Py_complex x; | |
| 3334 | 103 { |
| 3771 | 104 Py_complex r; |
| 3334 | 105 double l = exp(x.real); |
| 106 r.real = l*cos(x.imag); | |
| 107 r.imag = l*sin(x.imag); | |
| 108 return r; | |
| 109 } | |
| 110 | |
| 3771 | 111 static Py_complex c_log(x) |
| 112 Py_complex x; | |
| 3334 | 113 { |
| 3771 | 114 Py_complex r; |
| 3334 | 115 double l = hypot(x.real,x.imag); |
| 116 r.imag = atan2(x.imag, x.real); | |
| 117 r.real = log(l); | |
| 118 return r; | |
| 119 } | |
| 120 | |
| 3771 | 121 static Py_complex c_log10(x) |
| 122 Py_complex x; | |
| 3334 | 123 { |
| 3771 | 124 Py_complex r; |
| 3334 | 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; | |
| 129 } | |
| 130 | |
| 3771 | 131 static Py_complex c_prodi(x) |
| 132 Py_complex x; | |
| 3334 | 133 { |
| 3771 | 134 Py_complex r; |
| 3334 | 135 r.real = -x.imag; |
| 136 r.imag = x.real; | |
| 137 return r; | |
| 138 } | |
| 139 | |
| 3771 | 140 static Py_complex c_sin(x) |
| 141 Py_complex x; | |
| 3334 | 142 { |
| 3771 | 143 Py_complex r; |
| 3334 | 144 r.real = sin(x.real)*cosh(x.imag); |
| 145 r.imag = cos(x.real)*sinh(x.imag); | |
| 146 return r; | |
| 147 } | |
| 148 | |
| 3771 | 149 static Py_complex c_sinh(x) |
| 150 Py_complex x; | |
| 3334 | 151 { |
| 3771 | 152 Py_complex r; |
| 3334 | 153 r.real = cos(x.imag)*sinh(x.real); |
| 154 r.imag = sin(x.imag)*cosh(x.real); | |
| 155 return r; | |
| 156 } | |
| 157 | |
| 3771 | 158 static Py_complex c_sqrt(x) |
| 159 Py_complex x; | |
| 3334 | 160 { |
| 3771 | 161 Py_complex r; |
| 3334 | 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; | |
| 171 } | |
| 172 else if (x.imag >= 0.) { | |
| 173 r.real = d; | |
| 174 r.imag = s; | |
| 175 } | |
| 176 else { | |
| 177 r.real = -d; | |
| 178 r.imag = -s; | |
| 179 } | |
| 180 } | |
| 181 return r; | |
| 182 } | |
| 183 | |
| 3771 | 184 static Py_complex c_tan(x) |
| 185 Py_complex x; | |
| 3334 | 186 { |
| 3771 | 187 Py_complex r; |
| 3334 | 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; | |
| 203 } | |
| 204 | |
| 3771 | 205 static Py_complex c_tanh(x) |
| 206 Py_complex x; | |
| 3334 | 207 { |
| 3771 | 208 Py_complex r; |
| 3334 | 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; | |
| 224 } | |
| 225 | |
| 226 | |
| 227 /* And now the glue to make them available from Python: */ | |
| 228 | |
|
4639
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
229 static PyObject * |
| 3334 | 230 math_error() |
| 231 { | |
| 232 if (errno == EDOM) | |
|
4639
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
233 PyErr_SetString(PyExc_ValueError, "math domain error"); |
| 3334 | 234 else if (errno == ERANGE) |
|
4639
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
235 PyErr_SetString(PyExc_OverflowError, "math range error"); |
|
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
236 else /* Unexpected math error */ |
|
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
237 PyErr_SetFromErrno(PyExc_ValueError); |
| 3334 | 238 return NULL; |
| 239 } | |
| 240 | |
|
4639
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
241 static PyObject * |
| 3334 | 242 math_1(args, func) |
|
4639
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
243 PyObject *args; |
|
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
244 Py_complex (*func) Py_FPROTO((Py_complex)); |
| 3334 | 245 { |
| 3771 | 246 Py_complex x; |
| 3334 | 247 if (!PyArg_ParseTuple(args, "D", &x)) |
| 248 return NULL; | |
| 249 errno = 0; | |
|
5120
a48f3678f9e1
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4649
diff
changeset
|
250 PyFPE_START_PROTECT("complex function", return 0) |
| 3334 | 251 x = (*func)(x); |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5120
diff
changeset
|
252 PyFPE_END_PROTECT(x) |
| 3334 | 253 CHECK(x.real); |
| 254 CHECK(x.imag); | |
| 255 if (errno != 0) | |
| 256 return math_error(); | |
| 257 else | |
|
4639
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
258 return PyComplex_FromCComplex(x); |
| 3334 | 259 } |
| 260 | |
| 261 #define FUNC1(stubname, func) \ | |
|
4639
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
262 static PyObject * stubname(self, args) PyObject *self, *args; { \ |
| 3334 | 263 return math_1(args, func); \ |
| 264 } | |
| 265 | |
| 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) | |
| 282 | |
| 283 | |
|
4639
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
284 static PyMethodDef cmath_methods[] = { |
| 3334 | 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 */ | |
| 302 }; | |
| 303 | |
| 304 void | |
| 305 initcmath() | |
| 306 { | |
|
4639
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
307 PyObject *m, *d, *v; |
| 3334 | 308 |
| 309 m = Py_InitModule("cmath", cmath_methods); | |
|
4639
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
310 d = PyModule_GetDict(m); |
|
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
311 PyDict_SetItemString(d, "pi", |
|
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
312 v = PyFloat_FromDouble(atan(1.0) * 4.0)); |
|
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
313 Py_DECREF(v); |
|
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
314 PyDict_SetItemString(d, "e", v = PyFloat_FromDouble(exp(1.0))); |
|
97ec9dfbb2b6
Renamed Grandly
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4600
diff
changeset
|
315 Py_DECREF(v); |
| 3334 | 316 } |
