Skip to content

Commit

Permalink
Fix issue with .real/.imag with string dtypes (#609)
Browse files Browse the repository at this point in the history
  • Loading branch information
Illviljan authored Oct 30, 2023
1 parent 44e434d commit 7c0c962
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
3 changes: 2 additions & 1 deletion sparse/_umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,8 @@ def _get_fill_value(self):
from ._coo import COO

zero_args = tuple(
arg.fill_value[...] if isinstance(arg, COO) else arg for arg in self.args
np.asarray(arg.fill_value, like=arg.data) if isinstance(arg, COO) else arg
for arg in self.args
)

# Some elemwise functions require a dtype argument, some abhorr it.
Expand Down
3 changes: 2 additions & 1 deletion sparse/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def assert_eq(x, y, check_nnz=True, compare_dtype=True, **kwargs):

check_equal = (
np.array_equal
if np.issubdtype(x.dtype, np.integer) and np.issubdtype(y.dtype, np.integer)
if (np.issubdtype(x.dtype, np.integer) and np.issubdtype(y.dtype, np.integer))
or (np.issubdtype(x.dtype, np.flexible) and np.issubdtype(y.dtype, np.flexible))
else functools.partial(np.allclose, equal_nan=True)
)

Expand Down
19 changes: 12 additions & 7 deletions sparse/tests/test_coo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1358,16 +1358,21 @@ def test_full_like():
)


@pytest.mark.parametrize("complex", [True, False])
def test_complex_methods(complex):
if complex:
x = np.array([1 + 2j, 2 - 1j, 0, 1, 0])
else:
x = np.array([1, 2, 0, 0, 0])
@pytest.mark.parametrize(
"x",
[
np.array([1, 2, 0, 0, 0]),
np.array([1 + 2j, 2 - 1j, 0, 1, 0]),
np.array(["a", "b", "c"]),
],
)
def test_complex_methods(x):
s = sparse.COO.from_numpy(x)
assert_eq(s.imag, x.imag)
assert_eq(s.real, x.real)
assert_eq(s.conj(), x.conj())

if np.issubdtype(s.dtype, np.number):
assert_eq(s.conj(), x.conj())


def test_np_matrix():
Expand Down

0 comments on commit 7c0c962

Please sign in to comment.