test_index_tricks.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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_impl import (
  8. mgrid, ogrid, ndenumerate, fill_diagonal, diag_indices, diag_indices_from,
  9. index_exp, ndindex, c_, 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_array_almost_equal(grid64, grid32)
  216. # At some point this was float64, but NEP 50 changed it:
  217. assert grid32.dtype == np.float32
  218. assert grid64.dtype == np.float64
  219. # different code path for single slice
  220. grid64 = mgrid[0.1:0.33:0.1]
  221. grid32 = mgrid[np.float32(0.1):np.float32(0.33):np.float32(0.1)]
  222. assert_(grid32.dtype == np.float64)
  223. assert_array_almost_equal(grid64, grid32)
  224. def test_accepts_longdouble(self):
  225. # regression tests for #16945
  226. grid64 = mgrid[0.1:0.33:0.1, ]
  227. grid128 = mgrid[
  228. np.longdouble(0.1):np.longdouble(0.33):np.longdouble(0.1),
  229. ]
  230. assert_(grid128.dtype == np.longdouble)
  231. assert_array_almost_equal(grid64, grid128)
  232. grid128c_a = mgrid[0:np.longdouble(1):3.4j]
  233. grid128c_b = mgrid[0:np.longdouble(1):3.4j, ]
  234. assert_(grid128c_a.dtype == grid128c_b.dtype == np.longdouble)
  235. assert_array_equal(grid128c_a, grid128c_b[0])
  236. # different code path for single slice
  237. grid64 = mgrid[0.1:0.33:0.1]
  238. grid128 = mgrid[
  239. np.longdouble(0.1):np.longdouble(0.33):np.longdouble(0.1)
  240. ]
  241. assert_(grid128.dtype == np.longdouble)
  242. assert_array_almost_equal(grid64, grid128)
  243. def test_accepts_npcomplexfloating(self):
  244. # Related to #16466
  245. assert_array_almost_equal(
  246. mgrid[0.1:0.3:3j, ], mgrid[0.1:0.3:np.complex64(3j), ]
  247. )
  248. # different code path for single slice
  249. assert_array_almost_equal(
  250. mgrid[0.1:0.3:3j], mgrid[0.1:0.3:np.complex64(3j)]
  251. )
  252. # Related to #16945
  253. grid64_a = mgrid[0.1:0.3:3.3j]
  254. grid64_b = mgrid[0.1:0.3:3.3j, ][0]
  255. assert_(grid64_a.dtype == grid64_b.dtype == np.float64)
  256. assert_array_equal(grid64_a, grid64_b)
  257. grid128_a = mgrid[0.1:0.3:np.clongdouble(3.3j)]
  258. grid128_b = mgrid[0.1:0.3:np.clongdouble(3.3j), ][0]
  259. assert_(grid128_a.dtype == grid128_b.dtype == np.longdouble)
  260. assert_array_equal(grid64_a, grid64_b)
  261. class TestConcatenator:
  262. def test_1d(self):
  263. assert_array_equal(r_[1, 2, 3, 4, 5, 6], np.array([1, 2, 3, 4, 5, 6]))
  264. b = np.ones(5)
  265. c = r_[b, 0, 0, b]
  266. assert_array_equal(c, [1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1])
  267. def test_mixed_type(self):
  268. g = r_[10.1, 1:10]
  269. assert_(g.dtype == 'f8')
  270. def test_more_mixed_type(self):
  271. g = r_[-10.1, np.array([1]), np.array([2, 3, 4]), 10.0]
  272. assert_(g.dtype == 'f8')
  273. def test_complex_step(self):
  274. # Regression test for #12262
  275. g = r_[0:36:100j]
  276. assert_(g.shape == (100,))
  277. # Related to #16466
  278. g = r_[0:36:np.complex64(100j)]
  279. assert_(g.shape == (100,))
  280. def test_2d(self):
  281. b = np.random.rand(5, 5)
  282. c = np.random.rand(5, 5)
  283. d = r_['1', b, c] # append columns
  284. assert_(d.shape == (5, 10))
  285. assert_array_equal(d[:, :5], b)
  286. assert_array_equal(d[:, 5:], c)
  287. d = r_[b, c]
  288. assert_(d.shape == (10, 5))
  289. assert_array_equal(d[:5, :], b)
  290. assert_array_equal(d[5:, :], c)
  291. def test_0d(self):
  292. assert_equal(r_[0, np.array(1), 2], [0, 1, 2])
  293. assert_equal(r_[[0, 1, 2], np.array(3)], [0, 1, 2, 3])
  294. assert_equal(r_[np.array(0), [1, 2, 3]], [0, 1, 2, 3])
  295. class TestNdenumerate:
  296. def test_basic(self):
  297. a = np.array([[1, 2], [3, 4]])
  298. assert_equal(list(ndenumerate(a)),
  299. [((0, 0), 1), ((0, 1), 2), ((1, 0), 3), ((1, 1), 4)])
  300. class TestIndexExpression:
  301. def test_regression_1(self):
  302. # ticket #1196
  303. a = np.arange(2)
  304. assert_equal(a[:-1], a[s_[:-1]])
  305. assert_equal(a[:-1], a[index_exp[:-1]])
  306. def test_simple_1(self):
  307. a = np.random.rand(4, 5, 6)
  308. assert_equal(a[:, :3, [1, 2]], a[index_exp[:, :3, [1, 2]]])
  309. assert_equal(a[:, :3, [1, 2]], a[s_[:, :3, [1, 2]]])
  310. class TestIx_:
  311. def test_regression_1(self):
  312. # Test empty untyped inputs create outputs of indexing type, gh-5804
  313. a, = np.ix_(range(0))
  314. assert_equal(a.dtype, np.intp)
  315. a, = np.ix_([])
  316. assert_equal(a.dtype, np.intp)
  317. # but if the type is specified, don't change it
  318. a, = np.ix_(np.array([], dtype=np.float32))
  319. assert_equal(a.dtype, np.float32)
  320. def test_shape_and_dtype(self):
  321. sizes = (4, 5, 3, 2)
  322. # Test both lists and arrays
  323. for func in (range, np.arange):
  324. arrays = np.ix_(*[func(sz) for sz in sizes])
  325. for k, (a, sz) in enumerate(zip(arrays, sizes)):
  326. assert_equal(a.shape[k], sz)
  327. assert_(all(sh == 1 for j, sh in enumerate(a.shape) if j != k))
  328. assert_(np.issubdtype(a.dtype, np.integer))
  329. def test_bool(self):
  330. bool_a = [True, False, True, True]
  331. int_a, = np.nonzero(bool_a)
  332. assert_equal(np.ix_(bool_a)[0], int_a)
  333. def test_1d_only(self):
  334. idx2d = [[1, 2, 3], [4, 5, 6]]
  335. assert_raises(ValueError, np.ix_, idx2d)
  336. def test_repeated_input(self):
  337. length_of_vector = 5
  338. x = np.arange(length_of_vector)
  339. out = ix_(x, x)
  340. assert_equal(out[0].shape, (length_of_vector, 1))
  341. assert_equal(out[1].shape, (1, length_of_vector))
  342. # check that input shape is not modified
  343. assert_equal(x.shape, (length_of_vector,))
  344. def test_c_():
  345. a = c_[np.array([[1, 2, 3]]), 0, 0, np.array([[4, 5, 6]])]
  346. assert_equal(a, [[1, 2, 3, 0, 0, 4, 5, 6]])
  347. class TestFillDiagonal:
  348. def test_basic(self):
  349. a = np.zeros((3, 3), int)
  350. fill_diagonal(a, 5)
  351. assert_array_equal(
  352. a, np.array([[5, 0, 0],
  353. [0, 5, 0],
  354. [0, 0, 5]])
  355. )
  356. def test_tall_matrix(self):
  357. a = np.zeros((10, 3), int)
  358. fill_diagonal(a, 5)
  359. assert_array_equal(
  360. a, np.array([[5, 0, 0],
  361. [0, 5, 0],
  362. [0, 0, 5],
  363. [0, 0, 0],
  364. [0, 0, 0],
  365. [0, 0, 0],
  366. [0, 0, 0],
  367. [0, 0, 0],
  368. [0, 0, 0],
  369. [0, 0, 0]])
  370. )
  371. def test_tall_matrix_wrap(self):
  372. a = np.zeros((10, 3), int)
  373. fill_diagonal(a, 5, True)
  374. assert_array_equal(
  375. a, np.array([[5, 0, 0],
  376. [0, 5, 0],
  377. [0, 0, 5],
  378. [0, 0, 0],
  379. [5, 0, 0],
  380. [0, 5, 0],
  381. [0, 0, 5],
  382. [0, 0, 0],
  383. [5, 0, 0],
  384. [0, 5, 0]])
  385. )
  386. def test_wide_matrix(self):
  387. a = np.zeros((3, 10), int)
  388. fill_diagonal(a, 5)
  389. assert_array_equal(
  390. a, np.array([[5, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  391. [0, 5, 0, 0, 0, 0, 0, 0, 0, 0],
  392. [0, 0, 5, 0, 0, 0, 0, 0, 0, 0]])
  393. )
  394. def test_operate_4d_array(self):
  395. a = np.zeros((3, 3, 3, 3), int)
  396. fill_diagonal(a, 4)
  397. i = np.array([0, 1, 2])
  398. assert_equal(np.where(a != 0), (i, i, i, i))
  399. def test_low_dim_handling(self):
  400. # raise error with low dimensionality
  401. a = np.zeros(3, int)
  402. with assert_raises_regex(ValueError, "at least 2-d"):
  403. fill_diagonal(a, 5)
  404. def test_hetero_shape_handling(self):
  405. # raise error with high dimensionality and
  406. # shape mismatch
  407. a = np.zeros((3,3,7,3), int)
  408. with assert_raises_regex(ValueError, "equal length"):
  409. fill_diagonal(a, 2)
  410. def test_diag_indices():
  411. di = diag_indices(4)
  412. a = np.array([[1, 2, 3, 4],
  413. [5, 6, 7, 8],
  414. [9, 10, 11, 12],
  415. [13, 14, 15, 16]])
  416. a[di] = 100
  417. assert_array_equal(
  418. a, np.array([[100, 2, 3, 4],
  419. [5, 100, 7, 8],
  420. [9, 10, 100, 12],
  421. [13, 14, 15, 100]])
  422. )
  423. # Now, we create indices to manipulate a 3-d array:
  424. d3 = diag_indices(2, 3)
  425. # And use it to set the diagonal of a zeros array to 1:
  426. a = np.zeros((2, 2, 2), int)
  427. a[d3] = 1
  428. assert_array_equal(
  429. a, np.array([[[1, 0],
  430. [0, 0]],
  431. [[0, 0],
  432. [0, 1]]])
  433. )
  434. class TestDiagIndicesFrom:
  435. def test_diag_indices_from(self):
  436. x = np.random.random((4, 4))
  437. r, c = diag_indices_from(x)
  438. assert_array_equal(r, np.arange(4))
  439. assert_array_equal(c, np.arange(4))
  440. def test_error_small_input(self):
  441. x = np.ones(7)
  442. with assert_raises_regex(ValueError, "at least 2-d"):
  443. diag_indices_from(x)
  444. def test_error_shape_mismatch(self):
  445. x = np.zeros((3, 3, 2, 3), int)
  446. with assert_raises_regex(ValueError, "equal length"):
  447. diag_indices_from(x)
  448. def test_ndindex():
  449. x = list(ndindex(1, 2, 3))
  450. expected = [ix for ix, e in ndenumerate(np.zeros((1, 2, 3)))]
  451. assert_array_equal(x, expected)
  452. x = list(ndindex((1, 2, 3)))
  453. assert_array_equal(x, expected)
  454. # Test use of scalars and tuples
  455. x = list(ndindex((3,)))
  456. assert_array_equal(x, list(ndindex(3)))
  457. # Make sure size argument is optional
  458. x = list(ndindex())
  459. assert_equal(x, [()])
  460. x = list(ndindex(()))
  461. assert_equal(x, [()])
  462. # Make sure 0-sized ndindex works correctly
  463. x = list(ndindex(*[0]))
  464. assert_equal(x, [])