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

Function tri

numpy/lib/twodim_base.py:368–420  ·  view source on GitHub ↗

An array with ones at and below the given diagonal and zeros elsewhere. Parameters ---------- N : int Number of rows in the array. M : int, optional Number of columns in the array. By default, `M` is taken equal to `N`. k : int, optional The

(N, M=None, k=0, dtype=float, *, like=None)

Source from the content-addressed store, hash-verified

366@set_array_function_like_doc
367@set_module('numpy')
368def tri(N, M=None, k=0, dtype=float, *, like=None):
369 """
370 An array with ones at and below the given diagonal and zeros elsewhere.
371
372 Parameters
373 ----------
374 N : int
375 Number of rows in the array.
376 M : int, optional
377 Number of columns in the array.
378 By default, `M` is taken equal to `N`.
379 k : int, optional
380 The sub-diagonal at and below which the array is filled.
381 `k` = 0 is the main diagonal, while `k` < 0 is below it,
382 and `k` > 0 is above. The default is 0.
383 dtype : dtype, optional
384 Data type of the returned array. The default is float.
385 ${ARRAY_FUNCTION_LIKE}
386
387 .. versionadded:: 1.20.0
388
389 Returns
390 -------
391 tri : ndarray of shape (N, M)
392 Array with its lower triangle filled with ones and zero elsewhere;
393 in other words ``T[i,j] == 1`` for ``j <= i + k``, 0 otherwise.
394
395 Examples
396 --------
397 >>> np.tri(3, 5, 2, dtype=int)
398 array([[1, 1, 1, 0, 0],
399 [1, 1, 1, 1, 0],
400 [1, 1, 1, 1, 1]])
401
402 >>> np.tri(3, 5, -1)
403 array([[0., 0., 0., 0., 0.],
404 [1., 0., 0., 0., 0.],
405 [1., 1., 0., 0., 0.]])
406
407 """
408 if like is not None:
409 return _tri_with_like(like, N, M=M, k=k, dtype=dtype)
410
411 if M is None:
412 M = N
413
414 m = greater_equal.outer(arange(N, dtype=_min_int(0, N)),
415 arange(-k, M-k, dtype=_min_int(-k, M - k)))
416
417 # Avoid making a copy if the requested type is already bool
418 m = m.astype(dtype, copy=False)
419
420 return m
421
422
423_tri_with_like = array_function_dispatch()(tri)

Callers 5

test_dtypeMethod · 0.90
trilFunction · 0.85
triuFunction · 0.85
tril_indicesFunction · 0.85
triu_indicesFunction · 0.85

Calls 4

arangeFunction · 0.85
_min_intFunction · 0.85
astypeMethod · 0.80
outerMethod · 0.45

Tested by 1

test_dtypeMethod · 0.72