-
Notifications
You must be signed in to change notification settings - Fork 0
/
math_helper.py
63 lines (51 loc) · 1.57 KB
/
math_helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
@file
@brief Mathemical functions.
"""
def decompose_permutation(perm):
"""
Decomposes a permutation into transitions.
:param perm: permutation (integers)
:return: list of tuples
.. note::
The function does not check *perm* is a permutation.
If the input value is wrong, the execution could
end up in an infinite loop.
.. runpython::
:showcode:
import pprint
from deeponnxcustom.tools.math_helper import decompose_permutation
pprint.pprint(decompose_permutation((1, 0)))
pprint.pprint(decompose_permutation((2, 0, 1)))
pprint.pprint(decompose_permutation((1, 4, 2, 3, 0)))
Function @see fn apply_transitions applies this series
of transitions.
"""
perm = list(perm)
transitions = []
while True:
index = -1
for i, p in enumerate(perm):
if p != i:
index = i
break
if index == -1:
break
while perm[index] != index:
a, b = index, perm[index]
transitions.append((b, a))
perm[a], perm[b] = perm[b], perm[a]
index = b
return list(reversed(transitions))
def apply_transitions(n, transitions):
"""
Applies a list of transitions (permutations of two elements)
on the first *n* integers.
:param n: number of elements in the permutation
:param transitions: list of transitions
:return: permuted ensemble
"""
ens = list(range(n))
for a, b in transitions:
ens[a], ens[b] = ens[b], ens[a]
return ens