|
3 | 3 |
|
4 | 4 |
|
5 | 5 | class Element:
|
| 6 | + """An element of the specified group. |
| 7 | +
|
| 8 | + Parameters |
| 9 | + ---------- |
| 10 | + group: |
| 11 | + The group of which this is an element. |
| 12 | + value: |
| 13 | + The individual element value. |
| 14 | + """ |
6 | 15 | def __init__(self, group, value):
|
7 | 16 | group._validate(value)
|
8 | 17 | self.group = group
|
9 | 18 | self.value = value
|
10 | 19 |
|
11 | 20 | def __mul__(self, other):
|
12 |
| - '''Use * to represent the group operation.''' |
| 21 | + """Use * to represent the group operation.""" |
13 | 22 | return Element(self.group,
|
14 | 23 | self.group.operation(self.value,
|
15 | 24 | other.value))
|
16 | 25 |
|
17 | 26 | def __str__(self):
|
| 27 | + """Return a string of the form value_group.""" |
18 | 28 | return f"{self.value}_{self.group}"
|
19 | 29 |
|
20 | 30 | def __repr__(self):
|
| 31 | + """Return the canonical string representation of the element.""" |
21 | 32 | return f"{type(self).__name__}" \
|
22 | 33 | f"({repr(self.group), repr(self.value)})"
|
23 | 34 |
|
24 | 35 |
|
25 | 36 | class CyclicGroup:
|
26 |
| - '''A cyclic group represented by integer addition modulo group order.''' |
| 37 | + """A cyclic group represented by integer addition modulo group order.""" |
27 | 38 | def __init__(self, order):
|
28 | 39 | self.order = order
|
29 | 40 |
|
30 | 41 | def _validate(self, value):
|
31 |
| - '''Ensure that value is a legitimate element value in this group.''' |
| 42 | + """Ensure that value is a legitimate element value in this group.""" |
32 | 43 | if not (isinstance(value, Integral) and 0 <= value < self.order):
|
33 | 44 | raise ValueError("Element value must be an integer"
|
34 | 45 | f" in the range [0, {self.order})")
|
35 | 46 |
|
36 | 47 | def operation(self, a, b):
|
37 |
| - '''The group operation is addition modulo n.''' |
| 48 | + """Perform the group operation on two values. |
| 49 | +
|
| 50 | + The group operation is addition modulo n.""" |
38 | 51 | return (a + b) % self.order
|
39 | 52 |
|
40 | 53 | def __call__(self, value):
|
41 |
| - '''Provide a convenient way to create elements of this group.''' |
| 54 | + """Create an element of this group.""" |
42 | 55 | return Element(self, value)
|
43 | 56 |
|
44 | 57 | def __str__(self):
|
| 58 | + """Represent the group as Gd.""" |
45 | 59 | return f"C{self.order}"
|
46 | 60 |
|
47 | 61 | def __repr__(self):
|
| 62 | + """Return the canonical string representation of the group.""" |
48 | 63 | return f"{type(self).__name__}({repr(self.order)})"
|
49 | 64 |
|
50 | 65 |
|
51 | 66 | class GeneralLinearGroup:
|
52 |
| - '''The general linear group represented by degree x degree matrices.''' |
| 67 | + """The general linear group represented by degree x degree matrices.""" |
53 | 68 | def __init__(self, degree):
|
54 | 69 | self.degree = degree
|
55 | 70 |
|
56 | 71 | def _validate(self, value):
|
57 |
| - '''Ensure that value is a legitimate element value in this group.''' |
58 |
| - value = np.asarray(value) |
59 |
| - if not (value.shape == (self.degree, self.degree)): |
| 72 | + """Ensure that value is a legitimate element value in this group.""" |
| 73 | + if not (isinstance(value, np.ndarray), |
| 74 | + value.shape == (self.degree, self.degree)): |
60 | 75 | raise ValueError("Element value must be a "
|
61 | 76 | f"{self.degree} x {self.degree}"
|
62 | 77 | "square array.")
|
63 | 78 |
|
64 | 79 | def operation(self, a, b):
|
65 |
| - '''The group operation is matrix multiplication.''' |
| 80 | + """Perform the group operation on two values. |
| 81 | +
|
| 82 | + The group operation is matrix multiplication.""" |
66 | 83 | return a @ b
|
67 | 84 |
|
68 | 85 | def __call__(self, value):
|
69 |
| - '''Provide a convenient way to create elements of this group.''' |
| 86 | + """Create an element of this group.""" |
70 | 87 | return Element(self, value)
|
71 | 88 |
|
72 | 89 | def __str__(self):
|
| 90 | + """Represent the group as Gd.""" |
73 | 91 | return f"G{self.degree}"
|
74 | 92 |
|
75 | 93 | def __repr__(self):
|
| 94 | + """Return the canonical string representation of the group.""" |
76 | 95 | return f"{type(self).__name__}({repr(self.degree)})"
|
0 commit comments