Created
September 14, 2021 13:32
-
-
Save jayendra13/a176bc39088bab3d7bfa6343db7ab559 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import numpy as np | |
def original_mirror(x, direction=0, reverse=False): | |
"""Mirrors left to right or top to bottom as per the direction.""" | |
x_ = np.copy(x) | |
height, width = x.shape | |
if direction == 0: | |
half_break = math.ceil(height/2) | |
shift = height % 2 | |
if reverse: | |
x_[:half_break, :] = np.flipud(x_[half_break - shift:, :]) | |
else: | |
x_[half_break - shift:, :] = np.flipud(x_[:half_break, :]) | |
elif direction == 1: | |
half_break = math.ceil(width/2) | |
shift = width % 2 | |
if reverse: | |
x_[:, :half_break] = np.fliplr(x_[:, half_break -shift:]) | |
else: | |
x_[:, half_break - shift:] = np.fliplr(x_[:, :half_break]) | |
else: | |
raise ValueError(f'Mirror direction {direction} unsupported') | |
return x_ | |
def mirror(x): | |
"Horizontal mirror" | |
height = x.shape[0] | |
half_break = math.ceil(height/2) | |
shift = height % 2 | |
x_ = np.copy(x) | |
x_[half_break - shift:, :] = np.flipud(x_[:half_break, :]) | |
return x_ | |
x = np.array([[1,2,3], [4,5,6], [7,8,9]]) | |
assert np.all(original_mirror(x, 0, False) == mirror(x)) | |
assert np.all(original_mirror(x, 1, False) == mirror(x.T).T) | |
assert np.all(original_mirror(x, 0, True) == mirror(np.flipud(x))) | |
assert np.all(original_mirror(x, 1, True) == mirror(np.flipud(x.T)).T) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment