test_scalarmath.py 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. import contextlib
  2. import sys
  3. import warnings
  4. import itertools
  5. import operator
  6. import platform
  7. from numpy.compat import _pep440
  8. import pytest
  9. from hypothesis import given, settings
  10. from hypothesis.strategies import sampled_from
  11. from hypothesis.extra import numpy as hynp
  12. import numpy as np
  13. from numpy.testing import (
  14. assert_, assert_equal, assert_raises, assert_almost_equal,
  15. assert_array_equal, IS_PYPY, suppress_warnings, _gen_alignment_data,
  16. assert_warns,
  17. )
  18. types = [np.bool_, np.byte, np.ubyte, np.short, np.ushort, np.intc, np.uintc,
  19. np.int_, np.uint, np.longlong, np.ulonglong,
  20. np.single, np.double, np.longdouble, np.csingle,
  21. np.cdouble, np.clongdouble]
  22. floating_types = np.floating.__subclasses__()
  23. complex_floating_types = np.complexfloating.__subclasses__()
  24. objecty_things = [object(), None]
  25. reasonable_operators_for_scalars = [
  26. operator.lt, operator.le, operator.eq, operator.ne, operator.ge,
  27. operator.gt, operator.add, operator.floordiv, operator.mod,
  28. operator.mul, operator.pow, operator.sub, operator.truediv,
  29. ]
  30. # This compares scalarmath against ufuncs.
  31. class TestTypes:
  32. def test_types(self):
  33. for atype in types:
  34. a = atype(1)
  35. assert_(a == 1, "error with %r: got %r" % (atype, a))
  36. def test_type_add(self):
  37. # list of types
  38. for k, atype in enumerate(types):
  39. a_scalar = atype(3)
  40. a_array = np.array([3], dtype=atype)
  41. for l, btype in enumerate(types):
  42. b_scalar = btype(1)
  43. b_array = np.array([1], dtype=btype)
  44. c_scalar = a_scalar + b_scalar
  45. c_array = a_array + b_array
  46. # It was comparing the type numbers, but the new ufunc
  47. # function-finding mechanism finds the lowest function
  48. # to which both inputs can be cast - which produces 'l'
  49. # when you do 'q' + 'b'. The old function finding mechanism
  50. # skipped ahead based on the first argument, but that
  51. # does not produce properly symmetric results...
  52. assert_equal(c_scalar.dtype, c_array.dtype,
  53. "error with types (%d/'%c' + %d/'%c')" %
  54. (k, np.dtype(atype).char, l, np.dtype(btype).char))
  55. def test_type_create(self):
  56. for k, atype in enumerate(types):
  57. a = np.array([1, 2, 3], atype)
  58. b = atype([1, 2, 3])
  59. assert_equal(a, b)
  60. def test_leak(self):
  61. # test leak of scalar objects
  62. # a leak would show up in valgrind as still-reachable of ~2.6MB
  63. for i in range(200000):
  64. np.add(1, 1)
  65. def check_ufunc_scalar_equivalence(op, arr1, arr2):
  66. scalar1 = arr1[()]
  67. scalar2 = arr2[()]
  68. assert isinstance(scalar1, np.generic)
  69. assert isinstance(scalar2, np.generic)
  70. if arr1.dtype.kind == "c" or arr2.dtype.kind == "c":
  71. comp_ops = {operator.ge, operator.gt, operator.le, operator.lt}
  72. if op in comp_ops and (np.isnan(scalar1) or np.isnan(scalar2)):
  73. pytest.xfail("complex comp ufuncs use sort-order, scalars do not.")
  74. if op == operator.pow and arr2.item() in [-1, 0, 0.5, 1, 2]:
  75. # array**scalar special case can have different result dtype
  76. # (Other powers may have issues also, but are not hit here.)
  77. # TODO: It would be nice to resolve this issue.
  78. pytest.skip("array**2 can have incorrect/weird result dtype")
  79. # ignore fpe's since they may just mismatch for integers anyway.
  80. with warnings.catch_warnings(), np.errstate(all="ignore"):
  81. # Comparisons DeprecationWarnings replacing errors (2022-03):
  82. warnings.simplefilter("error", DeprecationWarning)
  83. try:
  84. res = op(arr1, arr2)
  85. except Exception as e:
  86. with pytest.raises(type(e)):
  87. op(scalar1, scalar2)
  88. else:
  89. scalar_res = op(scalar1, scalar2)
  90. assert_array_equal(scalar_res, res, strict=True)
  91. @pytest.mark.slow
  92. @settings(max_examples=10000, deadline=2000)
  93. @given(sampled_from(reasonable_operators_for_scalars),
  94. hynp.arrays(dtype=hynp.scalar_dtypes(), shape=()),
  95. hynp.arrays(dtype=hynp.scalar_dtypes(), shape=()))
  96. def test_array_scalar_ufunc_equivalence(op, arr1, arr2):
  97. """
  98. This is a thorough test attempting to cover important promotion paths
  99. and ensuring that arrays and scalars stay as aligned as possible.
  100. However, if it creates troubles, it should maybe just be removed.
  101. """
  102. check_ufunc_scalar_equivalence(op, arr1, arr2)
  103. @pytest.mark.slow
  104. @given(sampled_from(reasonable_operators_for_scalars),
  105. hynp.scalar_dtypes(), hynp.scalar_dtypes())
  106. def test_array_scalar_ufunc_dtypes(op, dt1, dt2):
  107. # Same as above, but don't worry about sampling weird values so that we
  108. # do not have to sample as much
  109. arr1 = np.array(2, dtype=dt1)
  110. arr2 = np.array(3, dtype=dt2) # some power do weird things.
  111. check_ufunc_scalar_equivalence(op, arr1, arr2)
  112. @pytest.mark.parametrize("fscalar", [np.float16, np.float32])
  113. def test_int_float_promotion_truediv(fscalar):
  114. # Promotion for mixed int and float32/float16 must not go to float64
  115. i = np.int8(1)
  116. f = fscalar(1)
  117. expected = np.result_type(i, f)
  118. assert (i / f).dtype == expected
  119. assert (f / i).dtype == expected
  120. # But normal int / int true division goes to float64:
  121. assert (i / i).dtype == np.dtype("float64")
  122. # For int16, result has to be ast least float32 (takes ufunc path):
  123. assert (np.int16(1) / f).dtype == np.dtype("float32")
  124. class TestBaseMath:
  125. def test_blocked(self):
  126. # test alignments offsets for simd instructions
  127. # alignments for vz + 2 * (vs - 1) + 1
  128. for dt, sz in [(np.float32, 11), (np.float64, 7), (np.int32, 11)]:
  129. for out, inp1, inp2, msg in _gen_alignment_data(dtype=dt,
  130. type='binary',
  131. max_size=sz):
  132. exp1 = np.ones_like(inp1)
  133. inp1[...] = np.ones_like(inp1)
  134. inp2[...] = np.zeros_like(inp2)
  135. assert_almost_equal(np.add(inp1, inp2), exp1, err_msg=msg)
  136. assert_almost_equal(np.add(inp1, 2), exp1 + 2, err_msg=msg)
  137. assert_almost_equal(np.add(1, inp2), exp1, err_msg=msg)
  138. np.add(inp1, inp2, out=out)
  139. assert_almost_equal(out, exp1, err_msg=msg)
  140. inp2[...] += np.arange(inp2.size, dtype=dt) + 1
  141. assert_almost_equal(np.square(inp2),
  142. np.multiply(inp2, inp2), err_msg=msg)
  143. # skip true divide for ints
  144. if dt != np.int32:
  145. assert_almost_equal(np.reciprocal(inp2),
  146. np.divide(1, inp2), err_msg=msg)
  147. inp1[...] = np.ones_like(inp1)
  148. np.add(inp1, 2, out=out)
  149. assert_almost_equal(out, exp1 + 2, err_msg=msg)
  150. inp2[...] = np.ones_like(inp2)
  151. np.add(2, inp2, out=out)
  152. assert_almost_equal(out, exp1 + 2, err_msg=msg)
  153. def test_lower_align(self):
  154. # check data that is not aligned to element size
  155. # i.e doubles are aligned to 4 bytes on i386
  156. d = np.zeros(23 * 8, dtype=np.int8)[4:-4].view(np.float64)
  157. o = np.zeros(23 * 8, dtype=np.int8)[4:-4].view(np.float64)
  158. assert_almost_equal(d + d, d * 2)
  159. np.add(d, d, out=o)
  160. np.add(np.ones_like(d), d, out=o)
  161. np.add(d, np.ones_like(d), out=o)
  162. np.add(np.ones_like(d), d)
  163. np.add(d, np.ones_like(d))
  164. class TestPower:
  165. def test_small_types(self):
  166. for t in [np.int8, np.int16, np.float16]:
  167. a = t(3)
  168. b = a ** 4
  169. assert_(b == 81, "error with %r: got %r" % (t, b))
  170. def test_large_types(self):
  171. for t in [np.int32, np.int64, np.float32, np.float64, np.longdouble]:
  172. a = t(51)
  173. b = a ** 4
  174. msg = "error with %r: got %r" % (t, b)
  175. if np.issubdtype(t, np.integer):
  176. assert_(b == 6765201, msg)
  177. else:
  178. assert_almost_equal(b, 6765201, err_msg=msg)
  179. def test_integers_to_negative_integer_power(self):
  180. # Note that the combination of uint64 with a signed integer
  181. # has common type np.float64. The other combinations should all
  182. # raise a ValueError for integer ** negative integer.
  183. exp = [np.array(-1, dt)[()] for dt in 'bhilq']
  184. # 1 ** -1 possible special case
  185. base = [np.array(1, dt)[()] for dt in 'bhilqBHILQ']
  186. for i1, i2 in itertools.product(base, exp):
  187. if i1.dtype != np.uint64:
  188. assert_raises(ValueError, operator.pow, i1, i2)
  189. else:
  190. res = operator.pow(i1, i2)
  191. assert_(res.dtype.type is np.float64)
  192. assert_almost_equal(res, 1.)
  193. # -1 ** -1 possible special case
  194. base = [np.array(-1, dt)[()] for dt in 'bhilq']
  195. for i1, i2 in itertools.product(base, exp):
  196. if i1.dtype != np.uint64:
  197. assert_raises(ValueError, operator.pow, i1, i2)
  198. else:
  199. res = operator.pow(i1, i2)
  200. assert_(res.dtype.type is np.float64)
  201. assert_almost_equal(res, -1.)
  202. # 2 ** -1 perhaps generic
  203. base = [np.array(2, dt)[()] for dt in 'bhilqBHILQ']
  204. for i1, i2 in itertools.product(base, exp):
  205. if i1.dtype != np.uint64:
  206. assert_raises(ValueError, operator.pow, i1, i2)
  207. else:
  208. res = operator.pow(i1, i2)
  209. assert_(res.dtype.type is np.float64)
  210. assert_almost_equal(res, .5)
  211. def test_mixed_types(self):
  212. typelist = [np.int8, np.int16, np.float16,
  213. np.float32, np.float64, np.int8,
  214. np.int16, np.int32, np.int64]
  215. for t1 in typelist:
  216. for t2 in typelist:
  217. a = t1(3)
  218. b = t2(2)
  219. result = a**b
  220. msg = ("error with %r and %r:"
  221. "got %r, expected %r") % (t1, t2, result, 9)
  222. if np.issubdtype(np.dtype(result), np.integer):
  223. assert_(result == 9, msg)
  224. else:
  225. assert_almost_equal(result, 9, err_msg=msg)
  226. def test_modular_power(self):
  227. # modular power is not implemented, so ensure it errors
  228. a = 5
  229. b = 4
  230. c = 10
  231. expected = pow(a, b, c) # noqa: F841
  232. for t in (np.int32, np.float32, np.complex64):
  233. # note that 3-operand power only dispatches on the first argument
  234. assert_raises(TypeError, operator.pow, t(a), b, c)
  235. assert_raises(TypeError, operator.pow, np.array(t(a)), b, c)
  236. def floordiv_and_mod(x, y):
  237. return (x // y, x % y)
  238. def _signs(dt):
  239. if dt in np.typecodes['UnsignedInteger']:
  240. return (+1,)
  241. else:
  242. return (+1, -1)
  243. class TestModulus:
  244. def test_modulus_basic(self):
  245. dt = np.typecodes['AllInteger'] + np.typecodes['Float']
  246. for op in [floordiv_and_mod, divmod]:
  247. for dt1, dt2 in itertools.product(dt, dt):
  248. for sg1, sg2 in itertools.product(_signs(dt1), _signs(dt2)):
  249. fmt = 'op: %s, dt1: %s, dt2: %s, sg1: %s, sg2: %s'
  250. msg = fmt % (op.__name__, dt1, dt2, sg1, sg2)
  251. a = np.array(sg1*71, dtype=dt1)[()]
  252. b = np.array(sg2*19, dtype=dt2)[()]
  253. div, rem = op(a, b)
  254. assert_equal(div*b + rem, a, err_msg=msg)
  255. if sg2 == -1:
  256. assert_(b < rem <= 0, msg)
  257. else:
  258. assert_(b > rem >= 0, msg)
  259. def test_float_modulus_exact(self):
  260. # test that float results are exact for small integers. This also
  261. # holds for the same integers scaled by powers of two.
  262. nlst = list(range(-127, 0))
  263. plst = list(range(1, 128))
  264. dividend = nlst + [0] + plst
  265. divisor = nlst + plst
  266. arg = list(itertools.product(dividend, divisor))
  267. tgt = list(divmod(*t) for t in arg)
  268. a, b = np.array(arg, dtype=int).T
  269. # convert exact integer results from Python to float so that
  270. # signed zero can be used, it is checked.
  271. tgtdiv, tgtrem = np.array(tgt, dtype=float).T
  272. tgtdiv = np.where((tgtdiv == 0.0) & ((b < 0) ^ (a < 0)), -0.0, tgtdiv)
  273. tgtrem = np.where((tgtrem == 0.0) & (b < 0), -0.0, tgtrem)
  274. for op in [floordiv_and_mod, divmod]:
  275. for dt in np.typecodes['Float']:
  276. msg = 'op: %s, dtype: %s' % (op.__name__, dt)
  277. fa = a.astype(dt)
  278. fb = b.astype(dt)
  279. # use list comprehension so a_ and b_ are scalars
  280. div, rem = zip(*[op(a_, b_) for a_, b_ in zip(fa, fb)])
  281. assert_equal(div, tgtdiv, err_msg=msg)
  282. assert_equal(rem, tgtrem, err_msg=msg)
  283. def test_float_modulus_roundoff(self):
  284. # gh-6127
  285. dt = np.typecodes['Float']
  286. for op in [floordiv_and_mod, divmod]:
  287. for dt1, dt2 in itertools.product(dt, dt):
  288. for sg1, sg2 in itertools.product((+1, -1), (+1, -1)):
  289. fmt = 'op: %s, dt1: %s, dt2: %s, sg1: %s, sg2: %s'
  290. msg = fmt % (op.__name__, dt1, dt2, sg1, sg2)
  291. a = np.array(sg1*78*6e-8, dtype=dt1)[()]
  292. b = np.array(sg2*6e-8, dtype=dt2)[()]
  293. div, rem = op(a, b)
  294. # Equal assertion should hold when fmod is used
  295. assert_equal(div*b + rem, a, err_msg=msg)
  296. if sg2 == -1:
  297. assert_(b < rem <= 0, msg)
  298. else:
  299. assert_(b > rem >= 0, msg)
  300. def test_float_modulus_corner_cases(self):
  301. # Check remainder magnitude.
  302. for dt in np.typecodes['Float']:
  303. b = np.array(1.0, dtype=dt)
  304. a = np.nextafter(np.array(0.0, dtype=dt), -b)
  305. rem = operator.mod(a, b)
  306. assert_(rem <= b, 'dt: %s' % dt)
  307. rem = operator.mod(-a, -b)
  308. assert_(rem >= -b, 'dt: %s' % dt)
  309. # Check nans, inf
  310. with suppress_warnings() as sup:
  311. sup.filter(RuntimeWarning, "invalid value encountered in remainder")
  312. sup.filter(RuntimeWarning, "divide by zero encountered in remainder")
  313. sup.filter(RuntimeWarning, "divide by zero encountered in floor_divide")
  314. sup.filter(RuntimeWarning, "divide by zero encountered in divmod")
  315. sup.filter(RuntimeWarning, "invalid value encountered in divmod")
  316. for dt in np.typecodes['Float']:
  317. fone = np.array(1.0, dtype=dt)
  318. fzer = np.array(0.0, dtype=dt)
  319. finf = np.array(np.inf, dtype=dt)
  320. fnan = np.array(np.nan, dtype=dt)
  321. rem = operator.mod(fone, fzer)
  322. assert_(np.isnan(rem), 'dt: %s' % dt)
  323. # MSVC 2008 returns NaN here, so disable the check.
  324. #rem = operator.mod(fone, finf)
  325. #assert_(rem == fone, 'dt: %s' % dt)
  326. rem = operator.mod(fone, fnan)
  327. assert_(np.isnan(rem), 'dt: %s' % dt)
  328. rem = operator.mod(finf, fone)
  329. assert_(np.isnan(rem), 'dt: %s' % dt)
  330. for op in [floordiv_and_mod, divmod]:
  331. div, mod = op(fone, fzer)
  332. assert_(np.isinf(div)) and assert_(np.isnan(mod))
  333. def test_inplace_floordiv_handling(self):
  334. # issue gh-12927
  335. # this only applies to in-place floordiv //=, because the output type
  336. # promotes to float which does not fit
  337. a = np.array([1, 2], np.int64)
  338. b = np.array([1, 2], np.uint64)
  339. with pytest.raises(TypeError,
  340. match=r"Cannot cast ufunc 'floor_divide' output from"):
  341. a //= b
  342. class TestComplexDivision:
  343. def test_zero_division(self):
  344. with np.errstate(all="ignore"):
  345. for t in [np.complex64, np.complex128]:
  346. a = t(0.0)
  347. b = t(1.0)
  348. assert_(np.isinf(b/a))
  349. b = t(complex(np.inf, np.inf))
  350. assert_(np.isinf(b/a))
  351. b = t(complex(np.inf, np.nan))
  352. assert_(np.isinf(b/a))
  353. b = t(complex(np.nan, np.inf))
  354. assert_(np.isinf(b/a))
  355. b = t(complex(np.nan, np.nan))
  356. assert_(np.isnan(b/a))
  357. b = t(0.)
  358. assert_(np.isnan(b/a))
  359. def test_signed_zeros(self):
  360. with np.errstate(all="ignore"):
  361. for t in [np.complex64, np.complex128]:
  362. # tupled (numerator, denominator, expected)
  363. # for testing as expected == numerator/denominator
  364. data = (
  365. (( 0.0,-1.0), ( 0.0, 1.0), (-1.0,-0.0)),
  366. (( 0.0,-1.0), ( 0.0,-1.0), ( 1.0,-0.0)),
  367. (( 0.0,-1.0), (-0.0,-1.0), ( 1.0, 0.0)),
  368. (( 0.0,-1.0), (-0.0, 1.0), (-1.0, 0.0)),
  369. (( 0.0, 1.0), ( 0.0,-1.0), (-1.0, 0.0)),
  370. (( 0.0,-1.0), ( 0.0,-1.0), ( 1.0,-0.0)),
  371. ((-0.0,-1.0), ( 0.0,-1.0), ( 1.0,-0.0)),
  372. ((-0.0, 1.0), ( 0.0,-1.0), (-1.0,-0.0))
  373. )
  374. for cases in data:
  375. n = cases[0]
  376. d = cases[1]
  377. ex = cases[2]
  378. result = t(complex(n[0], n[1])) / t(complex(d[0], d[1]))
  379. # check real and imag parts separately to avoid comparison
  380. # in array context, which does not account for signed zeros
  381. assert_equal(result.real, ex[0])
  382. assert_equal(result.imag, ex[1])
  383. def test_branches(self):
  384. with np.errstate(all="ignore"):
  385. for t in [np.complex64, np.complex128]:
  386. # tupled (numerator, denominator, expected)
  387. # for testing as expected == numerator/denominator
  388. data = list()
  389. # trigger branch: real(fabs(denom)) > imag(fabs(denom))
  390. # followed by else condition as neither are == 0
  391. data.append((( 2.0, 1.0), ( 2.0, 1.0), (1.0, 0.0)))
  392. # trigger branch: real(fabs(denom)) > imag(fabs(denom))
  393. # followed by if condition as both are == 0
  394. # is performed in test_zero_division(), so this is skipped
  395. # trigger else if branch: real(fabs(denom)) < imag(fabs(denom))
  396. data.append((( 1.0, 2.0), ( 1.0, 2.0), (1.0, 0.0)))
  397. for cases in data:
  398. n = cases[0]
  399. d = cases[1]
  400. ex = cases[2]
  401. result = t(complex(n[0], n[1])) / t(complex(d[0], d[1]))
  402. # check real and imag parts separately to avoid comparison
  403. # in array context, which does not account for signed zeros
  404. assert_equal(result.real, ex[0])
  405. assert_equal(result.imag, ex[1])
  406. class TestConversion:
  407. def test_int_from_long(self):
  408. l = [1e6, 1e12, 1e18, -1e6, -1e12, -1e18]
  409. li = [10**6, 10**12, 10**18, -10**6, -10**12, -10**18]
  410. for T in [None, np.float64, np.int64]:
  411. a = np.array(l, dtype=T)
  412. assert_equal([int(_m) for _m in a], li)
  413. a = np.array(l[:3], dtype=np.uint64)
  414. assert_equal([int(_m) for _m in a], li[:3])
  415. def test_iinfo_long_values(self):
  416. for code in 'bBhH':
  417. with pytest.warns(DeprecationWarning):
  418. res = np.array(np.iinfo(code).max + 1, dtype=code)
  419. tgt = np.iinfo(code).min
  420. assert_(res == tgt)
  421. for code in np.typecodes['AllInteger']:
  422. res = np.array(np.iinfo(code).max, dtype=code)
  423. tgt = np.iinfo(code).max
  424. assert_(res == tgt)
  425. for code in np.typecodes['AllInteger']:
  426. res = np.dtype(code).type(np.iinfo(code).max)
  427. tgt = np.iinfo(code).max
  428. assert_(res == tgt)
  429. def test_int_raise_behaviour(self):
  430. def overflow_error_func(dtype):
  431. dtype(np.iinfo(dtype).max + 1)
  432. for code in [np.int_, np.uint, np.longlong, np.ulonglong]:
  433. assert_raises(OverflowError, overflow_error_func, code)
  434. def test_int_from_infinite_longdouble(self):
  435. # gh-627
  436. x = np.longdouble(np.inf)
  437. assert_raises(OverflowError, int, x)
  438. with suppress_warnings() as sup:
  439. sup.record(np.ComplexWarning)
  440. x = np.clongdouble(np.inf)
  441. assert_raises(OverflowError, int, x)
  442. assert_equal(len(sup.log), 1)
  443. @pytest.mark.skipif(not IS_PYPY, reason="Test is PyPy only (gh-9972)")
  444. def test_int_from_infinite_longdouble___int__(self):
  445. x = np.longdouble(np.inf)
  446. assert_raises(OverflowError, x.__int__)
  447. with suppress_warnings() as sup:
  448. sup.record(np.ComplexWarning)
  449. x = np.clongdouble(np.inf)
  450. assert_raises(OverflowError, x.__int__)
  451. assert_equal(len(sup.log), 1)
  452. @pytest.mark.skipif(np.finfo(np.double) == np.finfo(np.longdouble),
  453. reason="long double is same as double")
  454. @pytest.mark.skipif(platform.machine().startswith("ppc"),
  455. reason="IBM double double")
  456. def test_int_from_huge_longdouble(self):
  457. # Produce a longdouble that would overflow a double,
  458. # use exponent that avoids bug in Darwin pow function.
  459. exp = np.finfo(np.double).maxexp - 1
  460. huge_ld = 2 * 1234 * np.longdouble(2) ** exp
  461. huge_i = 2 * 1234 * 2 ** exp
  462. assert_(huge_ld != np.inf)
  463. assert_equal(int(huge_ld), huge_i)
  464. def test_int_from_longdouble(self):
  465. x = np.longdouble(1.5)
  466. assert_equal(int(x), 1)
  467. x = np.longdouble(-10.5)
  468. assert_equal(int(x), -10)
  469. def test_numpy_scalar_relational_operators(self):
  470. # All integer
  471. for dt1 in np.typecodes['AllInteger']:
  472. assert_(1 > np.array(0, dtype=dt1)[()], "type %s failed" % (dt1,))
  473. assert_(not 1 < np.array(0, dtype=dt1)[()], "type %s failed" % (dt1,))
  474. for dt2 in np.typecodes['AllInteger']:
  475. assert_(np.array(1, dtype=dt1)[()] > np.array(0, dtype=dt2)[()],
  476. "type %s and %s failed" % (dt1, dt2))
  477. assert_(not np.array(1, dtype=dt1)[()] < np.array(0, dtype=dt2)[()],
  478. "type %s and %s failed" % (dt1, dt2))
  479. #Unsigned integers
  480. for dt1 in 'BHILQP':
  481. assert_(-1 < np.array(1, dtype=dt1)[()], "type %s failed" % (dt1,))
  482. assert_(not -1 > np.array(1, dtype=dt1)[()], "type %s failed" % (dt1,))
  483. assert_(-1 != np.array(1, dtype=dt1)[()], "type %s failed" % (dt1,))
  484. #unsigned vs signed
  485. for dt2 in 'bhilqp':
  486. assert_(np.array(1, dtype=dt1)[()] > np.array(-1, dtype=dt2)[()],
  487. "type %s and %s failed" % (dt1, dt2))
  488. assert_(not np.array(1, dtype=dt1)[()] < np.array(-1, dtype=dt2)[()],
  489. "type %s and %s failed" % (dt1, dt2))
  490. assert_(np.array(1, dtype=dt1)[()] != np.array(-1, dtype=dt2)[()],
  491. "type %s and %s failed" % (dt1, dt2))
  492. #Signed integers and floats
  493. for dt1 in 'bhlqp' + np.typecodes['Float']:
  494. assert_(1 > np.array(-1, dtype=dt1)[()], "type %s failed" % (dt1,))
  495. assert_(not 1 < np.array(-1, dtype=dt1)[()], "type %s failed" % (dt1,))
  496. assert_(-1 == np.array(-1, dtype=dt1)[()], "type %s failed" % (dt1,))
  497. for dt2 in 'bhlqp' + np.typecodes['Float']:
  498. assert_(np.array(1, dtype=dt1)[()] > np.array(-1, dtype=dt2)[()],
  499. "type %s and %s failed" % (dt1, dt2))
  500. assert_(not np.array(1, dtype=dt1)[()] < np.array(-1, dtype=dt2)[()],
  501. "type %s and %s failed" % (dt1, dt2))
  502. assert_(np.array(-1, dtype=dt1)[()] == np.array(-1, dtype=dt2)[()],
  503. "type %s and %s failed" % (dt1, dt2))
  504. def test_scalar_comparison_to_none(self):
  505. # Scalars should just return False and not give a warnings.
  506. # The comparisons are flagged by pep8, ignore that.
  507. with warnings.catch_warnings(record=True) as w:
  508. warnings.filterwarnings('always', '', FutureWarning)
  509. assert_(not np.float32(1) == None)
  510. assert_(not np.str_('test') == None)
  511. # This is dubious (see below):
  512. assert_(not np.datetime64('NaT') == None)
  513. assert_(np.float32(1) != None)
  514. assert_(np.str_('test') != None)
  515. # This is dubious (see below):
  516. assert_(np.datetime64('NaT') != None)
  517. assert_(len(w) == 0)
  518. # For documentation purposes, this is why the datetime is dubious.
  519. # At the time of deprecation this was no behaviour change, but
  520. # it has to be considered when the deprecations are done.
  521. assert_(np.equal(np.datetime64('NaT'), None))
  522. #class TestRepr:
  523. # def test_repr(self):
  524. # for t in types:
  525. # val = t(1197346475.0137341)
  526. # val_repr = repr(val)
  527. # val2 = eval(val_repr)
  528. # assert_equal( val, val2 )
  529. class TestRepr:
  530. def _test_type_repr(self, t):
  531. finfo = np.finfo(t)
  532. last_fraction_bit_idx = finfo.nexp + finfo.nmant
  533. last_exponent_bit_idx = finfo.nexp
  534. storage_bytes = np.dtype(t).itemsize*8
  535. # could add some more types to the list below
  536. for which in ['small denorm', 'small norm']:
  537. # Values from https://en.wikipedia.org/wiki/IEEE_754
  538. constr = np.array([0x00]*storage_bytes, dtype=np.uint8)
  539. if which == 'small denorm':
  540. byte = last_fraction_bit_idx // 8
  541. bytebit = 7-(last_fraction_bit_idx % 8)
  542. constr[byte] = 1 << bytebit
  543. elif which == 'small norm':
  544. byte = last_exponent_bit_idx // 8
  545. bytebit = 7-(last_exponent_bit_idx % 8)
  546. constr[byte] = 1 << bytebit
  547. else:
  548. raise ValueError('hmm')
  549. val = constr.view(t)[0]
  550. val_repr = repr(val)
  551. val2 = t(eval(val_repr))
  552. if not (val2 == 0 and val < 1e-100):
  553. assert_equal(val, val2)
  554. def test_float_repr(self):
  555. # long double test cannot work, because eval goes through a python
  556. # float
  557. for t in [np.float32, np.float64]:
  558. self._test_type_repr(t)
  559. if not IS_PYPY:
  560. # sys.getsizeof() is not valid on PyPy
  561. class TestSizeOf:
  562. def test_equal_nbytes(self):
  563. for type in types:
  564. x = type(0)
  565. assert_(sys.getsizeof(x) > x.nbytes)
  566. def test_error(self):
  567. d = np.float32()
  568. assert_raises(TypeError, d.__sizeof__, "a")
  569. class TestMultiply:
  570. def test_seq_repeat(self):
  571. # Test that basic sequences get repeated when multiplied with
  572. # numpy integers. And errors are raised when multiplied with others.
  573. # Some of this behaviour may be controversial and could be open for
  574. # change.
  575. accepted_types = set(np.typecodes["AllInteger"])
  576. deprecated_types = {'?'}
  577. forbidden_types = (
  578. set(np.typecodes["All"]) - accepted_types - deprecated_types)
  579. forbidden_types -= {'V'} # can't default-construct void scalars
  580. for seq_type in (list, tuple):
  581. seq = seq_type([1, 2, 3])
  582. for numpy_type in accepted_types:
  583. i = np.dtype(numpy_type).type(2)
  584. assert_equal(seq * i, seq * int(i))
  585. assert_equal(i * seq, int(i) * seq)
  586. for numpy_type in deprecated_types:
  587. i = np.dtype(numpy_type).type()
  588. assert_equal(
  589. assert_warns(DeprecationWarning, operator.mul, seq, i),
  590. seq * int(i))
  591. assert_equal(
  592. assert_warns(DeprecationWarning, operator.mul, i, seq),
  593. int(i) * seq)
  594. for numpy_type in forbidden_types:
  595. i = np.dtype(numpy_type).type()
  596. assert_raises(TypeError, operator.mul, seq, i)
  597. assert_raises(TypeError, operator.mul, i, seq)
  598. def test_no_seq_repeat_basic_array_like(self):
  599. # Test that an array-like which does not know how to be multiplied
  600. # does not attempt sequence repeat (raise TypeError).
  601. # See also gh-7428.
  602. class ArrayLike:
  603. def __init__(self, arr):
  604. self.arr = arr
  605. def __array__(self):
  606. return self.arr
  607. # Test for simple ArrayLike above and memoryviews (original report)
  608. for arr_like in (ArrayLike(np.ones(3)), memoryview(np.ones(3))):
  609. assert_array_equal(arr_like * np.float32(3.), np.full(3, 3.))
  610. assert_array_equal(np.float32(3.) * arr_like, np.full(3, 3.))
  611. assert_array_equal(arr_like * np.int_(3), np.full(3, 3))
  612. assert_array_equal(np.int_(3) * arr_like, np.full(3, 3))
  613. class TestNegative:
  614. def test_exceptions(self):
  615. a = np.ones((), dtype=np.bool_)[()]
  616. assert_raises(TypeError, operator.neg, a)
  617. def test_result(self):
  618. types = np.typecodes['AllInteger'] + np.typecodes['AllFloat']
  619. with suppress_warnings() as sup:
  620. sup.filter(RuntimeWarning)
  621. for dt in types:
  622. a = np.ones((), dtype=dt)[()]
  623. if dt in np.typecodes['UnsignedInteger']:
  624. st = np.dtype(dt).type
  625. max = st(np.iinfo(dt).max)
  626. assert_equal(operator.neg(a), max)
  627. else:
  628. assert_equal(operator.neg(a) + a, 0)
  629. class TestSubtract:
  630. def test_exceptions(self):
  631. a = np.ones((), dtype=np.bool_)[()]
  632. assert_raises(TypeError, operator.sub, a, a)
  633. def test_result(self):
  634. types = np.typecodes['AllInteger'] + np.typecodes['AllFloat']
  635. with suppress_warnings() as sup:
  636. sup.filter(RuntimeWarning)
  637. for dt in types:
  638. a = np.ones((), dtype=dt)[()]
  639. assert_equal(operator.sub(a, a), 0)
  640. class TestAbs:
  641. def _test_abs_func(self, absfunc, test_dtype):
  642. x = test_dtype(-1.5)
  643. assert_equal(absfunc(x), 1.5)
  644. x = test_dtype(0.0)
  645. res = absfunc(x)
  646. # assert_equal() checks zero signedness
  647. assert_equal(res, 0.0)
  648. x = test_dtype(-0.0)
  649. res = absfunc(x)
  650. assert_equal(res, 0.0)
  651. x = test_dtype(np.finfo(test_dtype).max)
  652. assert_equal(absfunc(x), x.real)
  653. with suppress_warnings() as sup:
  654. sup.filter(UserWarning)
  655. x = test_dtype(np.finfo(test_dtype).tiny)
  656. assert_equal(absfunc(x), x.real)
  657. x = test_dtype(np.finfo(test_dtype).min)
  658. assert_equal(absfunc(x), -x.real)
  659. @pytest.mark.parametrize("dtype", floating_types + complex_floating_types)
  660. def test_builtin_abs(self, dtype):
  661. if (
  662. sys.platform == "cygwin" and dtype == np.clongdouble and
  663. (
  664. _pep440.parse(platform.release().split("-")[0])
  665. < _pep440.Version("3.3.0")
  666. )
  667. ):
  668. pytest.xfail(
  669. reason="absl is computed in double precision on cygwin < 3.3"
  670. )
  671. self._test_abs_func(abs, dtype)
  672. @pytest.mark.parametrize("dtype", floating_types + complex_floating_types)
  673. def test_numpy_abs(self, dtype):
  674. if (
  675. sys.platform == "cygwin" and dtype == np.clongdouble and
  676. (
  677. _pep440.parse(platform.release().split("-")[0])
  678. < _pep440.Version("3.3.0")
  679. )
  680. ):
  681. pytest.xfail(
  682. reason="absl is computed in double precision on cygwin < 3.3"
  683. )
  684. self._test_abs_func(np.abs, dtype)
  685. class TestBitShifts:
  686. @pytest.mark.parametrize('type_code', np.typecodes['AllInteger'])
  687. @pytest.mark.parametrize('op',
  688. [operator.rshift, operator.lshift], ids=['>>', '<<'])
  689. def test_shift_all_bits(self, type_code, op):
  690. """ Shifts where the shift amount is the width of the type or wider """
  691. # gh-2449
  692. dt = np.dtype(type_code)
  693. nbits = dt.itemsize * 8
  694. for val in [5, -5]:
  695. for shift in [nbits, nbits + 4]:
  696. val_scl = np.array(val).astype(dt)[()]
  697. shift_scl = dt.type(shift)
  698. res_scl = op(val_scl, shift_scl)
  699. if val_scl < 0 and op is operator.rshift:
  700. # sign bit is preserved
  701. assert_equal(res_scl, -1)
  702. else:
  703. assert_equal(res_scl, 0)
  704. # Result on scalars should be the same as on arrays
  705. val_arr = np.array([val_scl]*32, dtype=dt)
  706. shift_arr = np.array([shift]*32, dtype=dt)
  707. res_arr = op(val_arr, shift_arr)
  708. assert_equal(res_arr, res_scl)
  709. class TestHash:
  710. @pytest.mark.parametrize("type_code", np.typecodes['AllInteger'])
  711. def test_integer_hashes(self, type_code):
  712. scalar = np.dtype(type_code).type
  713. for i in range(128):
  714. assert hash(i) == hash(scalar(i))
  715. @pytest.mark.parametrize("type_code", np.typecodes['AllFloat'])
  716. def test_float_and_complex_hashes(self, type_code):
  717. scalar = np.dtype(type_code).type
  718. for val in [np.pi, np.inf, 3, 6.]:
  719. numpy_val = scalar(val)
  720. # Cast back to Python, in case the NumPy scalar has less precision
  721. if numpy_val.dtype.kind == 'c':
  722. val = complex(numpy_val)
  723. else:
  724. val = float(numpy_val)
  725. assert val == numpy_val
  726. assert hash(val) == hash(numpy_val)
  727. if hash(float(np.nan)) != hash(float(np.nan)):
  728. # If Python distinguishes different NaNs we do so too (gh-18833)
  729. assert hash(scalar(np.nan)) != hash(scalar(np.nan))
  730. @pytest.mark.parametrize("type_code", np.typecodes['Complex'])
  731. def test_complex_hashes(self, type_code):
  732. # Test some complex valued hashes specifically:
  733. scalar = np.dtype(type_code).type
  734. for val in [np.pi+1j, np.inf-3j, 3j, 6.+1j]:
  735. numpy_val = scalar(val)
  736. assert hash(complex(numpy_val)) == hash(numpy_val)
  737. @contextlib.contextmanager
  738. def recursionlimit(n):
  739. o = sys.getrecursionlimit()
  740. try:
  741. sys.setrecursionlimit(n)
  742. yield
  743. finally:
  744. sys.setrecursionlimit(o)
  745. @given(sampled_from(objecty_things),
  746. sampled_from(reasonable_operators_for_scalars),
  747. sampled_from(types))
  748. def test_operator_object_left(o, op, type_):
  749. try:
  750. with recursionlimit(200):
  751. op(o, type_(1))
  752. except TypeError:
  753. pass
  754. @given(sampled_from(objecty_things),
  755. sampled_from(reasonable_operators_for_scalars),
  756. sampled_from(types))
  757. def test_operator_object_right(o, op, type_):
  758. try:
  759. with recursionlimit(200):
  760. op(type_(1), o)
  761. except TypeError:
  762. pass
  763. @given(sampled_from(reasonable_operators_for_scalars),
  764. sampled_from(types),
  765. sampled_from(types))
  766. def test_operator_scalars(op, type1, type2):
  767. try:
  768. op(type1(1), type2(1))
  769. except TypeError:
  770. pass
  771. @pytest.mark.parametrize("op", reasonable_operators_for_scalars)
  772. @pytest.mark.parametrize("val", [None, 2**64])
  773. def test_longdouble_inf_loop(op, val):
  774. # Note: The 2**64 value will pass once NEP 50 is adopted.
  775. try:
  776. op(np.longdouble(3), val)
  777. except TypeError:
  778. pass
  779. try:
  780. op(val, np.longdouble(3))
  781. except TypeError:
  782. pass
  783. @pytest.mark.parametrize("op", reasonable_operators_for_scalars)
  784. @pytest.mark.parametrize("val", [None, 2**64])
  785. def test_clongdouble_inf_loop(op, val):
  786. # Note: The 2**64 value will pass once NEP 50 is adopted.
  787. try:
  788. op(np.clongdouble(3), val)
  789. except TypeError:
  790. pass
  791. try:
  792. op(val, np.longdouble(3))
  793. except TypeError:
  794. pass
  795. @pytest.mark.parametrize("dtype", np.typecodes["AllInteger"])
  796. @pytest.mark.parametrize("operation", [
  797. lambda min, max: max + max,
  798. lambda min, max: min - max,
  799. lambda min, max: max * max], ids=["+", "-", "*"])
  800. def test_scalar_integer_operation_overflow(dtype, operation):
  801. st = np.dtype(dtype).type
  802. min = st(np.iinfo(dtype).min)
  803. max = st(np.iinfo(dtype).max)
  804. with pytest.warns(RuntimeWarning, match="overflow encountered"):
  805. operation(min, max)
  806. @pytest.mark.parametrize("dtype", np.typecodes["Integer"])
  807. @pytest.mark.parametrize("operation", [
  808. lambda min, neg_1: -min,
  809. lambda min, neg_1: abs(min),
  810. lambda min, neg_1: min * neg_1,
  811. pytest.param(lambda min, neg_1: min // neg_1,
  812. marks=pytest.mark.skip(reason="broken on some platforms"))],
  813. ids=["neg", "abs", "*", "//"])
  814. def test_scalar_signed_integer_overflow(dtype, operation):
  815. # The minimum signed integer can "overflow" for some additional operations
  816. st = np.dtype(dtype).type
  817. min = st(np.iinfo(dtype).min)
  818. neg_1 = st(-1)
  819. with pytest.warns(RuntimeWarning, match="overflow encountered"):
  820. operation(min, neg_1)
  821. @pytest.mark.parametrize("dtype", np.typecodes["UnsignedInteger"])
  822. def test_scalar_unsigned_integer_overflow(dtype):
  823. val = np.dtype(dtype).type(8)
  824. with pytest.warns(RuntimeWarning, match="overflow encountered"):
  825. -val
  826. zero = np.dtype(dtype).type(0)
  827. -zero # does not warn
  828. @pytest.mark.parametrize("dtype", np.typecodes["AllInteger"])
  829. @pytest.mark.parametrize("operation", [
  830. lambda val, zero: val // zero,
  831. lambda val, zero: val % zero, ], ids=["//", "%"])
  832. def test_scalar_integer_operation_divbyzero(dtype, operation):
  833. st = np.dtype(dtype).type
  834. val = st(100)
  835. zero = st(0)
  836. with pytest.warns(RuntimeWarning, match="divide by zero"):
  837. operation(val, zero)
  838. ops_with_names = [
  839. ("__lt__", "__gt__", operator.lt, True),
  840. ("__le__", "__ge__", operator.le, True),
  841. ("__eq__", "__eq__", operator.eq, True),
  842. # Note __op__ and __rop__ may be identical here:
  843. ("__ne__", "__ne__", operator.ne, True),
  844. ("__gt__", "__lt__", operator.gt, True),
  845. ("__ge__", "__le__", operator.ge, True),
  846. ("__floordiv__", "__rfloordiv__", operator.floordiv, False),
  847. ("__truediv__", "__rtruediv__", operator.truediv, False),
  848. ("__add__", "__radd__", operator.add, False),
  849. ("__mod__", "__rmod__", operator.mod, False),
  850. ("__mul__", "__rmul__", operator.mul, False),
  851. ("__pow__", "__rpow__", operator.pow, False),
  852. ("__sub__", "__rsub__", operator.sub, False),
  853. ]
  854. @pytest.mark.parametrize(["__op__", "__rop__", "op", "cmp"], ops_with_names)
  855. @pytest.mark.parametrize("sctype", [np.float32, np.float64, np.longdouble])
  856. def test_subclass_deferral(sctype, __op__, __rop__, op, cmp):
  857. """
  858. This test covers scalar subclass deferral. Note that this is exceedingly
  859. complicated, especially since it tends to fall back to the array paths and
  860. these additionally add the "array priority" mechanism.
  861. The behaviour was modified subtly in 1.22 (to make it closer to how Python
  862. scalars work). Due to its complexity and the fact that subclassing NumPy
  863. scalars is probably a bad idea to begin with. There is probably room
  864. for adjustments here.
  865. """
  866. class myf_simple1(sctype):
  867. pass
  868. class myf_simple2(sctype):
  869. pass
  870. def op_func(self, other):
  871. return __op__
  872. def rop_func(self, other):
  873. return __rop__
  874. myf_op = type("myf_op", (sctype,), {__op__: op_func, __rop__: rop_func})
  875. # inheritance has to override, or this is correctly lost:
  876. res = op(myf_simple1(1), myf_simple2(2))
  877. assert type(res) == sctype or type(res) == np.bool_
  878. assert op(myf_simple1(1), myf_simple2(2)) == op(1, 2) # inherited
  879. # Two independent subclasses do not really define an order. This could
  880. # be attempted, but we do not since Python's `int` does neither:
  881. assert op(myf_op(1), myf_simple1(2)) == __op__
  882. assert op(myf_simple1(1), myf_op(2)) == op(1, 2) # inherited
  883. def test_longdouble_complex():
  884. # Simple test to check longdouble and complex combinations, since these
  885. # need to go through promotion, which longdouble needs to be careful about.
  886. x = np.longdouble(1)
  887. assert x + 1j == 1+1j
  888. assert 1j + x == 1+1j
  889. @pytest.mark.parametrize(["__op__", "__rop__", "op", "cmp"], ops_with_names)
  890. @pytest.mark.parametrize("subtype", [float, int, complex, np.float16])
  891. @np._no_nep50_warning()
  892. def test_pyscalar_subclasses(subtype, __op__, __rop__, op, cmp):
  893. def op_func(self, other):
  894. return __op__
  895. def rop_func(self, other):
  896. return __rop__
  897. # Check that deferring is indicated using `__array_ufunc__`:
  898. myt = type("myt", (subtype,),
  899. {__op__: op_func, __rop__: rop_func, "__array_ufunc__": None})
  900. # Just like normally, we should never presume we can modify the float.
  901. assert op(myt(1), np.float64(2)) == __op__
  902. assert op(np.float64(1), myt(2)) == __rop__
  903. if op in {operator.mod, operator.floordiv} and subtype == complex:
  904. return # module is not support for complex. Do not test.
  905. if __rop__ == __op__:
  906. return
  907. # When no deferring is indicated, subclasses are handled normally.
  908. myt = type("myt", (subtype,), {__rop__: rop_func})
  909. # Check for float32, as a float subclass float64 may behave differently
  910. res = op(myt(1), np.float16(2))
  911. expected = op(subtype(1), np.float16(2))
  912. assert res == expected
  913. assert type(res) == type(expected)
  914. res = op(np.float32(2), myt(1))
  915. expected = op(np.float32(2), subtype(1))
  916. assert res == expected
  917. assert type(res) == type(expected)
  918. # Same check for longdouble:
  919. res = op(myt(1), np.longdouble(2))
  920. expected = op(subtype(1), np.longdouble(2))
  921. assert res == expected
  922. assert type(res) == type(expected)
  923. res = op(np.float32(2), myt(1))
  924. expected = op(np.longdouble(2), subtype(1))
  925. assert res == expected