-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnpfunc.py
75 lines (61 loc) · 1.7 KB
/
npfunc.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
64
65
66
67
68
69
70
71
72
73
74
75
import itertools
import numpy as np
def accum_unordered_vec(x):
y = np.zeros(x.shape)
for ix in itertools.permutations(range(5), 5):
y += np.moveaxis(x, [0, 1, 2, 3, 4], ix)
z = np.zeros(x.shape)
for ix in itertools.combinations(range(x.shape[0]), 5):
z[ix] = y[ix]
return z
def accum_unordered(x):
g = np.zeros(x.shape)
for ix in itertools.permutations(range(x.shape[0]), 5):
g[tuple(sorted(ix))] += x[ix]
return g
def fast_sort5(a, b, c, d, e):
"Sort 5 values with 7 Comparisons"
if a < b:
a, b = b, a
if c < d:
c, d = d, c
if a < c:
a, b, c, d = c, d, a, b
if e < c:
if e < d:
pass
else:
d, e = e, d
else:
if e < a:
c, d, e = e, c, d
else:
a, c, d, e = e, a, c, d
if b < d:
if b < e:
return b, e, d, c, a
else:
return e, b, d, c, a
else:
if b < c:
return e, d, b, c, a
else:
return e, d, c, b, a
def accum_unordered_loop(x):
n = x.shape[0]
g = np.zeros(x.shape)
for i in range(n):
for j in range(n):
if i == j:
continue
for k in range(n):
if i == k or j == k:
continue
for l in range(n):
if i == l or j == l or k == l:
continue
for m in range(n):
if i != m and j != m and k != m and l != m:
ix, jx, kx, lx, mx = fast_sort5(i, j, k, l, m)
g[ix, jx, kx, lx, mx] += x[i, j, k, l, m]
return g