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