PageRenderTime 24ms CodeModel.GetById 13ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/pypy/module/cmath/interp_cmath.py

https://bitbucket.org/pypy/pypy/
Python | 168 lines | 125 code | 42 blank | 1 comment | 2 complexity | 046413c77ea93c400bdc55b1a161c6cb MD5 | raw file
  1import math
  2from rpython.rlib.objectmodel import specialize
  3from rpython.tool.sourcetools import func_with_new_name
  4from pypy.interpreter.error import oefmt
  5from pypy.module.cmath import names_and_docstrings
  6from rpython.rlib import rcomplex
  7
  8pi = math.pi
  9e  = math.e
 10
 11
 12@specialize.arg(0)
 13def call_c_func(c_func, space, x, y):
 14    try:
 15        result = c_func(x, y)
 16    except ValueError:
 17        raise oefmt(space.w_ValueError, "math domain error")
 18    except OverflowError:
 19        raise oefmt(space.w_OverflowError, "math range error")
 20    return result
 21
 22
 23def unaryfn(c_func):
 24    def wrapper(space, w_z):
 25        x, y = space.unpackcomplex(w_z)
 26        resx, resy = call_c_func(c_func, space, x, y)
 27        return space.newcomplex(resx, resy)
 28    #
 29    name = c_func.func_name
 30    assert name.startswith('c_')
 31    wrapper.func_doc = names_and_docstrings[name[2:]]
 32    fnname = 'wrapped_' + name[2:]
 33    globals()[fnname] = func_with_new_name(wrapper, fnname)
 34    return c_func
 35
 36
 37def c_neg(x, y):
 38    return rcomplex.c_neg(x,y)
 39
 40
 41@unaryfn
 42def c_sqrt(x, y):
 43    return rcomplex.c_sqrt(x,y)
 44
 45@unaryfn
 46def c_acos(x, y):
 47    return rcomplex.c_acos(x,y)
 48
 49@unaryfn
 50def c_acosh(x, y):
 51    return rcomplex.c_acosh(x,y)
 52
 53@unaryfn
 54def c_asin(x, y):
 55    return rcomplex.c_asin(x,y)
 56
 57@unaryfn
 58def c_asinh(x, y):
 59    return rcomplex.c_asinh(x,y)
 60
 61@unaryfn
 62def c_atan(x, y):
 63    return rcomplex.c_atan(x,y)
 64
 65@unaryfn
 66def c_atanh(x, y):
 67    return rcomplex.c_atanh(x,y)
 68
 69@unaryfn
 70def c_log(x, y):
 71    return rcomplex.c_log(x,y)
 72
 73_inner_wrapped_log = wrapped_log
 74
 75def wrapped_log(space, w_z, w_base=None):
 76    w_logz = _inner_wrapped_log(space, w_z)
 77    if w_base is not None:
 78        w_logbase = _inner_wrapped_log(space, w_base)
 79        return space.truediv(w_logz, w_logbase)
 80    else:
 81        return w_logz
 82wrapped_log.func_doc = _inner_wrapped_log.func_doc
 83
 84
 85@unaryfn
 86def c_log10(x, y):
 87    return rcomplex.c_log10(x,y)
 88
 89@unaryfn
 90def c_exp(x, y):
 91    return rcomplex.c_exp(x,y)
 92
 93@unaryfn
 94def c_cosh(x, y):
 95    return rcomplex.c_cosh(x,y)
 96
 97@unaryfn
 98def c_sinh(x, y):
 99    return rcomplex.c_sinh(x,y)
100
101@unaryfn
102def c_tanh(x, y):
103    return rcomplex.c_tanh(x,y)
104
105@unaryfn
106def c_cos(x, y):
107    return rcomplex.c_cos(x,y)
108
109@unaryfn
110def c_sin(x, y):
111    return rcomplex.c_sin(x,y)
112
113@unaryfn
114def c_tan(x, y):
115    return rcomplex.c_tan(x,y)
116
117def c_rect(r, phi):
118    return rcomplex.c_rect(r,phi)
119
120def wrapped_rect(space, w_x, w_y):
121    x = space.float_w(w_x)
122    y = space.float_w(w_y)
123    resx, resy = call_c_func(c_rect, space, x, y)
124    return space.newcomplex(resx, resy)
125wrapped_rect.func_doc = names_and_docstrings['rect']
126
127
128def c_phase(x, y):
129    return rcomplex.c_phase(x,y)
130
131def wrapped_phase(space, w_z):
132    x, y = space.unpackcomplex(w_z)
133    result = call_c_func(c_phase, space, x, y)
134    return space.newfloat(result)
135wrapped_phase.func_doc = names_and_docstrings['phase']
136
137
138def c_abs(x, y):
139    return rcomplex.c_abs(x,y)
140
141def c_polar(x, y):
142    return rcomplex.c_polar(x,y)
143
144def wrapped_polar(space, w_z):
145    x, y = space.unpackcomplex(w_z)
146    resx, resy = call_c_func(c_polar, space, x, y)
147    return space.newtuple([space.newfloat(resx), space.newfloat(resy)])
148wrapped_polar.func_doc = names_and_docstrings['polar']
149
150
151def c_isinf(x, y):
152    return rcomplex.c_isinf(x,y)
153
154def wrapped_isinf(space, w_z):
155    x, y = space.unpackcomplex(w_z)
156    res = c_isinf(x, y)
157    return space.newbool(res)
158wrapped_isinf.func_doc = names_and_docstrings['isinf']
159
160
161def c_isnan(x, y):
162    return rcomplex.c_isnan(x,y)
163
164def wrapped_isnan(space, w_z):
165    x, y = space.unpackcomplex(w_z)
166    res = c_isnan(x, y)
167    return space.newbool(res)
168wrapped_isnan.func_doc = names_and_docstrings['isnan']