跳到内容

triu

返回一个将低于第 k 条对角线的所有元素设置为零的数组。

参数

名称 类型 描述 默认值
x COO

输入数组。

必需
k int

指定将低于其的元素设置为零的对角线。默认值为零,对应于主对角线。

0

返回值

类型 描述
COO

输出的上三角矩阵。

引发

类型 描述
ValueError

如果 x 不包含零填充值。

另请参阅
源代码位于 sparse/numba_backend/_coo/common.py
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
def triu(x, k=0):
    """
    Returns an array with all elements below the k-th diagonal set to zero.

    Parameters
    ----------
    x : COO
        The input array.
    k : int, optional
        The diagonal below which elements are set to zero. The default is
        zero, which corresponds to the main diagonal.

    Returns
    -------
    COO
        The output upper-triangular matrix.

    Raises
    ------
    ValueError
        If `x` doesn't have zero fill-values.

    See Also
    --------
    - [`numpy.triu`][] : NumPy equivalent function
    """
    from .core import COO

    check_zero_fill_value(x)

    if not x.ndim >= 2:
        raise NotImplementedError("sparse.triu is not implemented for scalars or 1-D arrays.")

    mask = x.coords[-2] + k <= x.coords[-1]

    coords = x.coords[:, mask]
    data = x.data[mask]

    return COO(coords, data, shape=x.shape, has_duplicates=False, sorted=True)