test_index_tricks.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. import pytest
  2. import numpy as np
  3. from numpy.testing import (
  4. assert_, assert_equal, assert_array_equal, assert_almost_equal,
  5. assert_array_almost_equal, assert_raises, assert_raises_regex,
  6. )
  7. from numpy.lib.index_tricks import (
  8. mgrid, ogrid, ndenumerate, fill_diagonal, diag_indices, diag_indices_from,
  9. index_exp, ndindex, r_, s_, ix_
  10. )
  11. class TestRavelUnravelIndex:
  12. def test_basic(self):
  13. assert_equal(np.unravel_index(2, (2, 2)), (1, 0))
  14. # test that new shape argument works properly
  15. assert_equal(np.unravel_index(indices=2,
  16. shape=(2, 2)),
  17. (1, 0))
  18. # test that an invalid second keyword argument
  19. # is properly handled, including the old name `dims`.
  20. with assert_raises(TypeError):
  21. np.unravel_index(indices=2, hape=(2, 2))
  22. with assert_raises(TypeError):
  23. np.unravel_index(2, hape=(2, 2))
  24. with assert_raises(TypeError):
  25. np.unravel_index(254, ims=(17, 94))
  26. with assert_raises(TypeError):
  27. np.unravel_index(254, dims=(17, 94))
  28. assert_equal(np.ravel_multi_index((1, 0), (2, 2)), 2)
  29. assert_equal(np.unravel_index(254, (17, 94)), (2, 66))
  30. assert_equal(np.ravel_multi_index((2, 66), (17, 94)), 254)
  31. assert_raises(ValueError, np.unravel_index, -1, (2, 2))
  32. assert_raises(TypeError, np.unravel_index, 0.5, (2, 2))
  33. assert_raises(ValueError, np.unravel_index, 4, (2, 2))
  34. assert_raises(ValueError, np.ravel_multi_index, (-3, 1), (2, 2))
  35. assert_raises(ValueError, np.ravel_multi_index, (2, 1), (2, 2))
  36. assert_raises(ValueError, np.ravel_multi_index, (0, -3), (2, 2))
  37. assert_raises(ValueError, np.ravel_multi_index, (0, 2), (2, 2))
  38. assert_raises(TypeError, np.ravel_multi_index, (0.1, 0.), (2, 2))
  39. assert_equal(np.unravel_index((2*3 + 1)*6 + 4, (4, 3, 6)), [2, 1, 4])
  40. assert_equal(
  41. np.ravel_multi_index([2, 1, 4], (4, 3, 6)), (2*3 + 1)*6 + 4)
  42. arr = np.array([[3, 6, 6], [4, 5, 1]])
  43. assert_equal(np.ravel_multi_index(arr, (7, 6)), [22, 41, 37])
  44. assert_equal(
  45. np.ravel_multi_index(arr, (7, 6), order='F'), [31, 41, 13])
  46. assert_equal(
  47. np.ravel_multi_index(arr, (4, 6), mode='clip'), [22, 23, 19])
  48. assert_equal(np.ravel_multi_index(arr, (4, 4), mode=('clip', 'wrap')),
  49. [12, 13, 13])
  50. assert_equal(np.ravel_multi_index((3, 1, 4, 1), (6, 7, 8, 9)), 1621)
  51. assert_equal(np.unravel_index(np.array([22, 41, 37]), (7, 6)),
  52. [[3, 6, 6], [4, 5, 1]])
  53. assert_equal(
  54. np.unravel_index(np.array([31, 41, 13]), (7, 6), order='F'),
  55. [[3, 6, 6], [4, 5, 1]])
  56. assert_equal(np.unravel_index(1621, (6, 7, 8, 9)), [3, 1, 4, 1])
  57. def test_empty_indices(self):
  58. msg1 = 'indices must be integral: the provided empty sequence was'
  59. msg2 = 'only int indices permitted'
  60. assert_raises_regex(TypeError, msg1, np.unravel_index, [], (10, 3, 5))
  61. assert_raises_regex(TypeError, msg1, np.unravel_index, (), (10, 3, 5))
  62. assert_raises_regex(TypeError, msg2, np.unravel_index, np.array([]),
  63. (10, 3, 5))
  64. assert_equal(np.unravel_index(np.array([],dtype=int), (10, 3, 5)),
  65. [[], [], []])
  66. assert_raises_regex(TypeError, msg1, np.ravel_multi_index, ([], []),
  67. (10, 3))
  68. assert_raises_regex(TypeError, msg1, np.ravel_multi_index, ([], ['abc']),
  69. (10, 3))
  70. assert_raises_regex(TypeError, msg2, np.ravel_multi_index,
  71. (np.array([]), np.array([])), (5, 3))
  72. assert_equal(np.ravel_multi_index(
  73. (np.array([], dtype=int), np.array([], dtype=int)), (5, 3)), [])
  74. assert_equal(np.ravel_multi_index(np.array([[], []], dtype=int),
  75. (5, 3)), [])
  76. def test_big_indices(self):
  77. # ravel_multi_index for big indices (issue #7546)
  78. if np.intp == np.int64:
  79. arr = ([1, 29], [3, 5], [3, 117], [19, 2],
  80. [2379, 1284], [2, 2], [0, 1])
  81. assert_equal(
  82. np.ravel_multi_index(arr, (41, 7, 120, 36, 2706, 8, 6)),
  83. [5627771580, 117259570957])
  84. # test unravel_index for big indices (issue #9538)
  85. assert_raises(ValueError, np.unravel_index, 1, (2**32-1, 2**31+1))
  86. # test overflow checking for too big array (issue #7546)
  87. dummy_arr = ([0],[0])
  88. half_max = np.iinfo(np.intp).max // 2
  89. assert_equal(
  90. np.ravel_multi_index(dummy_arr, (half_max, 2)), [0])
  91. assert_raises(ValueError,
  92. np.ravel_multi_index, dummy_arr, (half_max+1, 2))
  93. assert_equal(
  94. np.ravel_multi_index(dummy_arr, (half_max, 2), order='F'), [0])
  95. assert_raises(ValueError,
  96. np.ravel_multi_index, dummy_arr, (half_max+1, 2), order='F')
  97. def test_dtypes(self):
  98. # Test with different data types
  99. for dtype in [np.int16, np.uint16, np.int32,
  100. np.uint32, np.int64, np.uint64]:
  101. coords = np.array(
  102. [[1, 0, 1, 2, 3, 4], [1, 6, 1, 3, 2, 0]], dtype=dtype)
  103. shape = (5, 8)
  104. uncoords = 8*coords[0]+coords[1]
  105. assert_equal(np.ravel_multi_index(coords, shape), uncoords)
  106. assert_equal(coords, np.unravel_index(uncoords, shape))
  107. uncoords = coords[0]+5*coords[1]
  108. assert_equal(
  109. np.ravel_multi_index(coords, shape, order='F'), uncoords)
  110. assert_equal(coords, np.unravel_index(uncoords, shape, order='F'))
  111. coords = np.array(
  112. [[1, 0, 1, 2, 3, 4], [1, 6, 1, 3, 2, 0], [1, 3, 1, 0, 9, 5]],
  113. dtype=dtype)
  114. shape = (5, 8, 10)
  115. uncoords = 10*(8*coords[0]+coords[1])+coords[2]
  116. assert_equal(np.ravel_multi_index(coords, shape), uncoords)
  117. assert_equal(coords, np.unravel_index(uncoords, shape))
  118. uncoords = coords[0]+5*(coords[1]+8*coords[2])
  119. assert_equal(
  120. np.ravel_multi_index(coords, shape, order='F'), uncoords)
  121. assert_equal(coords, np.unravel_index(uncoords, shape, order='F'))
  122. def test_clipmodes(self):
  123. # Test clipmodes
  124. assert_equal(
  125. np.ravel_multi_index([5, 1, -1, 2], (4, 3, 7, 12), mode='wrap'),
  126. np.ravel_multi_index([1, 1, 6, 2], (4, 3, 7, 12)))
  127. assert_equal(np.ravel_multi_index([5, 1, -1, 2], (4, 3, 7, 12),
  128. mode=(
  129. 'wrap', 'raise', 'clip', 'raise')),
  130. np.ravel_multi_index([1, 1, 0, 2], (4, 3, 7, 12)))
  131. assert_raises(
  132. ValueError, np.ravel_multi_index, [5, 1, -1, 2], (4, 3, 7, 12))
  133. def test_writeability(self):
  134. # See gh-7269
  135. x, y = np.unravel_index([1, 2, 3], (4, 5))
  136. assert_(x.flags.writeable)
  137. assert_(y.flags.writeable)
  138. def test_0d(self):
  139. # gh-580
  140. x = np.unravel_index(0, ())
  141. assert_equal(x, ())
  142. assert_raises_regex(ValueError, "0d array", np.unravel_index, [0], ())
  143. assert_raises_regex(
  144. ValueError, "out of bounds", np.unravel_index, [1], ())
  145. @pytest.mark.parametrize("mode", ["clip", "wrap", "raise"])
  146. def test_empty_array_ravel(self, mode):
  147. res = np.ravel_multi_index(
  148. np.zeros((3, 0), dtype=np.intp), (2, 1, 0), mode=mode)
  149. assert(res.shape == (0,))
  150. with assert_raises(ValueError):
  151. np.ravel_multi_index(
  152. np.zeros((3, 1), dtype=np.intp), (2, 1, 0), mode=mode)
  153. def test_empty_array_unravel(self):
  154. res = np.unravel_index(np.zeros(0, dtype=np.intp), (2, 1, 0))
  155. # res is a tuple of three empty arrays
  156. assert(len(res) == 3)
  157. assert(all(a.shape == (0,) for a in res))
  158. with assert_raises(ValueError):
  159. np.unravel_index([1], (2, 1, 0))
  160. class TestGrid:
  161. def test_basic(self):
  162. a = mgrid[-1:1:10j]
  163. b = mgrid[-1:1:0.1]
  164. assert_(a.shape == (10,))
  165. assert_(b.shape == (20,))
  166. assert_(a[0] == -1)
  167. assert_almost_equal(a[-1], 1)
  168. assert_(b[0] == -1)
  169. assert_almost_equal(b[1]-b[0], 0.1, 11)
  170. assert_almost_equal(b[-1], b[0]+19*0.1, 11)
  171. assert_almost_equal(a[1]-a[0], 2.0/9.0, 11)
  172. def test_linspace_equivalence(self):
  173. y, st = np.linspace(2, 10, retstep=True)
  174. assert_almost_equal(st, 8/49.0)
  175. assert_array_almost_equal(y, mgrid[2:10:50j], 13)
  176. def test_nd(self):
  177. c = mgrid[-1:1:10j, -2:2:10j]
  178. d = mgrid[-1:1:0.1, -2:2:0.2]
  179. assert_(c.shape == (2, 10, 10))
  180. assert_(d.shape == (2, 20, 20))
  181. assert_array_equal(c[0][0, :], -np.ones(10, 'd'))
  182. assert_array_equal(c[1][:, 0], -2*np.ones(10, 'd'))
  183. assert_array_almost_equal(c[0][-1, :], np.ones(10, 'd'), 11)
  184. assert_array_almost_equal(c[1][:, -1], 2*np.ones(10, 'd'), 11)
  185. assert_array_almost_equal(d[0, 1, :] - d[0, 0, :],
  186. 0.1*np.ones(20, 'd'), 11)
  187. assert_array_almost_equal(d[1, :, 1] - d[1, :, 0],
  188. 0.2*np.ones(20, 'd'), 11)
  189. def test_sparse(self):
  190. grid_full = mgrid[-1:1:10j, -2:2:10j]
  191. grid_sparse = ogrid[-1:1:10j, -2:2:10j]
  192. # sparse grids can be made dense by broadcasting
  193. grid_broadcast = np.broadcast_arrays(*grid_sparse)
  194. for f, b in zip(grid_full, grid_broadcast):
  195. assert_equal(f, b)
  196. @pytest.mark.parametrize("start, stop, step, expected", [
  197. (None, 10, 10j, (200, 10)),
  198. (-10, 20, None, (1800, 30)),
  199. ])
  200. def test_mgrid_size_none_handling(self, start, stop, step, expected):
  201. # regression test None value handling for
  202. # start and step values used by mgrid;
  203. # internally, this aims to cover previously
  204. # unexplored code paths in nd_grid()
  205. grid = mgrid[start:stop:step, start:stop:step]
  206. # need a smaller grid to explore one of the
  207. # untested code paths
  208. grid_small = mgrid[start:stop:step]
  209. assert_equal(grid.size, expected[0])
  210. assert_equal(grid_small.size, expected[1])
  211. def test_accepts_npfloating(self):
  212. # regression test for #16466
  213. grid64 = mgrid[0.1:0.33:0.1, ]
  214. grid32 = mgrid[np.float32(0.1):np.float32(0.33):np.float32(0.1), ]
  215. assert_(grid32.dtype == np.float64)
  216. assert_array_almost_equal(grid64, grid32)
  217. # different code path for single slice
  218. grid64 = mgrid[0.1:0.33:0.1]
  219. grid32 = mgrid[np.float32(0.1):np.float32(0.33):np.float32(0.1)]
  220. assert_(grid32.dtype == np.float64)
  221. assert_array_almost_equal(grid64, grid32)
  222. def test_accepts_longdouble(self):
  223. # regression tests for #16945
  224. grid64 = mgrid[0.1:0.33:0.1, ]
  225. grid128 = mgrid[
  226. np.longdouble(0.1):np.longdouble(0.33):np.longdouble(0.1),
  227. ]
  228. assert_(grid128.dtype == np.longdouble)
  229. assert_array_almost_equal(grid64, grid128)
  230. grid128c_a = mgrid[0:np.longdouble(1):3.4j]
  231. grid128c_b = mgrid[0:np.longdouble(1):3.4j, ]
  232. assert_(grid128c_a.dtype == grid128c_b.dtype == np.longdouble)
  233. assert_array_equal(grid128c_a, grid128c_b[0])
  234. # different code path for single slice
  235. grid64 = mgrid[0.1:0.33:0.1]
  236. grid128 = mgrid[
  237. np.longdouble(0.1):np.longdouble(0.33):np.longdouble(0.1)
  238. ]
  239. assert_(grid128.dtype == np.longdouble)
  240. assert_array_almost_equal(grid64, grid128)
  241. def test_accepts_npcomplexfloating(self):
  242. # Related to #16466
  243. assert_array_almost_equal(
  244. mgrid[0.1:0.3:3j, ], mgrid[0.1:0.3:np.complex64(3j), ]
  245. )
  246. # different code path for single slice
  247. assert_array_almost_equal(
  248. mgrid[0.1:0.3:3j], mgrid[0.1:0.3:np.complex64(3j)]
  249. )
  250. # Related to #16945
  251. grid64_a = mgrid[0.1:0.3:3.3j]
  252. grid64_b = mgrid[0.1:0.3:3.3j, ][0]
  253. assert_(grid64_a.dtype == grid64_b.dtype == np.float64)
  254. assert_array_equal(grid64_a, grid64_b)
  255. grid128_a = mgrid[0.1:0.3:np.clongdouble(3.3j)]
  256. grid128_b = mgrid[0.1:0.3:np.clongdouble(3.3j), ][0]
  257. assert_(grid128_a.dtype == grid128_b.dtype == np.longdouble)
  258. assert_array_equal(grid64_a, grid64_b)
  259. class TestConcatenator:
  260. def test_1d(self):
  261. assert_array_equal(r_[1, 2, 3, 4, 5, 6], np.array([1, 2, 3, 4, 5, 6]))
  262. b = np.ones(5)
  263. c = r_[b, 0, 0, b]
  264. assert_array_equal(c, [1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1])
  265. def test_mixed_type(self):
  266. g = r_[10.1, 1:10]
  267. assert_(g.dtype == 'f8')
  268. def test_more_mixed_type(self):
  269. g = r_[-10.1, np.array([1]), np.array([2, 3, 4]), 10.0]
  270. assert_(g.dtype == 'f8')
  271. def test_complex_step(self):
  272. # Regression test for #12262
  273. g = r_[0:36:100j]
  274. assert_(g.shape == (100,))
  275. # Related to #16466
  276. g = r_[0:36:np.complex64(100j)]
  277. assert_(g.shape == (100,))
  278. def test_2d(self):
  279. b = np.random.rand(5, 5)
  280. c = np.random.rand(5, 5)
  281. d = r_['1', b, c] # append columns
  282. assert_(d.shape == (5, 10))
  283. assert_array_equal(d[:, :5], b)
  284. assert_array_equal(d[:, 5:], c)
  285. d = r_[b, c]
  286. assert_(d.shape == (10, 5))
  287. assert_array_equal(d[:5, :], b)
  288. assert_array_equal(d[5:, :], c)
  289. def test_0d(self):
  290. assert_equal(r_[0, np.array(1), 2], [0, 1, 2])
  291. assert_equal(r_[[0, 1, 2], np.array(3)], [0, 1, 2, 3])
  292. assert_equal(r_[np.array(0), [1, 2, 3]], [0, 1, 2, 3])
  293. class TestNdenumerate:
  294. def test_basic(self):
  295. a = np.array([[1, 2], [3, 4]])
  296. assert_equal(list(ndenumerate(a)),
  297. [((0, 0), 1), ((0, 1), 2), ((1, 0), 3), ((1, 1), 4)])
  298. class TestIndexExpression:
  299. def test_regression_1(self):
  300. # ticket #1196
  301. a = np.arange(2)
  302. assert_equal(a[:-1], a[s_[:-1]])
  303. assert_equal(a[:-1], a[index_exp[:-1]])
  304. def test_simple_1(self):
  305. a = np.random.rand(4, 5, 6)
  306. assert_equal(a[:, :3, [1, 2]], a[index_exp[:, :3, [1, 2]]])
  307. assert_equal(a[:, :3, [1, 2]], a[s_[:, :3, [1, 2]]])
  308. class TestIx_:
  309. def test_regression_1(self):
  310. # Test empty untyped inputs create outputs of indexing type, gh-5804
  311. a, = np.ix_(range(0))
  312. assert_equal(a.dtype, np.intp)
  313. a, = np.ix_([])
  314. assert_equal(a.dtype, np.intp)
  315. # but if the type is specified, don't change it
  316. a, = np.ix_(np.array([], dtype=np.float32))
  317. assert_equal(a.dtype, np.float32)
  318. def test_shape_and_dtype(self):
  319. sizes = (4, 5, 3, 2)
  320. # Test both lists and arrays
  321. for func in (range, np.arange):
  322. arrays = np.ix_(*[func(sz) for sz in sizes])
  323. for k, (a, sz) in enumerate(zip(arrays, sizes)):
  324. assert_equal(a.shape[k], sz)
  325. assert_(all(sh == 1 for j, sh in enumerate(a.shape) if j != k))
  326. assert_(np.issubdtype(a.dtype, np.integer))
  327. def test_bool(self):
  328. bool_a = [True, False, True, True]
  329. int_a, = np.nonzero(bool_a)
  330. assert_equal(np.ix_(bool_a)[0], int_a)
  331. def test_1d_only(self):
  332. idx2d = [[1, 2, 3], [4, 5, 6]]
  333. assert_raises(ValueError, np.ix_, idx2d)
  334. def test_repeated_input(self):
  335. length_of_vector = 5
  336. x = np.arange(length_of_vector)
  337. out = ix_(x, x)
  338. assert_equal(out[0].shape, (length_of_vector, 1))
  339. assert_equal(out[1].shape, (1, length_of_vector))
  340. # check that input shape is not modified
  341. assert_equal(x.shape, (length_of_vector,))
  342. def test_c_():
  343. a = np.c_[np.array([[1, 2, 3]]), 0, 0, np.array([[4, 5, 6]])]
  344. assert_equal(a, [[1, 2, 3, 0, 0, 4, 5, 6]])
  345. class TestFillDiagonal:
  346. def test_basic(self):
  347. a = np.zeros((3, 3), int)
  348. fill_diagonal(a, 5)
  349. assert_array_equal(
  350. a, np.array([[5, 0, 0],
  351. [0, 5, 0],
  352. [0, 0, 5]])
  353. )
  354. def test_tall_matrix(self):
  355. a = np.zeros((10, 3), int)
  356. fill_diagonal(a, 5)
  357. assert_array_equal(
  358. a, np.array([[5, 0, 0],
  359. [0, 5, 0],
  360. [0, 0, 5],
  361. [0, 0, 0],
  362. [0, 0, 0],
  363. [0, 0, 0],
  364. [0, 0, 0],
  365. [0, 0, 0],
  366. [0, 0, 0],
  367. [0, 0, 0]])
  368. )
  369. def test_tall_matrix_wrap(self):
  370. a = np.zeros((10, 3), int)
  371. fill_diagonal(a, 5, True)
  372. assert_array_equal(
  373. a, np.array([[5, 0, 0],
  374. [0, 5, 0],
  375. [0, 0, 5],
  376. [0, 0, 0],
  377. [5, 0, 0],
  378. [0, 5, 0],
  379. [0, 0, 5],
  380. [0, 0, 0],
  381. [5, 0, 0],
  382. [0, 5, 0]])
  383. )
  384. def test_wide_matrix(self):
  385. a = np.zeros((3, 10), int)
  386. fill_diagonal(a, 5)
  387. assert_array_equal(
  388. a, np.array([[5, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  389. [0, 5, 0, 0, 0, 0, 0, 0, 0, 0],
  390. [0, 0, 5, 0, 0, 0, 0, 0, 0, 0]])
  391. )
  392. def test_operate_4d_array(self):
  393. a = np.zeros((3, 3, 3, 3), int)
  394. fill_diagonal(a, 4)
  395. i = np.array([0, 1, 2])
  396. assert_equal(np.where(a != 0), (i, i, i, i))
  397. def test_low_dim_handling(self):
  398. # raise error with low dimensionality
  399. a = np.zeros(3, int)
  400. with assert_raises_regex(ValueError, "at least 2-d"):
  401. fill_diagonal(a, 5)
  402. def test_hetero_shape_handling(self):
  403. # raise error with high dimensionality and
  404. # shape mismatch
  405. a = np.zeros((3,3,7,3), int)
  406. with assert_raises_regex(ValueError, "equal length"):
  407. fill_diagonal(a, 2)
  408. def test_diag_indices():
  409. di = diag_indices(4)
  410. a = np.array([[1, 2, 3, 4],
  411. [5, 6, 7, 8],
  412. [9, 10, 11, 12],
  413. [13, 14, 15, 16]])
  414. a[di] = 100
  415. assert_array_equal(
  416. a, np.array([[100, 2, 3, 4],
  417. [5, 100, 7, 8],
  418. [9, 10, 100, 12],
  419. [13, 14, 15, 100]])
  420. )
  421. # Now, we create indices to manipulate a 3-d array:
  422. d3 = diag_indices(2, 3)
  423. # And use it to set the diagonal of a zeros array to 1:
  424. a = np.zeros((2, 2, 2), int)
  425. a[d3] = 1
  426. assert_array_equal(
  427. a, np.array([[[1, 0],
  428. [0, 0]],
  429. [[0, 0],
  430. [0, 1]]])
  431. )
  432. class TestDiagIndicesFrom:
  433. def test_diag_indices_from(self):
  434. x = np.random.random((4, 4))
  435. r, c = diag_indices_from(x)
  436. assert_array_equal(r, np.arange(4))
  437. assert_array_equal(c, np.arange(4))
  438. def test_error_small_input(self):
  439. x = np.ones(7)
  440. with assert_raises_regex(ValueError, "at least 2-d"):
  441. diag_indices_from(x)
  442. def test_error_shape_mismatch(self):
  443. x = np.zeros((3, 3, 2, 3), int)
  444. with assert_raises_regex(ValueError, "equal length"):
  445. diag_indices_from(x)
  446. def test_ndindex():
  447. x = list(ndindex(1, 2, 3))
  448. expected = [ix for ix, e in ndenumerate(np.zeros((1, 2, 3)))]
  449. assert_array_equal(x, expected)
  450. x = list(ndindex((1, 2, 3)))
  451. assert_array_equal(x, expected)
  452. # Test use of scalars and tuples
  453. x = list(ndindex((3,)))
  454. assert_array_equal(x, list(ndindex(3)))
  455. # Make sure size argument is optional
  456. x = list(ndindex())
  457. assert_equal(x, [()])
  458. x = list(ndindex(()))
  459. assert_equal(x, [()])
  460. # Make sure 0-sized ndindex works correctly
  461. x = list(ndindex(*[0]))
  462. assert_equal(x, [])