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

Function tril

numpy/lib/_twodim_base_impl.py:482–534  ·  view source on GitHub ↗

Lower triangle of an array. Return a copy of an array with elements above the `k`-th diagonal zeroed. For arrays with ``ndim`` exceeding 2, `tril` will apply to the final two axes. Parameters ---------- m : array_like, shape (..., M, N) Input array. k : int

(m, k=0)

Source from the content-addressed store, hash-verified

480
481@array_function_dispatch(_trilu_dispatcher)
482def tril(m, k=0):
483 """
484 Lower triangle of an array.
485
486 Return a copy of an array with elements above the `k`-th diagonal zeroed.
487 For arrays with ``ndim`` exceeding 2, `tril` will apply to the final two
488 axes.
489
490 Parameters
491 ----------
492 m : array_like, shape (..., M, N)
493 Input array.
494 k : int, optional
495 Diagonal above which to zero elements. `k = 0` (the default) is the
496 main diagonal, `k < 0` is below it and `k > 0` is above.
497
498 Returns
499 -------
500 tril : ndarray, shape (..., M, N)
501 Lower triangle of `m`, of same shape and data-type as `m`.
502
503 See Also
504 --------
505 triu : same thing, only for the upper triangle
506
507 Examples
508 --------
509 >>> import numpy as np
510 >>> np.tril([[1,2,3],[4,5,6],[7,8,9],[10,11,12]], -1)
511 array([[ 0, 0, 0],
512 [ 4, 0, 0],
513 [ 7, 8, 0],
514 [10, 11, 12]])
515
516 >>> np.tril(np.arange(3*4*5).reshape(3, 4, 5))
517 array([[[ 0, 0, 0, 0, 0],
518 [ 5, 6, 0, 0, 0],
519 [10, 11, 12, 0, 0],
520 [15, 16, 17, 18, 0]],
521 [[20, 0, 0, 0, 0],
522 [25, 26, 0, 0, 0],
523 [30, 31, 32, 0, 0],
524 [35, 36, 37, 38, 0]],
525 [[40, 0, 0, 0, 0],
526 [45, 46, 0, 0, 0],
527 [50, 51, 52, 0, 0],
528 [55, 56, 57, 58, 0]]])
529
530 """
531 m = asanyarray(m)
532 mask = tri(*m.shape[-2:], k=k, dtype=bool)
533
534 return where(mask, m, zeros(1, m.dtype))
535
536
537@array_function_dispatch(_trilu_dispatcher)

Callers

nothing calls this directly

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…