Upper triangle of an array. Return a copy of an array with the elements below the `k`-th diagonal zeroed. For arrays with ``ndim`` exceeding 2, `triu` will apply to the final two axes. Please refer to the documentation for `tril` for further details. See Also --------
(m, k=0)
| 536 | |
| 537 | @array_function_dispatch(_trilu_dispatcher) |
| 538 | def triu(m, k=0): |
| 539 | """ |
| 540 | Upper triangle of an array. |
| 541 | |
| 542 | Return a copy of an array with the elements below the `k`-th diagonal |
| 543 | zeroed. For arrays with ``ndim`` exceeding 2, `triu` will apply to the |
| 544 | final two axes. |
| 545 | |
| 546 | Please refer to the documentation for `tril` for further details. |
| 547 | |
| 548 | See Also |
| 549 | -------- |
| 550 | tril : lower triangle of an array |
| 551 | |
| 552 | Examples |
| 553 | -------- |
| 554 | >>> import numpy as np |
| 555 | >>> np.triu([[1,2,3],[4,5,6],[7,8,9],[10,11,12]], -1) |
| 556 | array([[ 1, 2, 3], |
| 557 | [ 4, 5, 6], |
| 558 | [ 0, 8, 9], |
| 559 | [ 0, 0, 12]]) |
| 560 | |
| 561 | >>> np.triu(np.arange(3*4*5).reshape(3, 4, 5)) |
| 562 | array([[[ 0, 1, 2, 3, 4], |
| 563 | [ 0, 6, 7, 8, 9], |
| 564 | [ 0, 0, 12, 13, 14], |
| 565 | [ 0, 0, 0, 18, 19]], |
| 566 | [[20, 21, 22, 23, 24], |
| 567 | [ 0, 26, 27, 28, 29], |
| 568 | [ 0, 0, 32, 33, 34], |
| 569 | [ 0, 0, 0, 38, 39]], |
| 570 | [[40, 41, 42, 43, 44], |
| 571 | [ 0, 46, 47, 48, 49], |
| 572 | [ 0, 0, 52, 53, 54], |
| 573 | [ 0, 0, 0, 58, 59]]]) |
| 574 | |
| 575 | """ |
| 576 | m = asanyarray(m) |
| 577 | mask = tri(*m.shape[-2:], k=k - 1, dtype=bool) |
| 578 | |
| 579 | return where(mask, zeros(1, m.dtype), m) |
| 580 | |
| 581 | |
| 582 | def _vander_dispatcher(x, N=None, increasing=None): |
no test coverage detected
searching dependent graphs…