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 all commits
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
34 changes: 28 additions & 6 deletions array_api_tests/test_data_type_functions.py
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
from typing import Union

import pytest
from hypothesis import given
from hypothesis import given, assume
from hypothesis import strategies as st

from . import _array_module as xp
@@ -23,26 +23,43 @@ 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=hh.all_dtypes,
dtype=hh.all_dtypes,
kw=hh.kwargs(copy=st.booleans()),
data=st.data(),
)
def test_astype(x_dtype, dtype, kw, data):
_complex_dtypes = (xp.complex64, xp.complex128)

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 _complex_dtypes:
real_dtype = _float_match_complex(x_dtype)
m1, M1 = dh.dtype_ranges[real_dtype]

real_dtype = dtype
if dtype in _complex_dtypes:
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,
@@ -54,6 +71,11 @@ def test_astype(x_dtype, dtype, kw, data):
hh.arrays(dtype=x_dtype, shape=hh.shapes(), elements=elements_strat), label="x"
)

# according to the spec, "Casting a complex floating-point array to a real-valued
# data type should not be permitted."
# https://data-apis.org/array-api/latest/API_specification/generated/array_api.astype.html#astype
assume(not ((x_dtype in _complex_dtypes) and (dtype not in _complex_dtypes)))

out = xp.astype(x, dtype, **kw)

ph.assert_kw_dtype("astype", kw_dtype=dtype, out_dtype=out.dtype)