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

Function tril

numpy/lib/twodim_base.py:431–482  ·  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

429
430@array_function_dispatch(_trilu_dispatcher)
431def tril(m, k=0):
432 """
433 Lower triangle of an array.
434
435 Return a copy of an array with elements above the `k`-th diagonal zeroed.
436 For arrays with ``ndim`` exceeding 2, `tril` will apply to the final two
437 axes.
438
439 Parameters
440 ----------
441 m : array_like, shape (..., M, N)
442 Input array.
443 k : int, optional
444 Diagonal above which to zero elements. `k = 0` (the default) is the
445 main diagonal, `k < 0` is below it and `k > 0` is above.
446
447 Returns
448 -------
449 tril : ndarray, shape (..., M, N)
450 Lower triangle of `m`, of same shape and data-type as `m`.
451
452 See Also
453 --------
454 triu : same thing, only for the upper triangle
455
456 Examples
457 --------
458 >>> np.tril([[1,2,3],[4,5,6],[7,8,9],[10,11,12]], -1)
459 array([[ 0, 0, 0],
460 [ 4, 0, 0],
461 [ 7, 8, 0],
462 [10, 11, 12]])
463
464 >>> np.tril(np.arange(3*4*5).reshape(3, 4, 5))
465 array([[[ 0, 0, 0, 0, 0],
466 [ 5, 6, 0, 0, 0],
467 [10, 11, 12, 0, 0],
468 [15, 16, 17, 18, 0]],
469 [[20, 0, 0, 0, 0],
470 [25, 26, 0, 0, 0],
471 [30, 31, 32, 0, 0],
472 [35, 36, 37, 38, 0]],
473 [[40, 0, 0, 0, 0],
474 [45, 46, 0, 0, 0],
475 [50, 51, 52, 0, 0],
476 [55, 56, 57, 58, 0]]])
477
478 """
479 m = asanyarray(m)
480 mask = tri(*m.shape[-2:], k=k, dtype=bool)
481
482 return where(mask, m, zeros(1, m.dtype))
483
484
485@array_function_dispatch(_trilu_dispatcher)

Callers

nothing calls this directly

Calls 4

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

Tested by

no test coverage detected