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 */ |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 20 | static Py_complex c_log(Py_complex); |
| 21 | static Py_complex c_prodi(Py_complex); |
| 22 | static 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 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 32 | PyDoc_STRVAR(c_acos_doc, |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 33 | "acos(x)\n" |
| 34 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 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 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 48 | PyDoc_STRVAR(c_acosh_doc, |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 49 | "acosh(x)\n" |
| 50 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 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 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 65 | PyDoc_STRVAR(c_asin_doc, |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 66 | "asin(x)\n" |
| 67 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 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 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 81 | PyDoc_STRVAR(c_asinh_doc, |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 82 | "asinh(x)\n" |
| 83 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 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 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 93 | PyDoc_STRVAR(c_atan_doc, |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 94 | "atan(x)\n" |
| 95 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 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 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 105 | PyDoc_STRVAR(c_atanh_doc, |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 106 | "atanh(x)\n" |
| 107 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 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 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 120 | PyDoc_STRVAR(c_cos_doc, |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 121 | "cos(x)\n" |
| 122 | "n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 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 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 135 | PyDoc_STRVAR(c_cosh_doc, |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 136 | "cosh(x)\n" |
| 137 | "n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 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 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 151 | PyDoc_STRVAR(c_exp_doc, |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 152 | "exp(x)\n" |
| 153 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 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 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 167 | PyDoc_STRVAR(c_log_doc, |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 168 | "log(x)\n" |
| 169 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 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 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 183 | PyDoc_STRVAR(c_log10_doc, |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 184 | "log10(x)\n" |
| 185 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 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 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 209 | PyDoc_STRVAR(c_sin_doc, |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 210 | "sin(x)\n" |
| 211 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 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 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 224 | PyDoc_STRVAR(c_sinh_doc, |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 225 | "sinh(x)\n" |
| 226 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 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 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 256 | PyDoc_STRVAR(c_sqrt_doc, |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 257 | "sqrt(x)\n" |
| 258 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 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 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 283 | PyDoc_STRVAR(c_tan_doc, |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 284 | "tan(x)\n" |
| 285 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 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 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 310 | PyDoc_STRVAR(c_tanh_doc, |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 311 | "tanh(x)\n" |
| 312 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 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 | dc5a508 | 2002-03-09 04:58:24 +0000 | [diff] [blame] | 340 | Py_ADJUST_ERANGE2(x.real, x.imag); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 341 | if (errno != 0) |
| 342 | return math_error(); |
| 343 | else |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 344 | return PyComplex_FromCComplex(x); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | #define FUNC1(stubname, func) \ |
Peter Schneider-Kamp | f1ca898 | 2000-07-10 09:31:34 +0000 | [diff] [blame] | 348 | static PyObject * stubname(PyObject *self, PyObject *args) { \ |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 349 | return math_1(args, func); \ |
| 350 | } |
| 351 | |
| 352 | FUNC1(cmath_acos, c_acos) |
| 353 | FUNC1(cmath_acosh, c_acosh) |
| 354 | FUNC1(cmath_asin, c_asin) |
| 355 | FUNC1(cmath_asinh, c_asinh) |
| 356 | FUNC1(cmath_atan, c_atan) |
| 357 | FUNC1(cmath_atanh, c_atanh) |
| 358 | FUNC1(cmath_cos, c_cos) |
| 359 | FUNC1(cmath_cosh, c_cosh) |
| 360 | FUNC1(cmath_exp, c_exp) |
| 361 | FUNC1(cmath_log, c_log) |
| 362 | FUNC1(cmath_log10, c_log10) |
| 363 | FUNC1(cmath_sin, c_sin) |
| 364 | FUNC1(cmath_sinh, c_sinh) |
| 365 | FUNC1(cmath_sqrt, c_sqrt) |
| 366 | FUNC1(cmath_tan, c_tan) |
| 367 | FUNC1(cmath_tanh, c_tanh) |
| 368 | |
| 369 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 370 | PyDoc_STRVAR(module_doc, |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 371 | "This module is always available. It provides access to mathematical\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 372 | "functions for complex numbers."); |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 373 | |
Roger E. Masse | 24070ca | 1996-12-09 22:59:53 +0000 | [diff] [blame] | 374 | static PyMethodDef cmath_methods[] = { |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 375 | {"acos", cmath_acos, METH_VARARGS, c_acos_doc}, |
| 376 | {"acosh", cmath_acosh, METH_VARARGS, c_acosh_doc}, |
| 377 | {"asin", cmath_asin, METH_VARARGS, c_asin_doc}, |
| 378 | {"asinh", cmath_asinh, METH_VARARGS, c_asinh_doc}, |
| 379 | {"atan", cmath_atan, METH_VARARGS, c_atan_doc}, |
| 380 | {"atanh", cmath_atanh, METH_VARARGS, c_atanh_doc}, |
| 381 | {"cos", cmath_cos, METH_VARARGS, c_cos_doc}, |
| 382 | {"cosh", cmath_cosh, METH_VARARGS, c_cosh_doc}, |
| 383 | {"exp", cmath_exp, METH_VARARGS, c_exp_doc}, |
| 384 | {"log", cmath_log, METH_VARARGS, c_log_doc}, |
| 385 | {"log10", cmath_log10, METH_VARARGS, c_log10_doc}, |
| 386 | {"sin", cmath_sin, METH_VARARGS, c_sin_doc}, |
| 387 | {"sinh", cmath_sinh, METH_VARARGS, c_sinh_doc}, |
| 388 | {"sqrt", cmath_sqrt, METH_VARARGS, c_sqrt_doc}, |
| 389 | {"tan", cmath_tan, METH_VARARGS, c_tan_doc}, |
| 390 | {"tanh", cmath_tanh, METH_VARARGS, c_tanh_doc}, |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 391 | {NULL, NULL} /* sentinel */ |
| 392 | }; |
| 393 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 394 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 395 | initcmath(void) |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 396 | { |
Fred Drake | f4e3484 | 2002-04-01 03:45:06 +0000 | [diff] [blame] | 397 | PyObject *m; |
Tim Peters | 14e2640 | 2001-02-20 20:15:19 +0000 | [diff] [blame] | 398 | |
Guido van Rossum | c6e2290 | 1998-12-04 19:26:43 +0000 | [diff] [blame] | 399 | m = Py_InitModule3("cmath", cmath_methods, module_doc); |
Fred Drake | f4e3484 | 2002-04-01 03:45:06 +0000 | [diff] [blame] | 400 | |
| 401 | PyModule_AddObject(m, "pi", |
| 402 | PyFloat_FromDouble(atan(1.0) * 4.0)); |
| 403 | PyModule_AddObject(m, "e", PyFloat_FromDouble(exp(1.0))); |
Guido van Rossum | 71aa32f | 1996-01-12 01:34:57 +0000 | [diff] [blame] | 404 | } |