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

Function triu

numpy/lib/twodim_base.py:486–526  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

484
485@array_function_dispatch(_trilu_dispatcher)
486def triu(m, k=0):
487 """
488 Upper triangle of an array.
489
490 Return a copy of an array with the elements below the `k`-th diagonal
491 zeroed. For arrays with ``ndim`` exceeding 2, `triu` will apply to the
492 final two axes.
493
494 Please refer to the documentation for `tril` for further details.
495
496 See Also
497 --------
498 tril : lower triangle of an array
499
500 Examples
501 --------
502 >>> np.triu([[1,2,3],[4,5,6],[7,8,9],[10,11,12]], -1)
503 array([[ 1, 2, 3],
504 [ 4, 5, 6],
505 [ 0, 8, 9],
506 [ 0, 0, 12]])
507
508 >>> np.triu(np.arange(3*4*5).reshape(3, 4, 5))
509 array([[[ 0, 1, 2, 3, 4],
510 [ 0, 6, 7, 8, 9],
511 [ 0, 0, 12, 13, 14],
512 [ 0, 0, 0, 18, 19]],
513 [[20, 21, 22, 23, 24],
514 [ 0, 26, 27, 28, 29],
515 [ 0, 0, 32, 33, 34],
516 [ 0, 0, 0, 38, 39]],
517 [[40, 41, 42, 43, 44],
518 [ 0, 46, 47, 48, 49],
519 [ 0, 0, 52, 53, 54],
520 [ 0, 0, 0, 58, 59]]])
521
522 """
523 m = asanyarray(m)
524 mask = tri(*m.shape[-2:], k=k-1, dtype=bool)
525
526 return where(mask, zeros(1, m.dtype), m)
527
528
529def _vander_dispatcher(x, N=None, increasing=None):

Callers 1

qrFunction · 0.90

Calls 4

asanyarrayFunction · 0.85
triFunction · 0.85
whereFunction · 0.50
zerosFunction · 0.50

Tested by

no test coverage detected