pypy / lib_pypy / cmath.py

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243``` ```"""This module is always available. It provides access to mathematical functions for complex numbers.""" # Complex math module # much code borrowed from mathmodule.c import math from math import e, pi try: from __pypy__ import builtinify except ImportError: builtinify = lambda f: f # constants _one = complex(1., 0.) _half = complex(0.5, 0.) _i = complex(0., 1.) _halfi = complex(0., 0.5) # internal function not available from Python def _prodi(x): x = complex(x, 0) real = -x.imag imag = x.real return complex(real, imag) @builtinify def acos(x): """acos(x) Return the arc cosine of x.""" return -(_prodi(log((x+(_i*sqrt((_one-(x*x)))))))) @builtinify def acosh(x): """acosh(x) Return the hyperbolic arccosine of x.""" z = log(_sqrt_half*(sqrt(x+_one)+sqrt(x-_one))) return z+z @builtinify def asin(x): """asin(x) Return the arc sine of x.""" # -i * log[(sqrt(1-x**2) + i*x] squared = x*x sqrt_1_minus_x_sq = sqrt(_one-squared) return -(_prodi(log((sqrt_1_minus_x_sq+_prodi(x))))) @builtinify def asinh(x): """asinh(x) Return the hyperbolic arc sine of x.""" z = log((_sqrt_half * (sqrt(x+_i)+sqrt((x-_i))) )) return z+z @builtinify def atan(x): """atan(x) Return the arc tangent of x.""" return _halfi*log(((_i+x)/(_i-x))) @builtinify def atanh(x): """atanh(x) Return the hyperbolic arc tangent of x.""" return _half*log((_one+x)/(_one-x)) @builtinify def cos(x): """cos(x) Return the cosine of x.""" x = complex(x, 0) real = math.cos(x.real) * math.cosh(x.imag) imag = -math.sin(x.real) * math.sinh(x.imag) return complex(real, imag) @builtinify def cosh(x): """cosh(x) Return the hyperbolic cosine of x.""" x = complex(x, 0) real = math.cos(x.imag) * math.cosh(x.real) imag = math.sin(x.imag) * math.sinh(x.real) return complex(real, imag) @builtinify def exp(x): """exp(x) Return the exponential value e**x.""" x = complex(x, 0) l = math.exp(x.real) real = l * math.cos(x.imag) imag = l * math.sin(x.imag) return complex(real, imag) @builtinify def log(x, base=None): """log(x) Return the natural logarithm of x.""" if base is not None: return log(x) / log(base) x = complex(x, 0) l = math.hypot(x.real,x.imag) imag = math.atan2(x.imag, x.real) real = math.log(l) return complex(real, imag) @builtinify def log10(x): """log10(x) Return the base-10 logarithm of x.""" x = complex(x, 0) l = math.hypot(x.real, x.imag) imag = math.atan2(x.imag, x.real)/math.log(10.) real = math.log10(l) return complex(real, imag) @builtinify def sin(x): """sin(x) Return the sine of x.""" x = complex(x, 0) real = math.sin(x.real) * math.cosh(x.imag) imag = math.cos(x.real) * math.sinh(x.imag) return complex(real, imag) @builtinify def sinh(x): """sinh(x) Return the hyperbolic sine of x.""" x = complex(x, 0) real = math.cos(x.imag) * math.sinh(x.real) imag = math.sin(x.imag) * math.cosh(x.real) return complex(real, imag) @builtinify def sqrt(x): """sqrt(x) Return the square root of x.""" x = complex(x, 0) if x.real == 0. and x.imag == 0.: real, imag = 0, 0 else: s = math.sqrt(0.5*(math.fabs(x.real) + math.hypot(x.real,x.imag))) d = 0.5*x.imag/s if x.real > 0.: real = s imag = d elif x.imag >= 0.: real = d imag = s else: real = -d imag = -s return complex(real, imag) _sqrt_half = sqrt(_half) @builtinify def tan(x): """tan(x) Return the tangent of x.""" x = complex(x, 0) sr = math.sin(x.real) cr = math.cos(x.real) shi = math.sinh(x.imag) chi = math.cosh(x.imag) rs = sr * chi is_ = cr * shi rc = cr * chi ic = -sr * shi d = rc*rc + ic * ic real = (rs*rc + is_*ic) / d imag = (is_*rc - rs*ic) / d return complex(real, imag) @builtinify def tanh(x): """tanh(x) Return the hyperbolic tangent of x.""" x = complex(x, 0) si = math.sin(x.imag) ci = math.cos(x.imag) shr = math.sinh(x.real) chr = math.cosh(x.real) rs = ci * shr is_ = si * chr rc = ci * chr ic = si * shr d = rc*rc + ic*ic real = (rs*rc + is_*ic) / d imag = (is_*rc - rs*ic) / d return complex(real, imag) ```
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.