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

Features/240 flipud #496

Merged
merged 18 commits into from
Apr 1, 2020
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [#483](https://github.com/helmholtz-analytics/heat/pull/483) Bugfix: Underlying torch tensor moves to the right device on array initialisation
- [#483](https://github.com/helmholtz-analytics/heat/pull/483) Bugfix: DNDarray.cpu() changes heat device to cpu
- Update documentation theme to "Read the Docs"
- [#496](https://github.com/helmholtz-analytics/heat/pull/496) New feature: flipud()
- [#498](https://github.com/helmholtz-analytics/heat/pull/498) Feature: flip()
- [#499](https://github.com/helmholtz-analytics/heat/pull/499) Bugfix: MPI datatype mapping: `torch.int16` now maps to `MPI.SHORT` instead of `MPI.SHORT_INT`
- [#515] (https://github.com/helmholtz-analytics/heat/pull/515) ht.var() now returns the unadjusted sample variance by default, Bessel's correction can be applied by setting ddof=1.
Expand Down
30 changes: 30 additions & 0 deletions heat/core/manipulations.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"diagonal",
"expand_dims",
"flip",
"flipud",
"hstack",
"resplit",
"sort",
Expand Down Expand Up @@ -619,6 +620,35 @@ def flip(a, axis=None):
return res


def flipud(a):
"""
Flip array in the up/down direction.

Parameters
----------
a: ht.DNDarray
Input array to be flipped

Returns
-------
res: ht.DNDarray
The flipped array.

Examples
--------
>>> a = ht.array([[0,1],[2,3]])
>>> ht.flipud(a)
tensor([[2, 3],
[0, 1]])

>>> b = ht.array([[0,1,2],[3,4,5]], split=0)
>>> ht.flipud(b)
(1/2) tensor([3,4,5])
(2/2) tensor([0,1,2])
"""
return flip(a, 0)


def hstack(tup):
"""
Stack arrays in sequence horizontally (column wise).
Expand Down
38 changes: 38 additions & 0 deletions heat/core/tests/test_manipulations.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,44 @@ def test_flip(self):
)
self.assertTrue(ht.equal(ht.flip(a, [1, 2]), r_a))

def test_flipud(self):
a = ht.array([1, 2], device=ht_device)
r_a = ht.array([2, 1], device=ht_device)
self.assertTrue(ht.equal(ht.flipud(a), r_a))

b = ht.array([[1, 2], [3, 4]], device=ht_device)
r_b = ht.array([[3, 4], [1, 2]], device=ht_device)
self.assertTrue(ht.equal(ht.flipud(b), r_b))

# splitted
c = ht.array(
[[[0, 1], [2, 3]], [[4, 5], [6, 7]], [[8, 9], [10, 11]], [[12, 13], [14, 15]]],
split=0,
device=ht_device,
)
r_c = ht.array(
[[[12, 13], [14, 15]], [[8, 9], [10, 11]], [[4, 5], [6, 7]], [[0, 1], [2, 3]]],
split=0,
device=ht_device,
)
self.assertTrue(ht.equal(ht.flipud(c), r_c))

c = ht.array(
[[[0, 1], [2, 3]], [[4, 5], [6, 7]], [[8, 9], [10, 11]], [[12, 13], [14, 15]]],
split=1,
device=ht_device,
dtype=ht.float32,
)
self.assertTrue(ht.equal(ht.resplit(ht.flipud(c), 0), r_c))

c = ht.array(
[[[0, 1], [2, 3]], [[4, 5], [6, 7]], [[8, 9], [10, 11]], [[12, 13], [14, 15]]],
split=2,
device=ht_device,
dtype=ht.int8,
)
self.assertTrue(ht.equal(ht.resplit(ht.flipud(c), 0), r_c))

def test_hstack(self):
# cases to test:
# MM===================================
Expand Down