From c6149c230a1afd6e8a31954d3139d55544bd0b4a Mon Sep 17 00:00:00 2001 From: raj_mathe Date: Tue, 14 Jun 2022 20:01:48 +0200 Subject: [PATCH] master > master: code py - utils, rel perm --- code/python/src/core/utils.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/code/python/src/core/utils.py b/code/python/src/core/utils.py index 0585607..a565c9b 100644 --- a/code/python/src/core/utils.py +++ b/code/python/src/core/utils.py @@ -6,6 +6,7 @@ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ from src.thirdparty.code import *; +from src.thirdparty.maths import *; from src.thirdparty.types import *; # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -14,6 +15,7 @@ from src.thirdparty.types import *; __all__ = [ 'iperm', + 'permute_part', ]; # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -24,7 +26,22 @@ def iperm(order: List[int]) -> List[int]: ''' Computes the inverse of a permutation. ''' - n = len(order); perm = list(enumerate(order)); uorder = list(map(lambda x: x[0], sorted(perm, key=lambda x: x[1]))); return uorder; + +def permute_part( + x: np.ndarray, + indexes: List[int], + order: List[int], + in_place: bool = True, +) -> np.ndarray: + ''' + Permutes a part of a list by a relative permutation for that part of the list. + ''' + if not in_place: + x = x[:]; + part = x[indexes]; + part[:] = part[order]; + x[indexes] = part; + return x;