Skip to content

Proposal: add support for returning upper and lower triangles of an array #237

Closed

Description

@kgryte

Proposal

Add APIs for returning the upper (triu) and lower (tril) triangles of an array.

Motivation

Currently, the array API specification does not provide APIs for returning the upper and lower triangles of an array.

These APIs were identified as potential candidates in gh-187 for inclusion in the next version of the standard. However, depending on the outcome of gh-217 may be necessary to include in the current version in order to ensure users can maintain equivalent functionality.

Background

triu and tril APIs are common across array libraries (see here). The notable exception being TensorFlow. MXNet seems to have APIs for returning the triangles of an array, but these are not documented in public documentation, so were not picked up during API comparison analysis.

Both triu and tril should be straightforward to include in the specification, as the signatures are consistent across libraries.

tril(m, k=0) → array
triu(m, k=0) → array

where k specifies the diagonal above/below which to zero elements.

Activity

  1. added
    API extension
    on Jul 26, 2021
  2. leofang commented on Jul 26, 2021

    @leofang
    Contributor

    @kgryte You're 10 mins ahead of me! I was working on cupy/cupy#5555 and just realized the very same need. I think we need them to be able to handle and return a batch of matrices like all other linalg functions, which is currently not possible with NumPy/CuPy.

  3. kgryte commented on Jul 26, 2021

    @kgryte
    ContributorAuthor

    @leofang I think handling batches could make sense; however, I don't think any array libraries currently support batching, including Torch. Should be a straightforward extension, as should not conflict with any existing NumPy et al usage patterns.

  4. lezcano commented on Jul 26, 2021

    @lezcano
    Contributor

    The docs of tril and triu are outdated, PyTorch does support batched inputs for these functions. I agree with @leofang that it would be good to write the specification for batched inputs.

  5. kgryte commented on Aug 23, 2021

    @kgryte
    ContributorAuthor

    I've opened gh-243 which adds both tril and triu to the specification, with support for providing a stack of matrices.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    API extension

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions