- Notifications
You must be signed in to change notification settings - Fork54
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
In some of my numerical codes, I have a class importnumpyasnpfromarray_api_compatimportarray_namespaceclassIdentity:dtype=np.dtype("u1")def__matmul__(self,x):returnxI=Identity()x=np.array([1,2,3])xp=array_namespace(x)print(xp.result_type(x,I)) I would now like to make this work with general |
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 5 comments
-
Hmm, based on just this example that may be a bit tricky to make work portably. Is the classIdentity:def__init__(self,dtype):self.dtype=dtypex=np.array([1,2,3])I=Identity(x.dtype) |
BetaWas this translation helpful?Give feedback.
All reactions
-
I'm not sure that this kind of duck typing can be made to work, at least in a guaranteed way, in the current spec. There's nothing in With that being said, ignoring those issues, one issue with your "lowest possible" dtype is that integer and float dtypesdon't promote to each other. You could have two objects, one for integers with uint8 and one for floats with float32. Although also note that uint8 and int8 are both equally low in the dtype promotion graph, so choosing one will always cause the other to promote to int16. TBH, what you likely really want is to use a library like JAX that lets you just write |
BetaWas this translation helpful?Give feedback.
All reactions
-
A typical use is, e.g., defcool_linear_solver(A,b,M=None):m,n=A.shapeassertm==nM=MorIdentity(n)dt=xp.result_dtype(A,b,M)x=xp.array(n,dtype)# ...# x += M @ (b - A @ x)# ... If Perhaps the easiest thing to do is to write dt=xp.result_dtype(A,b,M.dtype) and make explicit the fact that I expect |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
This would work based on the snippet you have given, right? |
BetaWas this translation helpful?Give feedback.
All reactions
-
@lucascolley Yeah, but there are several other dtype_determining_objects= [A,b]ifMisnotNone:dtype_determining_objects.append(M)ifNisnotNone:dtype_determining_objects.append(N)ifQisnotNone:dtype_determining_objects.append(Q)dt=xp.result_dtype(*dtype_determining_objects) but this feels clumsy. |
BetaWas this translation helpful?Give feedback.
All reactions
This discussion was converted from issue #739 on April 04, 2024 04:48.