_mptestutils.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. import os
  2. import sys
  3. import time
  4. from itertools import zip_longest
  5. import numpy as np
  6. from numpy.testing import assert_
  7. import pytest
  8. from scipy.special._testutils import assert_func_equal
  9. try:
  10. import mpmath
  11. except ImportError:
  12. pass
  13. # ------------------------------------------------------------------------------
  14. # Machinery for systematic tests with mpmath
  15. # ------------------------------------------------------------------------------
  16. class Arg:
  17. """Generate a set of numbers on the real axis, concentrating on
  18. 'interesting' regions and covering all orders of magnitude.
  19. """
  20. def __init__(self, a=-np.inf, b=np.inf, inclusive_a=True, inclusive_b=True):
  21. if a > b:
  22. raise ValueError("a should be less than or equal to b")
  23. if a == -np.inf:
  24. a = -0.5*np.finfo(float).max
  25. if b == np.inf:
  26. b = 0.5*np.finfo(float).max
  27. self.a, self.b = a, b
  28. self.inclusive_a, self.inclusive_b = inclusive_a, inclusive_b
  29. def _positive_values(self, a, b, n):
  30. if a < 0:
  31. raise ValueError("a should be positive")
  32. # Try to put half of the points into a linspace between a and
  33. # 10 the other half in a logspace.
  34. if n % 2 == 0:
  35. nlogpts = n//2
  36. nlinpts = nlogpts
  37. else:
  38. nlogpts = n//2
  39. nlinpts = nlogpts + 1
  40. if a >= 10:
  41. # Outside of linspace range; just return a logspace.
  42. pts = np.logspace(np.log10(a), np.log10(b), n)
  43. elif a > 0 and b < 10:
  44. # Outside of logspace range; just return a linspace
  45. pts = np.linspace(a, b, n)
  46. elif a > 0:
  47. # Linspace between a and 10 and a logspace between 10 and
  48. # b.
  49. linpts = np.linspace(a, 10, nlinpts, endpoint=False)
  50. logpts = np.logspace(1, np.log10(b), nlogpts)
  51. pts = np.hstack((linpts, logpts))
  52. elif a == 0 and b <= 10:
  53. # Linspace between 0 and b and a logspace between 0 and
  54. # the smallest positive point of the linspace
  55. linpts = np.linspace(0, b, nlinpts)
  56. if linpts.size > 1:
  57. right = np.log10(linpts[1])
  58. else:
  59. right = -30
  60. logpts = np.logspace(-30, right, nlogpts, endpoint=False)
  61. pts = np.hstack((logpts, linpts))
  62. else:
  63. # Linspace between 0 and 10, logspace between 0 and the
  64. # smallest positive point of the linspace, and a logspace
  65. # between 10 and b.
  66. if nlogpts % 2 == 0:
  67. nlogpts1 = nlogpts//2
  68. nlogpts2 = nlogpts1
  69. else:
  70. nlogpts1 = nlogpts//2
  71. nlogpts2 = nlogpts1 + 1
  72. linpts = np.linspace(0, 10, nlinpts, endpoint=False)
  73. if linpts.size > 1:
  74. right = np.log10(linpts[1])
  75. else:
  76. right = -30
  77. logpts1 = np.logspace(-30, right, nlogpts1, endpoint=False)
  78. logpts2 = np.logspace(1, np.log10(b), nlogpts2)
  79. pts = np.hstack((logpts1, linpts, logpts2))
  80. return np.sort(pts)
  81. def values(self, n):
  82. """Return an array containing n numbers."""
  83. a, b = self.a, self.b
  84. if a == b:
  85. return np.zeros(n)
  86. if not self.inclusive_a:
  87. n += 1
  88. if not self.inclusive_b:
  89. n += 1
  90. if n % 2 == 0:
  91. n1 = n//2
  92. n2 = n1
  93. else:
  94. n1 = n//2
  95. n2 = n1 + 1
  96. if a >= 0:
  97. pospts = self._positive_values(a, b, n)
  98. negpts = []
  99. elif b <= 0:
  100. pospts = []
  101. negpts = -self._positive_values(-b, -a, n)
  102. else:
  103. pospts = self._positive_values(0, b, n1)
  104. negpts = -self._positive_values(0, -a, n2 + 1)
  105. # Don't want to get zero twice
  106. negpts = negpts[1:]
  107. pts = np.hstack((negpts[::-1], pospts))
  108. if not self.inclusive_a:
  109. pts = pts[1:]
  110. if not self.inclusive_b:
  111. pts = pts[:-1]
  112. return pts
  113. class FixedArg:
  114. def __init__(self, values):
  115. self._values = np.asarray(values)
  116. def values(self, n):
  117. return self._values
  118. class ComplexArg:
  119. def __init__(self, a=complex(-np.inf, -np.inf), b=complex(np.inf, np.inf)):
  120. self.real = Arg(a.real, b.real)
  121. self.imag = Arg(a.imag, b.imag)
  122. def values(self, n):
  123. m = int(np.floor(np.sqrt(n)))
  124. x = self.real.values(m)
  125. y = self.imag.values(m + 1)
  126. return (x[:,None] + 1j*y[None,:]).ravel()
  127. class IntArg:
  128. def __init__(self, a=-1000, b=1000):
  129. self.a = a
  130. self.b = b
  131. def values(self, n):
  132. v1 = Arg(self.a, self.b).values(max(1 + n//2, n-5)).astype(int)
  133. v2 = np.arange(-5, 5)
  134. v = np.unique(np.r_[v1, v2])
  135. v = v[(v >= self.a) & (v < self.b)]
  136. return v
  137. def get_args(argspec, n):
  138. if isinstance(argspec, np.ndarray):
  139. args = argspec.copy()
  140. else:
  141. nargs = len(argspec)
  142. ms = np.asarray([1.5 if isinstance(spec, ComplexArg) else 1.0 for spec in argspec])
  143. ms = (n**(ms/sum(ms))).astype(int) + 1
  144. args = [spec.values(m) for spec, m in zip(argspec, ms)]
  145. args = np.array(np.broadcast_arrays(*np.ix_(*args))).reshape(nargs, -1).T
  146. return args
  147. class MpmathData:
  148. def __init__(self, scipy_func, mpmath_func, arg_spec, name=None,
  149. dps=None, prec=None, n=None, rtol=1e-7, atol=1e-300,
  150. ignore_inf_sign=False, distinguish_nan_and_inf=True,
  151. nan_ok=True, param_filter=None):
  152. # mpmath tests are really slow (see gh-6989). Use a small number of
  153. # points by default, increase back to 5000 (old default) if XSLOW is
  154. # set
  155. if n is None:
  156. try:
  157. is_xslow = int(os.environ.get('SCIPY_XSLOW', '0'))
  158. except ValueError:
  159. is_xslow = False
  160. n = 5000 if is_xslow else 500
  161. self.scipy_func = scipy_func
  162. self.mpmath_func = mpmath_func
  163. self.arg_spec = arg_spec
  164. self.dps = dps
  165. self.prec = prec
  166. self.n = n
  167. self.rtol = rtol
  168. self.atol = atol
  169. self.ignore_inf_sign = ignore_inf_sign
  170. self.nan_ok = nan_ok
  171. if isinstance(self.arg_spec, np.ndarray):
  172. self.is_complex = np.issubdtype(self.arg_spec.dtype, np.complexfloating)
  173. else:
  174. self.is_complex = any([isinstance(arg, ComplexArg) for arg in self.arg_spec])
  175. self.ignore_inf_sign = ignore_inf_sign
  176. self.distinguish_nan_and_inf = distinguish_nan_and_inf
  177. if not name or name == '<lambda>':
  178. name = getattr(scipy_func, '__name__', None)
  179. if not name or name == '<lambda>':
  180. name = getattr(mpmath_func, '__name__', None)
  181. self.name = name
  182. self.param_filter = param_filter
  183. def check(self):
  184. np.random.seed(1234)
  185. # Generate values for the arguments
  186. argarr = get_args(self.arg_spec, self.n)
  187. # Check
  188. old_dps, old_prec = mpmath.mp.dps, mpmath.mp.prec
  189. try:
  190. if self.dps is not None:
  191. dps_list = [self.dps]
  192. else:
  193. dps_list = [20]
  194. if self.prec is not None:
  195. mpmath.mp.prec = self.prec
  196. # Proper casting of mpmath input and output types. Using
  197. # native mpmath types as inputs gives improved precision
  198. # in some cases.
  199. if np.issubdtype(argarr.dtype, np.complexfloating):
  200. pytype = mpc2complex
  201. def mptype(x):
  202. return mpmath.mpc(complex(x))
  203. else:
  204. def mptype(x):
  205. return mpmath.mpf(float(x))
  206. def pytype(x):
  207. if abs(x.imag) > 1e-16*(1 + abs(x.real)):
  208. return np.nan
  209. else:
  210. return mpf2float(x.real)
  211. # Try out different dps until one (or none) works
  212. for j, dps in enumerate(dps_list):
  213. mpmath.mp.dps = dps
  214. try:
  215. assert_func_equal(self.scipy_func,
  216. lambda *a: pytype(self.mpmath_func(*map(mptype, a))),
  217. argarr,
  218. vectorized=False,
  219. rtol=self.rtol, atol=self.atol,
  220. ignore_inf_sign=self.ignore_inf_sign,
  221. distinguish_nan_and_inf=self.distinguish_nan_and_inf,
  222. nan_ok=self.nan_ok,
  223. param_filter=self.param_filter)
  224. break
  225. except AssertionError:
  226. if j >= len(dps_list)-1:
  227. # reraise the Exception
  228. tp, value, tb = sys.exc_info()
  229. if value.__traceback__ is not tb:
  230. raise value.with_traceback(tb)
  231. raise value
  232. finally:
  233. mpmath.mp.dps, mpmath.mp.prec = old_dps, old_prec
  234. def __repr__(self):
  235. if self.is_complex:
  236. return "<MpmathData: %s (complex)>" % (self.name,)
  237. else:
  238. return "<MpmathData: %s>" % (self.name,)
  239. def assert_mpmath_equal(*a, **kw):
  240. d = MpmathData(*a, **kw)
  241. d.check()
  242. def nonfunctional_tooslow(func):
  243. return pytest.mark.skip(reason=" Test not yet functional (too slow), needs more work.")(func)
  244. # ------------------------------------------------------------------------------
  245. # Tools for dealing with mpmath quirks
  246. # ------------------------------------------------------------------------------
  247. def mpf2float(x):
  248. """
  249. Convert an mpf to the nearest floating point number. Just using
  250. float directly doesn't work because of results like this:
  251. with mp.workdps(50):
  252. float(mpf("0.99999999999999999")) = 0.9999999999999999
  253. """
  254. return float(mpmath.nstr(x, 17, min_fixed=0, max_fixed=0))
  255. def mpc2complex(x):
  256. return complex(mpf2float(x.real), mpf2float(x.imag))
  257. def trace_args(func):
  258. def tofloat(x):
  259. if isinstance(x, mpmath.mpc):
  260. return complex(x)
  261. else:
  262. return float(x)
  263. def wrap(*a, **kw):
  264. sys.stderr.write("%r: " % (tuple(map(tofloat, a)),))
  265. sys.stderr.flush()
  266. try:
  267. r = func(*a, **kw)
  268. sys.stderr.write("-> %r" % r)
  269. finally:
  270. sys.stderr.write("\n")
  271. sys.stderr.flush()
  272. return r
  273. return wrap
  274. try:
  275. import posix
  276. import signal
  277. POSIX = ('setitimer' in dir(signal))
  278. except ImportError:
  279. POSIX = False
  280. class TimeoutError(Exception):
  281. pass
  282. def time_limited(timeout=0.5, return_val=np.nan, use_sigalrm=True):
  283. """
  284. Decorator for setting a timeout for pure-Python functions.
  285. If the function does not return within `timeout` seconds, the
  286. value `return_val` is returned instead.
  287. On POSIX this uses SIGALRM by default. On non-POSIX, settrace is
  288. used. Do not use this with threads: the SIGALRM implementation
  289. does probably not work well. The settrace implementation only
  290. traces the current thread.
  291. The settrace implementation slows down execution speed. Slowdown
  292. by a factor around 10 is probably typical.
  293. """
  294. if POSIX and use_sigalrm:
  295. def sigalrm_handler(signum, frame):
  296. raise TimeoutError()
  297. def deco(func):
  298. def wrap(*a, **kw):
  299. old_handler = signal.signal(signal.SIGALRM, sigalrm_handler)
  300. signal.setitimer(signal.ITIMER_REAL, timeout)
  301. try:
  302. return func(*a, **kw)
  303. except TimeoutError:
  304. return return_val
  305. finally:
  306. signal.setitimer(signal.ITIMER_REAL, 0)
  307. signal.signal(signal.SIGALRM, old_handler)
  308. return wrap
  309. else:
  310. def deco(func):
  311. def wrap(*a, **kw):
  312. start_time = time.time()
  313. def trace(frame, event, arg):
  314. if time.time() - start_time > timeout:
  315. raise TimeoutError()
  316. return trace
  317. sys.settrace(trace)
  318. try:
  319. return func(*a, **kw)
  320. except TimeoutError:
  321. sys.settrace(None)
  322. return return_val
  323. finally:
  324. sys.settrace(None)
  325. return wrap
  326. return deco
  327. def exception_to_nan(func):
  328. """Decorate function to return nan if it raises an exception"""
  329. def wrap(*a, **kw):
  330. try:
  331. return func(*a, **kw)
  332. except Exception:
  333. return np.nan
  334. return wrap
  335. def inf_to_nan(func):
  336. """Decorate function to return nan if it returns inf"""
  337. def wrap(*a, **kw):
  338. v = func(*a, **kw)
  339. if not np.isfinite(v):
  340. return np.nan
  341. return v
  342. return wrap
  343. def mp_assert_allclose(res, std, atol=0, rtol=1e-17):
  344. """
  345. Compare lists of mpmath.mpf's or mpmath.mpc's directly so that it
  346. can be done to higher precision than double.
  347. """
  348. failures = []
  349. for k, (resval, stdval) in enumerate(zip_longest(res, std)):
  350. if resval is None or stdval is None:
  351. raise ValueError('Lengths of inputs res and std are not equal.')
  352. if mpmath.fabs(resval - stdval) > atol + rtol*mpmath.fabs(stdval):
  353. failures.append((k, resval, stdval))
  354. nfail = len(failures)
  355. if nfail > 0:
  356. ndigits = int(abs(np.log10(rtol)))
  357. msg = [""]
  358. msg.append("Bad results ({} out of {}) for the following points:"
  359. .format(nfail, k + 1))
  360. for k, resval, stdval in failures:
  361. resrep = mpmath.nstr(resval, ndigits, min_fixed=0, max_fixed=0)
  362. stdrep = mpmath.nstr(stdval, ndigits, min_fixed=0, max_fixed=0)
  363. if stdval == 0:
  364. rdiff = "inf"
  365. else:
  366. rdiff = mpmath.fabs((resval - stdval)/stdval)
  367. rdiff = mpmath.nstr(rdiff, 3)
  368. msg.append("{}: {} != {} (rdiff {})".format(k, resrep, stdrep,
  369. rdiff))
  370. assert_(False, "\n".join(msg))