Skip to content

Can an API for dealing with composite operations be defined? #474

Closed
@rgommers

Description

@rgommers

We've had discussions several times about whether some function that is a composite of other functions already present in the standard can be added. The most recent example is gh-460, which proposed adding abs2 (= abs(x) ** 2). Typically this isn't worth it, unless there are very compelling reasons. Fusing function calls is something that multiple libraries have compilers for, so there may not be a gain for those libraries to add a function like abs2, just extra API surface. And performance-wise, the gain must be quite large for it to be justified to add a function.

The discussion then turned to "is it possible to write for example a standardized way for array API consumers to element-wise apply arbitrary functions to arrays"? Something along the lines of np.vectorize.

Another direction could be to try and write a portable JIT-able set of functions. Something like:

# Here `xp` and `compiler` can be injected

@compiler.jit
def abs2(x):
    """Squared absolute value"""
    return xp.real(x) * xp.real(x) + xp.imag(x) * xp.imag(x)

There's multiple options for compilers here, and they work with different array libraries. Perhaps a more future-proof direction (np.vectorize wasn't quite a success, and numexpr type string expressions are a bit of a hack as well ...).

This is not a worked out proposal, just opening it here as a follow-up to the discussion in gh-460 and as a tracker issue to collect more ideas and serve as a reference for when other composite functions are proposed to be added to the standard.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions