Skip to content
Open
Show file tree
Hide file tree
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
8 changes: 6 additions & 2 deletions src/array_api_extra/_statistical.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,10 @@
or _compat.is_cupy_namespace(xp)
or _compat.is_dask_namespace(xp)
or _compat.is_jax_namespace(xp)
or _compat.is_torch_namespace(xp)
or (_compat.is_torch_namespace(xp) and
(xp.isdtype(a.dtype, "real floating") or _compat.device(a).type != "cpu")
)
):

Check failure on line 372 in src/array_api_extra/_statistical.py

View workflow job for this annotation

GitHub Actions / Lint

Cannot access attribute "type" for class "Device"   Attribute "type" is unknown (reportAttributeAccessIssue)
return xp.nanmean(a, axis=axis)

return _agnostic._statistical.nanmean(a, axis=axis, xp=xp)
Expand Down Expand Up @@ -418,8 +420,10 @@
or _compat.is_cupy_namespace(xp)
or _compat.is_dask_namespace(xp)
or _compat.is_jax_namespace(xp)
or _compat.is_torch_namespace(xp)
or (_compat.is_torch_namespace(xp) and
(xp.isdtype(a.dtype, "real floating") or _compat.device(a).type != "cpu")
)
):
return xp.nansum(a, axis=axis)

return _agnostic._statistical.nansum(a, axis=axis, xp=xp)

Check failure on line 429 in src/array_api_extra/_statistical.py

View workflow job for this annotation

GitHub Actions / Lint

Cannot access attribute "type" for class "Device"   Attribute "type" is unknown (reportAttributeAccessIssue)
34 changes: 34 additions & 0 deletions tests/main/test_statistical.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,23 @@ def test_xp(self, axis: int | None, expected_list: list[float], xp: ArrayNamespa
expected = xp.asarray(expected_list)
assert_equal(res, expected)

def test_complex(self, xp: ArrayNamespace):
a = xp.asarray(
[
[1 + 2j, complex(float("nan"), 0.0), 4 + 1j, 5 - 1j],
[complex(float("nan"), 0.0), -2 + 1j, 3 - 2j, -4 + 2j],
[2 - 1j, 1 + 3j, 3 + 2j, complex(float("nan"), 0.0)],
],
dtype=xp.complex128,
)

res = nansum(a, axis=0)
expected = xp.asarray(
[3 + 1j, -1 + 4j, 10 + 1j, 1 + 1j],
dtype=xp.complex128,
)
assert_equal(res, expected)


class TestNanMean:
def test_simple(self, xp: ArrayNamespace):
Expand Down Expand Up @@ -570,3 +587,20 @@ def test_xp(self, axis: int | None, expected_list: list[float], xp: ArrayNamespa
res = nanmean(a, axis=axis, xp=xp)
expected = xp.asarray(expected_list)
assert_equal(res, expected)

def test_complex(self, xp: ArrayNamespace):
a = xp.asarray(
[
[1 + 2j, complex(float("nan"), 0.0), 4 + 1j, 5 - 1j],
[complex(float("nan"), 0.0), -2 + 1j, 3 - 2j, -4 + 2j],
[2 - 1j, 1 + 3j, 3 + 2j, complex(float("nan"), 0.0)],
],
dtype=xp.complex128,
)

res = nanmean(a, axis=0)
expected = xp.asarray(
[1.5 + 0.5j, -0.5 + 2j, 10 / 3 + 1j / 3, 0.5 + 0.5j],
dtype=xp.complex128,
)
assert_close(res, expected)
Loading