Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 1 | /* Complex math module */ |
| 2 | |
| 3 | /* much code borrowed from mathmodule.c */ |
| 4 | |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 5 | #include "Python.h" |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 7 | #ifdef i860 |
| 8 | /* Cray APP has bogus definition of HUGE_VAL in <math.h> */ |
| 9 | #undef HUGE_VAL |
| 10 | #endif |
| 11 | |
| 12 | #ifdef HUGE_VAL |
| 13 | #define CHECK(x) if (errno != 0) ; \ |
| 14 | else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \ |
| 15 | else errno = ERANGE |
| 16 | #else |
| 17 | #define CHECK(x) /* Don't know how to check */ |
| 18 | #endif |
| 19 | |
| 20 | #ifndef M_PI |
| 21 | #define M_PI (3.141592653589793239) |
| 22 | #endif |
| 23 | |
| 24 | /* First, the C functions that do the real work */ |
| 25 | |
| 26 | /* constants */ |
Tim Peters | 8cb82bd | 2001-02-20 20:36:38 +0000 | [diff] [blame^] | 27 | static Py_complex c_one = {1., 0.}; |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 28 | static Py_complex c_half = {0.5, 0.}; |
| 29 | static Py_complex c_i = {0., 1.}; |
Tim Peters | 8cb82bd | 2001-02-20 20:36:38 +0000 | [diff] [blame^] | 30 | static Py_complex c_halfi = {0., 0.5}; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 31 | |
| 32 | /* forward declarations */ |
Thomas Wouters | eb61b6e | 2000-07-24 11:17:40 +0000 | [diff] [blame] | 33 | staticforward Py_complex c_log(Py_complex); |
| 34 | staticforward Py_complex c_prodi(Py_complex); |
| 35 | staticforward Py_complex c_sqrt(Py_complex); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 36 | |
| 37 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 38 | static Py_complex |
| 39 | c_acos(Py_complex x) |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 40 | { |
| 41 | return c_neg(c_prodi(c_log(c_sum(x,c_prod(c_i, |
Tim Peters | 8cb82bd | 2001-02-20 20:36:38 +0000 | [diff] [blame^] | 42 | c_sqrt(c_diff(c_one,c_prod(x,x)))))))); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 45 | static char c_acos_doc[] = |
| 46 | "acos(x)\n" |
| 47 | "\n" |
| 48 | "Return the arc cosine of x."; |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 49 | |
| 50 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 51 | static Py_complex |
| 52 | c_acosh(Py_complex x) |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 53 | { |
Guido van Rossum | f385c5e | 2000-06-30 02:29:22 +0000 | [diff] [blame] | 54 | Py_complex z; |
| 55 | z = c_sqrt(c_half); |
Tim Peters | 8cb82bd | 2001-02-20 20:36:38 +0000 | [diff] [blame^] | 56 | z = c_log(c_prod(z, c_sum(c_sqrt(c_sum(x,c_one)), |
| 57 | c_sqrt(c_diff(x,c_one))))); |
Guido van Rossum | f385c5e | 2000-06-30 02:29:22 +0000 | [diff] [blame] | 58 | return c_sum(z, z); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 61 | static char c_acosh_doc[] = |
| 62 | "acosh(x)\n" |
| 63 | "\n" |
| 64 | "Return the hyperbolic arccosine of x."; |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 65 | |
| 66 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 67 | static Py_complex |
| 68 | c_asin(Py_complex x) |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 69 | { |
Guido van Rossum | f385c5e | 2000-06-30 02:29:22 +0000 | [diff] [blame] | 70 | Py_complex z; |
| 71 | z = c_sqrt(c_half); |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 72 | z = c_log(c_prod(z, c_sum(c_sqrt(c_sum(x, c_i)), |
| 73 | c_sqrt(c_diff(x, c_i))))); |
Guido van Rossum | f385c5e | 2000-06-30 02:29:22 +0000 | [diff] [blame] | 74 | return c_sum(z, z); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 77 | static char c_asin_doc[] = |
| 78 | "asin(x)\n" |
| 79 | "\n" |
| 80 | "Return the arc sine of x."; |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 81 | |
| 82 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 83 | static Py_complex |
| 84 | c_asinh(Py_complex x) |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 85 | { |
Guido van Rossum | 11a5071 | 1999-01-14 19:11:11 +0000 | [diff] [blame] | 86 | /* Break up long expression for WATCOM */ |
| 87 | Py_complex z; |
Tim Peters | 8cb82bd | 2001-02-20 20:36:38 +0000 | [diff] [blame^] | 88 | z = c_sum(c_one, c_prod(x, x)); |
Guido van Rossum | f385c5e | 2000-06-30 02:29:22 +0000 | [diff] [blame] | 89 | return c_log(c_sum(c_sqrt(z), x)); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 92 | static char c_asinh_doc[] = |
| 93 | "asinh(x)\n" |
| 94 | "\n" |
| 95 | "Return the hyperbolic arc sine of x."; |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 96 | |
| 97 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 98 | static Py_complex |
| 99 | c_atan(Py_complex x) |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 100 | { |
Tim Peters | 8cb82bd | 2001-02-20 20:36:38 +0000 | [diff] [blame^] | 101 | return c_prod(c_halfi,c_log(c_quot(c_sum(c_i,x),c_diff(c_i,x)))); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 104 | static char c_atan_doc[] = |
| 105 | "atan(x)\n" |
| 106 | "\n" |
| 107 | "Return the arc tangent of x."; |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 108 | |
| 109 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 110 | static Py_complex |
| 111 | c_atanh(Py_complex x) |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 112 | { |
Tim Peters | 8cb82bd | 2001-02-20 20:36:38 +0000 | [diff] [blame^] | 113 | return c_prod(c_half,c_log(c_quot(c_sum(c_one,x),c_diff(c_one,x)))); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 116 | static char c_atanh_doc[] = |
| 117 | "atanh(x)\n" |
| 118 | "\n" |
| 119 | "Return the hyperbolic arc tangent of x."; |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 120 | |
| 121 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 122 | static Py_complex |
| 123 | c_cos(Py_complex x) |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 124 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 125 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 126 | r.real = cos(x.real)*cosh(x.imag); |
| 127 | r.imag = -sin(x.real)*sinh(x.imag); |
| 128 | return r; |
| 129 | } |
| 130 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 131 | static char c_cos_doc[] = |
| 132 | "cos(x)\n" |
| 133 | "n" |
| 134 | "Return the cosine of x."; |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 135 | |
| 136 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 137 | static Py_complex |
| 138 | c_cosh(Py_complex x) |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 139 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 140 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 141 | r.real = cos(x.imag)*cosh(x.real); |
| 142 | r.imag = sin(x.imag)*sinh(x.real); |
| 143 | return r; |
| 144 | } |
| 145 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 146 | static char c_cosh_doc[] = |
| 147 | "cosh(x)\n" |
| 148 | "n" |
| 149 | "Return the hyperbolic cosine of x."; |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 150 | |
| 151 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 152 | static Py_complex |
| 153 | c_exp(Py_complex x) |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 154 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 155 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 156 | double l = exp(x.real); |
| 157 | r.real = l*cos(x.imag); |
| 158 | r.imag = l*sin(x.imag); |
| 159 | return r; |
| 160 | } |
| 161 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 162 | static char c_exp_doc[] = |
| 163 | "exp(x)\n" |
| 164 | "\n" |
| 165 | "Return the exponential value e**x."; |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 166 | |
| 167 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 168 | static Py_complex |
| 169 | c_log(Py_complex x) |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 170 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 171 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 172 | double l = hypot(x.real,x.imag); |
| 173 | r.imag = atan2(x.imag, x.real); |
| 174 | r.real = log(l); |
| 175 | return r; |
| 176 | } |
| 177 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 178 | static char c_log_doc[] = |
| 179 | "log(x)\n" |
| 180 | "\n" |
| 181 | "Return the natural logarithm of x."; |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 182 | |
| 183 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 184 | static Py_complex |
| 185 | c_log10(Py_complex x) |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 186 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 187 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 188 | double l = hypot(x.real,x.imag); |
| 189 | r.imag = atan2(x.imag, x.real)/log(10.); |
| 190 | r.real = log10(l); |
| 191 | return r; |
| 192 | } |
| 193 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 194 | static char c_log10_doc[] = |
| 195 | "log10(x)\n" |
| 196 | "\n" |
| 197 | "Return the base-10 logarithm of x."; |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 198 | |
| 199 | |
| 200 | /* internal function not available from Python */ |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 201 | static Py_complex |
| 202 | c_prodi(Py_complex x) |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 203 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 204 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 205 | r.real = -x.imag; |
| 206 | r.imag = x.real; |
| 207 | return r; |
| 208 | } |
| 209 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 210 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 211 | static Py_complex |
| 212 | c_sin(Py_complex x) |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 213 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 214 | Py_complex r; |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 215 | r.real = sin(x.real) * cosh(x.imag); |
| 216 | r.imag = cos(x.real) * sinh(x.imag); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 217 | return r; |
| 218 | } |
| 219 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 220 | static char c_sin_doc[] = |
| 221 | "sin(x)\n" |
| 222 | "\n" |
| 223 | "Return the sine of x."; |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 224 | |
| 225 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 226 | static Py_complex |
| 227 | c_sinh(Py_complex x) |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 228 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 229 | Py_complex r; |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 230 | r.real = cos(x.imag) * sinh(x.real); |
| 231 | r.imag = sin(x.imag) * cosh(x.real); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 232 | return r; |
| 233 | } |
| 234 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 235 | static char c_sinh_doc[] = |
| 236 | "sinh(x)\n" |
| 237 | "\n" |
| 238 | "Return the hyperbolic sine of x."; |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 239 | |
| 240 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 241 | static Py_complex |
| 242 | c_sqrt(Py_complex x) |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 243 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 244 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 245 | double s,d; |
| 246 | if (x.real == 0. && x.imag == 0.) |
| 247 | r = x; |
| 248 | else { |
| 249 | s = sqrt(0.5*(fabs(x.real) + hypot(x.real,x.imag))); |
| 250 | d = 0.5*x.imag/s; |
| 251 | if (x.real > 0.) { |
| 252 | r.real = s; |
| 253 | r.imag = d; |
| 254 | } |
| 255 | else if (x.imag >= 0.) { |
| 256 | r.real = d; |
| 257 | r.imag = s; |
| 258 | } |
| 259 | else { |
| 260 | r.real = -d; |
| 261 | r.imag = -s; |
| 262 | } |
| 263 | } |
| 264 | return r; |
| 265 | } |
| 266 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 267 | static char c_sqrt_doc[] = |
| 268 | "sqrt(x)\n" |
| 269 | "\n" |
| 270 | "Return the square root of x."; |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 271 | |
| 272 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 273 | static Py_complex |
| 274 | c_tan(Py_complex x) |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 275 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 276 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 277 | double sr,cr,shi,chi; |
| 278 | double rs,is,rc,ic; |
| 279 | double d; |
| 280 | sr = sin(x.real); |
| 281 | cr = cos(x.real); |
| 282 | shi = sinh(x.imag); |
| 283 | chi = cosh(x.imag); |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 284 | rs = sr * chi; |
| 285 | is = cr * shi; |
| 286 | rc = cr * chi; |
| 287 | ic = -sr * shi; |
| 288 | d = rc*rc + ic * ic; |
| 289 | r.real = (rs*rc + is*ic) / d; |
| 290 | r.imag = (is*rc - rs*ic) / d; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 291 | return r; |
| 292 | } |
| 293 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 294 | static char c_tan_doc[] = |
| 295 | "tan(x)\n" |
| 296 | "\n" |
| 297 | "Return the tangent of x."; |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 298 | |
| 299 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 300 | static Py_complex |
| 301 | c_tanh(Py_complex x) |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 302 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 303 | Py_complex r; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 304 | double si,ci,shr,chr; |
| 305 | double rs,is,rc,ic; |
| 306 | double d; |
| 307 | si = sin(x.imag); |
| 308 | ci = cos(x.imag); |
| 309 | shr = sinh(x.real); |
| 310 | chr = cosh(x.real); |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 311 | rs = ci * shr; |
| 312 | is = si * chr; |
| 313 | rc = ci * chr; |
| 314 | ic = si * shr; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 315 | d = rc*rc + ic*ic; |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 316 | r.real = (rs*rc + is*ic) / d; |
| 317 | r.imag = (is*rc - rs*ic) / d; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 318 | return r; |
| 319 | } |
| 320 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 321 | static char c_tanh_doc[] = |
| 322 | "tanh(x)\n" |
| 323 | "\n" |
| 324 | "Return the hyperbolic tangent of x."; |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 325 | |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 326 | |
| 327 | /* And now the glue to make them available from Python: */ |
| 328 | |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 329 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 330 | math_error(void) |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 331 | { |
| 332 | if (errno == EDOM) |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 333 | PyErr_SetString(PyExc_ValueError, "math domain error"); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 334 | else if (errno == ERANGE) |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 335 | PyErr_SetString(PyExc_OverflowError, "math range error"); |
| 336 | else /* Unexpected math error */ |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 337 | PyErr_SetFromErrno(PyExc_ValueError); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 338 | return NULL; |
| 339 | } |
| 340 | |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 341 | static PyObject * |
Peter Schneider-Kamp | f1ca898 | 2000-07-10 09:31:34 +0000 | [diff] [blame] | 342 | math_1(PyObject *args, Py_complex (*func)(Py_complex)) |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 343 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 344 | Py_complex x; |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 345 | if (!PyArg_ParseTuple(args, "D", &x)) |
| 346 | return NULL; |
| 347 | errno = 0; |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 348 | PyFPE_START_PROTECT("complex function", return 0) |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 349 | x = (*func)(x); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 350 | PyFPE_END_PROTECT(x) |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 351 | CHECK(x.real); |
| 352 | CHECK(x.imag); |
| 353 | if (errno != 0) |
| 354 | return math_error(); |
| 355 | else |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 356 | return PyComplex_FromCComplex(x); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | #define FUNC1(stubname, func) \ |
Peter Schneider-Kamp | f1ca898 | 2000-07-10 09:31:34 +0000 | [diff] [blame] | 360 | static PyObject * stubname(PyObject *self, PyObject *args) { \ |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 361 | return math_1(args, func); \ |
| 362 | } |
| 363 | |
| 364 | FUNC1(cmath_acos, c_acos) |
| 365 | FUNC1(cmath_acosh, c_acosh) |
| 366 | FUNC1(cmath_asin, c_asin) |
| 367 | FUNC1(cmath_asinh, c_asinh) |
| 368 | FUNC1(cmath_atan, c_atan) |
| 369 | FUNC1(cmath_atanh, c_atanh) |
| 370 | FUNC1(cmath_cos, c_cos) |
| 371 | FUNC1(cmath_cosh, c_cosh) |
| 372 | FUNC1(cmath_exp, c_exp) |
| 373 | FUNC1(cmath_log, c_log) |
| 374 | FUNC1(cmath_log10, c_log10) |
| 375 | FUNC1(cmath_sin, c_sin) |
| 376 | FUNC1(cmath_sinh, c_sinh) |
| 377 | FUNC1(cmath_sqrt, c_sqrt) |
| 378 | FUNC1(cmath_tan, c_tan) |
| 379 | FUNC1(cmath_tanh, c_tanh) |
| 380 | |
| 381 | |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 382 | static char module_doc[] = |
| 383 | "This module is always available. It provides access to mathematical\n" |
| 384 | "functions for complex numbers."; |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 385 | |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 386 | static PyMethodDef cmath_methods[] = { |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 387 | {"acos", cmath_acos, METH_VARARGS, c_acos_doc}, |
| 388 | {"acosh", cmath_acosh, METH_VARARGS, c_acosh_doc}, |
| 389 | {"asin", cmath_asin, METH_VARARGS, c_asin_doc}, |
| 390 | {"asinh", cmath_asinh, METH_VARARGS, c_asinh_doc}, |
| 391 | {"atan", cmath_atan, METH_VARARGS, c_atan_doc}, |
| 392 | {"atanh", cmath_atanh, METH_VARARGS, c_atanh_doc}, |
| 393 | {"cos", cmath_cos, METH_VARARGS, c_cos_doc}, |
| 394 | {"cosh", cmath_cosh, METH_VARARGS, c_cosh_doc}, |
| 395 | {"exp", cmath_exp, METH_VARARGS, c_exp_doc}, |
| 396 | {"log", cmath_log, METH_VARARGS, c_log_doc}, |
| 397 | {"log10", cmath_log10, METH_VARARGS, c_log10_doc}, |
| 398 | {"sin", cmath_sin, METH_VARARGS, c_sin_doc}, |
| 399 | {"sinh", cmath_sinh, METH_VARARGS, c_sinh_doc}, |
| 400 | {"sqrt", cmath_sqrt, METH_VARARGS, c_sqrt_doc}, |
| 401 | {"tan", cmath_tan, METH_VARARGS, c_tan_doc}, |
| 402 | {"tanh", cmath_tanh, METH_VARARGS, c_tanh_doc}, |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 403 | {NULL, NULL} /* sentinel */ |
| 404 | }; |
| 405 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 406 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 407 | initcmath(void) |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 408 | { |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 409 | PyObject *m, *d, *v; |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 410 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 411 | m = Py_InitModule3("cmath", cmath_methods, module_doc); |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 412 | d = PyModule_GetDict(m); |
| 413 | PyDict_SetItemString(d, "pi", |
| 414 | v = PyFloat_FromDouble(atan(1.0) * 4.0)); |
| 415 | Py_DECREF(v); |
| 416 | PyDict_SetItemString(d, "e", v = PyFloat_FromDouble(exp(1.0))); |
| 417 | Py_DECREF(v); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 418 | } |