MCPcopy Create free account
hub / github.com/numpy/numpy / test_tril_indices

Function test_tril_indices

numpy/lib/tests/test_twodim_base.py:399–443  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

397
398
399def test_tril_indices():
400 # indices without and with offset
401 il1 = tril_indices(4)
402 il2 = tril_indices(4, k=2)
403 il3 = tril_indices(4, m=5)
404 il4 = tril_indices(4, k=2, m=5)
405
406 a = np.array([[1, 2, 3, 4],
407 [5, 6, 7, 8],
408 [9, 10, 11, 12],
409 [13, 14, 15, 16]])
410 b = np.arange(1, 21).reshape(4, 5)
411
412 # indexing:
413 assert_array_equal(a[il1],
414 array([1, 5, 6, 9, 10, 11, 13, 14, 15, 16]))
415 assert_array_equal(b[il3],
416 array([1, 6, 7, 11, 12, 13, 16, 17, 18, 19]))
417
418 # And for assigning values:
419 a[il1] = -1
420 assert_array_equal(a,
421 array([[-1, 2, 3, 4],
422 [-1, -1, 7, 8],
423 [-1, -1, -1, 12],
424 [-1, -1, -1, -1]]))
425 b[il3] = -1
426 assert_array_equal(b,
427 array([[-1, 2, 3, 4, 5],
428 [-1, -1, 8, 9, 10],
429 [-1, -1, -1, 14, 15],
430 [-1, -1, -1, -1, 20]]))
431 # These cover almost the whole array (two diagonals right of the main one):
432 a[il2] = -10
433 assert_array_equal(a,
434 array([[-10, -10, -10, 4],
435 [-10, -10, -10, -10],
436 [-10, -10, -10, -10],
437 [-10, -10, -10, -10]]))
438 b[il4] = -10
439 assert_array_equal(b,
440 array([[-10, -10, -10, 4, 5],
441 [-10, -10, -10, -10, 10],
442 [-10, -10, -10, -10, -10],
443 [-10, -10, -10, -10, -10]]))
444
445
446class TestTriuIndices:

Callers

nothing calls this directly

Calls 4

tril_indicesFunction · 0.90
assert_array_equalFunction · 0.90
arrayFunction · 0.90
reshapeMethod · 0.80

Tested by

no test coverage detected