Skip to content

Commit 07cba88

Browse files
committed
Docstrings for groups examples.
1 parent f228157 commit 07cba88

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

‎doc/webgit

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Subproject commit c5544a45f10699ffbf661e4d53d500968a76bbac
1+
Subproject commit 66c77fc192e23ce5644c897ab6e0a15cbe64e245

‎example_code/groups.py

+2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ def __mul__(self, other):
2424
other.value))
2525

2626
def __str__(self):
27+
"""Return a string of the form value_group."""
2728
return f"{self.value}_{self.group}"
2829

2930
def __repr__(self):
31+
"""Return the canonical string representation of the element."""
3032
return f"{type(self).__name__}" \
3133
f"({repr(self.group), repr(self.value)})"
3234

‎example_code/groups_basic.py

+30-11
Original file line numberDiff line numberDiff line change
@@ -3,74 +3,93 @@
33

44

55
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+
"""
615
def __init__(self, group, value):
716
group._validate(value)
817
self.group = group
918
self.value = value
1019

1120
def __mul__(self, other):
12-
'''Use * to represent the group operation.'''
21+
"""Use * to represent the group operation."""
1322
return Element(self.group,
1423
self.group.operation(self.value,
1524
other.value))
1625

1726
def __str__(self):
27+
"""Return a string of the form value_group."""
1828
return f"{self.value}_{self.group}"
1929

2030
def __repr__(self):
31+
"""Return the canonical string representation of the element."""
2132
return f"{type(self).__name__}" \
2233
f"({repr(self.group), repr(self.value)})"
2334

2435

2536
class CyclicGroup:
26-
'''A cyclic group represented by integer addition modulo group order.'''
37+
"""A cyclic group represented by integer addition modulo group order."""
2738
def __init__(self, order):
2839
self.order = order
2940

3041
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."""
3243
if not (isinstance(value, Integral) and 0 <= value < self.order):
3344
raise ValueError("Element value must be an integer"
3445
f" in the range [0, {self.order})")
3546

3647
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."""
3851
return (a + b) % self.order
3952

4053
def __call__(self, value):
41-
'''Provide a convenient way to create elements of this group.'''
54+
"""Create an element of this group."""
4255
return Element(self, value)
4356

4457
def __str__(self):
58+
"""Represent the group as Gd."""
4559
return f"C{self.order}"
4660

4761
def __repr__(self):
62+
"""Return the canonical string representation of the group."""
4863
return f"{type(self).__name__}({repr(self.order)})"
4964

5065

5166
class GeneralLinearGroup:
52-
'''The general linear group represented by degree x degree matrices.'''
67+
"""The general linear group represented by degree x degree matrices."""
5368
def __init__(self, degree):
5469
self.degree = degree
5570

5671
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)):
6075
raise ValueError("Element value must be a "
6176
f"{self.degree} x {self.degree}"
6277
"square array.")
6378

6479
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."""
6683
return a @ b
6784

6885
def __call__(self, value):
69-
'''Provide a convenient way to create elements of this group.'''
86+
"""Create an element of this group."""
7087
return Element(self, value)
7188

7289
def __str__(self):
90+
"""Represent the group as Gd."""
7391
return f"G{self.degree}"
7492

7593
def __repr__(self):
94+
"""Return the canonical string representation of the group."""
7695
return f"{type(self).__name__}({repr(self.degree)})"

0 commit comments

Comments
 (0)