| 1 |
import math |
| 2 |
from rpython.rlib.objectmodel import specialize |
| 3 |
from rpython.tool.sourcetools import func_with_new_name |
| 4 |
from pypy.interpreter.error import oefmt |
| 5 |
from pypy.interpreter.gateway import unwrap_spec |
| 6 |
from pypy.module.cmath.moduledef import names_and_docstrings |
| 7 |
from rpython.rlib import rcomplex, rfloat |
| 8 |
|
| 9 |
pi = math.pi |
| 10 |
tau = math.pi * 2.0 |
| 11 |
e = math.e |
| 12 |
inf = float('inf') |
| 13 |
nan = float('nan') |
| 14 |
infj = complex(0.0, inf) |
| 15 |
nanj = complex(0.0, nan) |
| 16 |
|
| 17 |
|
| 18 |
@specialize.arg(0) |
| 19 |
def call_c_func(c_func, space, x, y): |
| 20 |
try: |
| 21 |
result = c_func(x, y) |
| 22 |
except ValueError: |
| 23 |
raise oefmt(space.w_ValueError, "math domain error") |
| 24 |
except OverflowError: |
| 25 |
raise oefmt(space.w_OverflowError, "math range error") |
| 26 |
return result |
| 27 |
|
| 28 |
|
| 29 |
def unaryfn(c_func): |
| 30 |
def wrapper(space, w_z): |
| 31 |
x, y = space.unpackcomplex(w_z) |
| 32 |
resx, resy = call_c_func(c_func, space, x, y) |
| 33 |
return space.newcomplex(resx, resy) |
| 34 |
|
| 35 |
name = c_func.func_name |
| 36 |
assert name.startswith('c_') |
| 37 |
wrapper.func_doc = names_and_docstrings[name[2:]] |
| 38 |
fnname = 'wrapped_' + name[2:] |
| 39 |
globals()[fnname] = func_with_new_name(wrapper, fnname) |
| 40 |
return c_func |
| 41 |
|
| 42 |
|
| 43 |
def c_neg(x, y): |
| 44 |
return rcomplex.c_neg(x,y) |
| 45 |
|
| 46 |
|
| 47 |
@unaryfn |
| 48 |
def c_sqrt(x, y): |
| 49 |
return rcomplex.c_sqrt(x,y) |
| 50 |
|
| 51 |
@unaryfn |
| 52 |
def c_acos(x, y): |
| 53 |
return rcomplex.c_acos(x,y) |
| 54 |
|
| 55 |
@unaryfn |
| 56 |
def c_acosh(x, y): |
| 57 |
return rcomplex.c_acosh(x,y) |
| 58 |
|
| 59 |
@unaryfn |
| 60 |
def c_asin(x, y): |
| 61 |
return rcomplex.c_asin(x,y) |
| 62 |
|
| 63 |
@unaryfn |
| 64 |
def c_asinh(x, y): |
| 65 |
return rcomplex.c_asinh(x,y) |
| 66 |
|
| 67 |
@unaryfn |
| 68 |
def c_atan(x, y): |
| 69 |
return rcomplex.c_atan(x,y) |
| 70 |
|
| 71 |
@unaryfn |
| 72 |
def c_atanh(x, y): |
| 73 |
return rcomplex.c_atanh(x,y) |
| 74 |
|
| 75 |
@unaryfn |
| 76 |
def c_log(x, y): |
| 77 |
return rcomplex.c_log(x,y) |
| 78 |
|
| 79 |
_inner_wrapped_log = wrapped_log |
| 80 |
|
| 81 |
def wrapped_log(space, w_z, w_base=None): |
| 82 |
w_logz = _inner_wrapped_log(space, w_z) |
| 83 |
if w_base is not None: |
| 84 |
w_logbase = _inner_wrapped_log(space, w_base) |
| 85 |
return space.truediv(w_logz, w_logbase) |
| 86 |
else: |
| 87 |
return w_logz |
| 88 |
wrapped_log.func_doc = _inner_wrapped_log.func_doc |
| 89 |
|
| 90 |
|
| 91 |
@unaryfn |
| 92 |
def c_log10(x, y): |
| 93 |
return rcomplex.c_log10(x,y) |
| 94 |
|
| 95 |
@unaryfn |
| 96 |
def c_exp(x, y): |
| 97 |
return rcomplex.c_exp(x,y) |
| 98 |
|
| 99 |
@unaryfn |
| 100 |
def c_cosh(x, y): |
| 101 |
return rcomplex.c_cosh(x,y) |
| 102 |
|
| 103 |
@unaryfn |
| 104 |
def c_sinh(x, y): |
| 105 |
return rcomplex.c_sinh(x,y) |
| 106 |
|
| 107 |
@unaryfn |
| 108 |
def c_tanh(x, y): |
| 109 |
return rcomplex.c_tanh(x,y) |
| 110 |
|
| 111 |
@unaryfn |
| 112 |
def c_cos(x, y): |
| 113 |
return rcomplex.c_cos(x,y) |
| 114 |
|
| 115 |
@unaryfn |
| 116 |
def c_sin(x, y): |
| 117 |
return rcomplex.c_sin(x,y) |
| 118 |
|
| 119 |
@unaryfn |
| 120 |
def c_tan(x, y): |
| 121 |
return rcomplex.c_tan(x,y) |
| 122 |
|
| 123 |
def c_rect(r, phi): |
| 124 |
return rcomplex.c_rect(r,phi) |
| 125 |
|
| 126 |
def wrapped_rect(space, w_x, w_y): |
| 127 |
x = space.float_w(w_x) |
| 128 |
y = space.float_w(w_y) |
| 129 |
resx, resy = call_c_func(c_rect, space, x, y) |
| 130 |
return space.newcomplex(resx, resy) |
| 131 |
wrapped_rect.func_doc = names_and_docstrings['rect'] |
| 132 |
|
| 133 |
|
| 134 |
def c_phase(x, y): |
| 135 |
return rcomplex.c_phase(x,y) |
| 136 |
|
| 137 |
def wrapped_phase(space, w_z): |
| 138 |
x, y = space.unpackcomplex(w_z) |
| 139 |
result = call_c_func(c_phase, space, x, y) |
| 140 |
return space.newfloat(result) |
| 141 |
wrapped_phase.func_doc = names_and_docstrings['phase'] |
| 142 |
|
| 143 |
|
| 144 |
def c_abs(x, y): |
| 145 |
return rcomplex.c_abs(x,y) |
| 146 |
|
| 147 |
def c_polar(x, y): |
| 148 |
return rcomplex.c_polar(x,y) |
| 149 |
|
| 150 |
def wrapped_polar(space, w_z): |
| 151 |
x, y = space.unpackcomplex(w_z) |
| 152 |
resx, resy = call_c_func(c_polar, space, x, y) |
| 153 |
return space.newtuple2(space.newfloat(resx), space.newfloat(resy)) |
| 154 |
wrapped_polar.func_doc = names_and_docstrings['polar'] |
| 155 |
|
| 156 |
|
| 157 |
def c_isinf(x, y): |
| 158 |
return rcomplex.c_isinf(x,y) |
| 159 |
|
| 160 |
def wrapped_isinf(space, w_z): |
| 161 |
x, y = space.unpackcomplex(w_z) |
| 162 |
res = c_isinf(x, y) |
| 163 |
return space.newbool(res) |
| 164 |
wrapped_isinf.func_doc = names_and_docstrings['isinf'] |
| 165 |
|
| 166 |
|
| 167 |
def c_isnan(x, y): |
| 168 |
return rcomplex.c_isnan(x,y) |
| 169 |
|
| 170 |
def wrapped_isnan(space, w_z): |
| 171 |
x, y = space.unpackcomplex(w_z) |
| 172 |
res = c_isnan(x, y) |
| 173 |
return space.newbool(res) |
| 174 |
wrapped_isnan.func_doc = names_and_docstrings['isnan'] |
| 175 |
|
| 176 |
def c_isfinite(x, y): |
| 177 |
return rcomplex.c_isfinite(x, y) |
| 178 |
|
| 179 |
def wrapped_isfinite(space, w_z): |
| 180 |
x, y = space.unpackcomplex(w_z) |
| 181 |
res = c_isfinite(x, y) |
| 182 |
return space.newbool(res) |
| 183 |
wrapped_isfinite.func_doc = names_and_docstrings['isfinite'] |
| 184 |
|
| 185 |
|
| 186 |
@unwrap_spec(rel_tol=float, abs_tol=float) |
| 187 |
def isclose(space, w_a, w_b, __kwonly__, rel_tol=1e-09, abs_tol=0.0): |
| 188 |
"""isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0) -> bool |
| 189 |
|
| 190 |
Determine whether two complex numbers are close in value. |
| 191 |
|
| 192 |
rel_tol |
| 193 |
maximum difference for being considered "close", relative to the |
| 194 |
magnitude of the input values |
| 195 |
abs_tol |
| 196 |
maximum difference for being considered "close", regardless of the |
| 197 |
magnitude of the input values |
| 198 |
|
| 199 |
Return True if a is close in value to b, and False otherwise. |
| 200 |
|
| 201 |
For the values to be considered close, the difference between them |
| 202 |
must be smaller than at least one of the tolerances. |
| 203 |
|
| 204 |
-inf, inf and NaN behave similarly to the IEEE 754 Standard. That |
| 205 |
is, NaN is not close to anything, even itself. inf and -inf are |
| 206 |
only close to themselves.""" |
| 207 |
ax, ay = space.unpackcomplex(w_a) |
| 208 |
bx, by = space.unpackcomplex(w_b) |
| 209 |
|
| 210 |
|
| 211 |
if rel_tol < 0.0 or abs_tol < 0.0: |
| 212 |
raise oefmt(space.w_ValueError, "tolerances must be non-negative") |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
if ax == bx and ay == by: |
| 217 |
return space.w_True |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
if (math.isinf(ax) or math.isinf(ay) or |
| 225 |
math.isinf(bx) or math.isinf(by)): |
| 226 |
return space.w_False |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
diff = c_abs(bx - ax, by - ay) |
| 231 |
result = ((diff <= rel_tol * c_abs(bx, by) or |
| 232 |
diff <= rel_tol * c_abs(ax, ay)) or |
| 233 |
diff <= abs_tol) |
| 234 |
return space.newbool(result) |