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/35 fliplr #580

Merged
merged 5 commits into from
Jun 4, 2020
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Pending Additions

- [#573](https://github.com/helmholtz-analytics/heat/pull/573) Bugfix: matmul fixes: early out for 2 vectors, remainders not added if inner block is 1 for split 10 case
- [#580](https://github.com/helmholtz-analytics/heat/pull/580) New feature: fliplr()


# v0.4.0
Expand Down
30 changes: 30 additions & 0 deletions heat/core/manipulations.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"expand_dims",
"flatten",
"flip",
"fliplr",
"flipud",
"hstack",
"reshape",
Expand Down Expand Up @@ -702,6 +703,35 @@ def flip(a, axis=None):
return res


def fliplr(a):
"""
Flip array in the left/right direction. If a.ndim > 2, flip along dimension 1.

Parameters
----------
a: ht.DNDarray
Input array to be flipped, must be at least 2-D

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

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

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


def flipud(a):
"""
Flip array in the up/down direction.
Expand Down
39 changes: 39 additions & 0 deletions heat/core/tests/test_manipulations.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,45 @@ def test_flip(self):
)
self.assertTrue(ht.equal(ht.flip(a, [1, 2]), r_a))

def test_fliplr(self):
b = ht.array([[1, 2], [3, 4]], device=ht_device)
r_b = ht.array([[2, 1], [4, 3]], device=ht_device)
self.assertTrue(ht.equal(ht.fliplr(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(
[[[2, 3], [0, 1]], [[6, 7], [4, 5]], [[10, 11], [8, 9]], [[14, 15], [12, 13]]],
split=0,
device=ht_device,
)
self.assertTrue(ht.equal(ht.fliplr(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.fliplr(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.fliplr(c), 0), r_c))

# test exception
a = ht.arange(10, device=ht_device)
with self.assertRaises(IndexError):
ht.fliplr(a)

def test_flipud(self):
a = ht.array([1, 2], device=ht_device)
r_a = ht.array([2, 1], device=ht_device)
Expand Down