Skip to content

Identity() operator dtype #739

@nschloe

Description

@nschloe

In some of my numerical codes, I have a class Indentity() that represents cheap identity matrices. To make it work with things like result_type, I assign it the "lowest possible" dtype, "u1":

import numpy as np
from array_api_compat import array_namespace

class Identity:
    dtype = np.dtype("u1")

    def __matmul__(self, x):
        return x


I = Identity()
x = np.array([1,2,3])

xp = array_namespace(x)
print(xp.result_type(x, I))
int64

I would now like to make this work with general xp arrays. Assigning a concrete implementation for dtype though isn't what I want. Using None as a dtype sounds plausible, but with NumPy gives a warning and then a wrong result_type:

DeprecationWarning: in the future the `.dtype` attribute of a given datatype object must be a valid dtype instance. `data_type.dtype` may need to be coerced using `np.dtype(data_type.dtype)`. (Deprecated NumPy 1.20)
  print(xp.result_type(x, I))
float64

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