array-api-compat icon indicating copy to clipboard operation
array-api-compat copied to clipboard

add support for dtype kinds in `astype`

Open lucascolley opened this issue 6 months ago • 3 comments

https://github.com/data-apis/array-api/pull/848#issuecomment-2638722698

lucascolley avatar Jun 17 '25 09:06 lucascolley

This seems to have slipped under the radar. Could you remind me what are the motivating use cases? IIUC, one was float32->complex64, which is now IMO adequately handled by 1j*float32_array -> complex64_array. Was there something about signed/unsigned integers? My search-foo is failing me

ev-br avatar Oct 28 '25 11:10 ev-br

Hmm, not sure if there was a real-world motivating case for signed/unsigned integers, but in general I still think this is rather nice to have. Suppose you are using something like this in a context of parametrising over dtype kinds, something like

dtype_id = 1.0 if dtype_kind=="float" else 1j # or more cases
x *= dtype_id

can become

x = xp.astype(x, kind=dtype_kind)

which is not only cleaner but also semantically much nicer IMO!

lucascolley avatar Oct 28 '25 11:10 lucascolley

This is the int/uint discussion I was trying to remember: https://github.com/data-apis/array-api/issues/859#issuecomment-2502160704

ev-br avatar Oct 29 '25 11:10 ev-br