Skip to content

ENH: test astype with complex inputs #311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 28, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
ENH: test astype with complex inputs
  • Loading branch information
ev-br committed Nov 18, 2024
commit 038ae16b6f89403063a659dc0b61d5bb5e4aee9b
29 changes: 24 additions & 5 deletions array_api_tests/test_data_type_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,49 @@ def non_complex_dtypes():
return xps.boolean_dtypes() | hh.real_dtypes


def numeric_dtypes():
return xps.boolean_dtypes() | hh.real_dtypes | hh.complex_dtypes


def float32(n: Union[int, float]) -> float:
return struct.unpack("!f", struct.pack("!f", float(n)))[0]


def _float_match_complex(complex_dtype):
return xp.float32 if complex_dtype == xp.complex64 else xp.float64


@given(
x_dtype=non_complex_dtypes(),
dtype=non_complex_dtypes(),
x_dtype=numeric_dtypes(),
dtype=numeric_dtypes(),
kw=hh.kwargs(copy=st.booleans()),
data=st.data(),
)
def test_astype(x_dtype, dtype, kw, data):
if xp.bool in (x_dtype, dtype):
elements_strat = hh.from_dtype(x_dtype)
else:
m1, M1 = dh.dtype_ranges[x_dtype]
m2, M2 = dh.dtype_ranges[dtype]

if dh.is_int_dtype(x_dtype):
cast = int
elif x_dtype == xp.float32:
elif x_dtype in (xp.float32, xp.complex64):
cast = float32
else:
cast = float

real_dtype = x_dtype
if x_dtype in (xp.complex64, xp.complex128):
real_dtype = _float_match_complex(x_dtype)
m1, M1 = dh.dtype_ranges[real_dtype]

real_dtype = dtype
if dtype in (xp.complex64, xp.complex128):
real_dtype = _float_match_complex(x_dtype)
m2, M2 = dh.dtype_ranges[real_dtype]

min_value = cast(max(m1, m2))
max_value = cast(min(M1, M2))

elements_strat = hh.from_dtype(
x_dtype,
min_value=min_value,
Expand Down
Loading