amep.utils.upper_triangle#

amep.utils.upper_triangle(matrix: ndarray, diagonal: bool = False, indices: bool = False) ndarray#

Returns the upper diagonal of a quadratic matrix.

Parameters:
  • matrix (np.ndarray) – Matrix of shape (N,N).

  • diagonal (bool, optional) – If True, the diagonal elements are returned as well. The default is False.

  • indices (bool, optional) – If True, the indices of the matrix elements are returned as well. The default is False.

Returns:

Array of shape (M,) or (3,M) if indices is True.

Return type:

np.ndarray

Examples

>>> import amep
>>> import numpy as np
>>> m = np.array([[0,1,2],[3,4,5],[6,7,8]])
>>> print(m)
[[0 1 2]
 [3 4 5]
 [6 7 8]]
>>> m1 = amep.utils.upper_triangle(m, diagonal=False, indices=False)
>>> print(m1)
[1 2 5]
>>> m2 = amep.utils.upper_triangle(m, diagonal=True, indices=False)
>>> print(m2)
[0 1 2 4 5 8]
>>> m3 = amep.utils.upper_triangle(m, diagonal=False, indices=True)
>>> print(m3)
[[0 0 1]
 [1 2 2]
 [1 2 5]]
>>>