MCPcopy Index your code
hub / github.com/numpy/numpy / triu

Function triu

numpy/lib/_twodim_base_impl.py:538–579  ·  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

536
537@array_function_dispatch(_trilu_dispatcher)
538def 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
582def _vander_dispatcher(x, N=None, increasing=None):

Callers 1

qrFunction · 0.90

Calls 4

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

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…