PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/module/cmath/interp_cmath.py

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