-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
105 lines (79 loc) · 3 KB
/
models.py
File metadata and controls
105 lines (79 loc) · 3 KB
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
from typing import Optional, Self
from dataclasses import dataclass
@dataclass
class GroupRepresentation:
n: Optional[int]
x: Optional[int]
def __str__(self) -> str:
if self.n is None and self.x is None:
return "unknown"
return f"<{self.n}:{self.x}>"
def __hash__(self) -> int:
return hash(str(self))
def __eq__(self, other):
return self.n == other.n and self.x == other.x
def __lt__(self, other):
# unknown groups are greater than non-unknowns
if self.n is None and self.x is None:
return False
if other.n is None and other.x is None:
return True
return self.n < other.n or (self.n == other.n and self.x < other.x)
@dataclass
class Permutation:
parts: list[tuple[int]]
def __str__(self) -> str:
s = ""
for part in self.parts:
s += '(' + ','.join([str(p) for p in part]) + ')'
return s
@dataclass
class GroupPermutationRepresentation:
perms: list[Permutation]
def __str__(self) -> str:
return '<' + ', '.join([str(p) for p in self.perms]) + '>'
@dataclass
class GroupStructure:
properties: dict[str, dict[str, int]]
soluble: Optional[bool] = None
def zero(self) -> Self:
return GroupStructure(
soluble=self.soluble,
properties={
key: {prop: 0 for prop in properties}
for key, properties in self.properties.items()
}
)
def property_types(self) -> list[str]:
return self.properties.keys()
def property_keys(self, property_type: str) -> list[str]:
return self.properties[property_type].keys()
@dataclass
class Group:
perm_rep: GroupPermutationRepresentation
isom_rep: GroupRepresentation
structures: dict[GroupRepresentation, GroupStructure]
perm_id: Optional[int] = None
soluble: Optional[bool] = None
def order(self) -> int:
rep = next(iter(self.structures.keys()))
return rep.n
def structure_property_types(self) -> list[str]:
# all structures are assumed to have equal property types
structure = next(iter(self.structures.values()))
return structure.property_types()
def structure_property_keys(self, property_type: str) -> list[str]:
# all structures are assumed to have equal property keys, given the same type
structure = next(iter(self.structures.values()))
return structure.property_keys(property_type)
def has_structure_solublity(self) -> bool:
structure = next(iter(self.structures.values()))
return structure.soluble is not None
def normalise_group_structures(groups: list[Group]) -> list[Group]:
structures = {rep: structure for g in groups for rep, structure in g.structures.items()}
for g in groups:
g.structures = {
rep: g.structures[rep] if rep in g.structures else structure.zero()
for rep, structure in structures.items()
}
return groups