diff options
author | Anton Khirnov <anton@khirnov.net> | 2018-03-22 14:48:24 +0100 |
---|---|---|
committer | Anton Khirnov <anton@khirnov.net> | 2018-03-22 14:48:24 +0100 |
commit | 334f7b637147e8b85b72265a4a84c34378652a13 (patch) | |
tree | 1fef025a72a5f36e42f33fb717f5482347bded16 | |
parent | f4d6e7a434db5967f9df67caf318326bd023f6c7 (diff) |
Add array reflection.
-rw-r--r-- | utils.py | 18 |
1 files changed, 18 insertions, 0 deletions
@@ -19,3 +19,21 @@ def matrix_invert(mat): inv = np.linalg.inv(mat) return inv.transpose((1, 2, 0)).reshape(oldshape) +def array_reflect(data, parity = 1.0, axis = -1): + """ + Reflect an N-D array with respect to the specified axis. E.g. input + [0, 1, 2, 3] becomes [3, 2, 1, 0, 1, 2, 3] with parity=1.0 and + [-3, -2, -1, 0, 1, 2, 3] with parity=-1.0. + + :param array_like data: The array to reflect. + :param float parity: The reflected portion is multiplied by this factor, + typically 1.0 or -1.0. + :param int axis: Index of the axis to reflect along. + :return: Reflected array. + """ + slices0 = [slice(None) for _ in data.shape] + slices1 = [slice(None) for _ in data.shape] + slices0[axis] = slice(None, None, -1) + slices1[axis] = slice(1, None) + + return np.concatenate((parity * data[slices0], data[slices1]), axis) |