Skip to content
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

Feature/737 isreal iscomplex #738

Merged
merged 6 commits into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
### Logical
- [#711](https://github.com/helmholtz-analytics/heat/pull/711) `isfinite()`, `isinf()`, `isnan()`

### Types
- [#738](https://github.com/helmholtz-analytics/heat/pull/738) `iscomplex()`, `isreal()`


## Bug fixes
- [#709](https://github.com/helmholtz-analytics/heat/pull/709) Set the encoding for README.md in setup.py explicitly.
Expand Down
66 changes: 66 additions & 0 deletions heat/core/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,72 @@ def test_complex128(self):

self.assertEqual(ht.complex128.char(), "c16")

def test_iscomplex(self):
a = ht.array([1, 1.2, 1 + 1j, 1 + 0j])
s = ht.array([False, False, True, False])
r = ht.iscomplex(a)
self.assertEqual(r.shape, s.shape)
self.assertEqual(r.dtype, s.dtype)
self.assertEqual(r.device, s.device)
self.assertTrue(ht.equal(r, s))

a = ht.array([1, 1.2, True], split=0)
s = ht.array([False, False, False], split=0)
r = ht.iscomplex(a)
self.assertEqual(r.shape, s.shape)
self.assertEqual(r.dtype, s.dtype)
self.assertEqual(r.device, s.device)
self.assertTrue(ht.equal(r, s))

a = ht.ones((6, 6), dtype=ht.bool, split=0)
s = ht.zeros((6, 6), dtype=ht.bool, split=0)
r = ht.iscomplex(a)
self.assertEqual(r.shape, s.shape)
self.assertEqual(r.dtype, s.dtype)
self.assertEqual(r.device, s.device)
self.assertTrue(ht.equal(r, s))

a = ht.full((5, 5), 1 + 1j, dtype=ht.int, split=1)
s = ht.ones((5, 5), dtype=ht.bool, split=1)
r = ht.iscomplex(a)
self.assertEqual(r.shape, s.shape)
self.assertEqual(r.dtype, s.dtype)
self.assertEqual(r.device, s.device)
self.assertTrue(ht.equal(r, s))

def test_isreal(self):
a = ht.array([1, 1.2, 1 + 1j, 1 + 0j])
s = ht.array([True, True, False, True])
r = ht.isreal(a)
self.assertEqual(r.shape, s.shape)
self.assertEqual(r.dtype, s.dtype)
self.assertEqual(r.device, s.device)
self.assertTrue(ht.equal(r, s))

a = ht.array([1, 1.2, True], split=0)
s = ht.array([True, True, True], split=0)
r = ht.isreal(a)
self.assertEqual(r.shape, s.shape)
self.assertEqual(r.dtype, s.dtype)
self.assertEqual(r.device, s.device)
self.assertTrue(ht.equal(r, s))

a = ht.ones((6, 6), dtype=ht.bool, split=0)
s = ht.ones((6, 6), dtype=ht.bool, split=0)
r = ht.isreal(a)
self.assertEqual(r.shape, s.shape)
self.assertEqual(r.dtype, s.dtype)
self.assertEqual(r.device, s.device)
self.assertTrue(ht.equal(r, s))

a = ht.full((5, 5), 1 + 1j, dtype=ht.int, split=1)
s = ht.zeros((5, 5), dtype=ht.bool, split=1)
r = ht.isreal(a)
self.assertEqual(r.shape, s.shape)
self.assertEqual(r.dtype, s.dtype)
self.assertEqual(r.device, s.device)
self.assertTrue(ht.equal(r, s))


class TestTypeConversion(TestCase):
def test_can_cast(self):
Expand Down
40 changes: 40 additions & 0 deletions heat/core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
from . import communication
from . import devices
from . import factories
from . import _operations
from . import sanitation


__all__ = [
Expand Down Expand Up @@ -55,6 +57,8 @@
"double",
"flexible",
"can_cast",
"iscomplex",
"isreal",
"issubdtype",
"promote_types",
"complex64",
Expand Down Expand Up @@ -607,6 +611,42 @@ def can_cast(from_, to, casting="intuitive"):
break


def iscomplex(x):
"""
Test element-wise if input is complex.

Parameters
----------
x : DNDarray

Examples
--------
>>> ht.iscomplex(ht.array([1+1j, 1]))
DNDarray([ True, False], dtype=ht.bool, device=cpu:0, split=None)
"""
sanitation.sanitize_in(x)

if issubclass(x.dtype, _complexfloating):
return x.imag != 0
else:
return factories.zeros(x.shape, bool, split=x.split, device=x.device, comm=x.comm)
Comment on lines +627 to +632
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use torch.is_complex?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not find it 🔍 😄

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

torch.is_complex returns the result for the whole tensor, not element-wise

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow. that is a really annoying difference. but that is what it is



def isreal(x):
"""
Test element-wise if input is real-valued.

Parameters
----------
x : DNDarray

Examples
--------
ht.iscomplex(ht.array([1+1j, 1]))
"""
return _operations.__local_op(torch.isreal, x, None, no_cast=True)


def issubdtype(arg1, arg2):
"""
Returns True if first argument is a typecode lower/equal in type hierarchy.
Expand Down