master > master: code py - utils, rel perm

This commit is contained in:
RD 2022-06-14 20:01:48 +02:00
parent 3b8f80cff9
commit c6149c230a

View File

@ -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;