forked from google-deepmind/alphageometry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_utils.py
132 lines (93 loc) · 2.71 KB
/
graph_utils.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Copyright 2023 DeepMind Technologies Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Utilizations for graph representation.
Mainly for listing combinations and permutations of elements.
"""
from geometry import Point
def _cross(elems1, elems2):
for e1 in elems1:
for e2 in elems2:
yield e1, e2
def cross(elems1, elems2):
return list(_cross(elems1, elems2))
def _comb2(elems):
if len(elems) < 2:
return
for i, e1 in enumerate(elems[:-1]):
for e2 in elems[i + 1 :]:
yield e1, e2
def comb2(elems):
return list(_comb2(elems))
def _comb3(elems):
if len(elems) < 3:
return
for i, e1 in enumerate(elems[:-2]):
for j, e2 in enumerate(elems[i + 1 : -1]):
for e3 in elems[i + j + 2 :]:
yield e1, e2, e3
def comb3(elems):
return list(_comb3(elems))
def _comb4(elems):
if len(elems) < 4:
return
for i, e1 in enumerate(elems[:-3]):
for j, e2 in enumerate(elems[i + 1 : -2]):
for e3, e4 in _comb2(elems[i + j + 2 :]):
yield e1, e2, e3, e4
def comb4(elems):
return list(_comb4(elems))
def _perm2(elems):
for e1, e2 in comb2(elems):
yield e1, e2
yield e2, e1
def perm2(elems):
return list(_perm2(elems))
def _all_4points(l1, l2):
p1s = l1.neighbors(Point)
p2s = l2.neighbors(Point)
for a, b in perm2(p1s):
for c, d in perm2(p2s):
yield a, b, c, d
def all_4points(l1, l2):
return list(_all_4points(l1, l2))
def _all_8points(l1, l2, l3, l4):
for a, b, c, d in all_4points(l1, l2):
for e, f, g, h in all_4points(l3, l4):
yield (a, b, c, d, e, f, g, h)
def all_8points(l1, l2, l3, l4):
return list(_all_8points(l1, l2, l3, l4))
def _perm3(elems):
for x in elems:
for y in elems:
if y == x:
continue
for z in elems:
if z not in (x, y):
yield x, y, z
def perm3(elems):
return list(_perm3(elems))
def _perm4(elems):
for x in elems:
for y in elems:
if y == x:
continue
for z in elems:
if z in (x, y):
continue
for t in elems:
if t not in (x, y, z):
yield x, y, z, t
def perm4(elems):
return list(_perm4(elems))