Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit 570fb63

Browse files
fchapotonguojing0
authored andcommitted
clean-up two files in quadratic forms
1 parent cb65d06 commit 570fb63

File tree

2 files changed

+82
-85
lines changed

2 files changed

+82
-85
lines changed

src/sage/quadratic_forms/genera/spinor_genus.py

+26-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
r"""
22
Spinor genus computations.
33
4-
This file defines the group of spinor operators used for the computation of spinor genera.
4+
This file defines the group of spinor operators used
5+
for the computation of spinor genera.
56
It is meant for internal use only.
67
78
EXAMPLES::
@@ -24,8 +25,10 @@
2425
# https://www.gnu.org/licenses/
2526
# ****************************************************************************
2627

27-
from sage.groups.abelian_gps.abelian_group_gap import AbelianGroupGap, AbelianGroupElement_gap
28-
from sage.rings.all import ZZ,QQ
28+
from sage.groups.abelian_gps.abelian_group_gap import (AbelianGroupGap,
29+
AbelianGroupElement_gap)
30+
from sage.rings.all import ZZ, QQ
31+
2932

3033
class SpinorOperator(AbelianGroupElement_gap):
3134
r"""
@@ -42,7 +45,7 @@ class SpinorOperator(AbelianGroupElement_gap):
4245
[2:7, 3:-1, 7:-1]
4346
"""
4447

45-
def _repr_(self):
48+
def _repr_(self) -> str:
4649
r"""
4750
Return the print representation.
4851
@@ -64,8 +67,8 @@ def _repr_(self):
6467
s += "5"
6568
elif e[0] == 1 and e[1] == 1:
6669
s += "7"
67-
for k in range(1,len(p)):
68-
s += ", %s:%s"%(p[k], (-1)**e[k+1])
70+
for k in range(1, len(p)):
71+
s += ", %s:%s" % (p[k], (-1)**e[k + 1])
6972
s += "]"
7073
return s
7174

@@ -74,7 +77,8 @@ class SpinorOperators(AbelianGroupGap):
7477
r"""
7578
The group of spinor operators of a genus.
7679
77-
It is a product of `p`-adic unit square classes used for spinor genus computations.
80+
It is a product of `p`-adic unit square classes
81+
used for spinor genus computations.
7882
7983
INPUT:
8084
@@ -88,7 +92,7 @@ class SpinorOperators(AbelianGroupGap):
8892
"""
8993
def __init__(self, primes):
9094
r"""
91-
Initialize the group of spinor operators
95+
Initialize the group of spinor operators.
9296
9397
TESTS::
9498
@@ -99,7 +103,7 @@ def __init__(self, primes):
99103
if primes[0] != 2:
100104
raise ValueError("first prime must be 2")
101105
self._primes = tuple(ZZ(p) for p in primes)
102-
orders = len(self._primes)*[2] + [2]
106+
orders = len(self._primes) * [2] + [2]
103107
# 3, 5, unit_p1, unit_p2,...
104108
orders = tuple(orders)
105109
AbelianGroupGap.__init__(self, orders)
@@ -123,7 +127,7 @@ def __reduce__(self):
123127

124128
Element = SpinorOperator
125129

126-
def _repr_(self):
130+
def _repr_(self) -> str:
127131
r"""
128132
Return the print representation of ``self``.
129133
@@ -137,7 +141,7 @@ def _repr_(self):
137141

138142
def to_square_class(self, x, p):
139143
r"""
140-
Return `(1, ..., 1, x, 1, ..., 1)` with the square class of `x` at position `p`.
144+
Return `(1,...,1,x,1,...,1)` with the square class of `x` at position `p`.
141145
142146
INPUT:
143147
@@ -166,7 +170,7 @@ def to_square_class(self, x, p):
166170
v, u = x.val_unit(p)
167171
v = v % 2
168172
if v != 0:
169-
raise ValueError("x(=%s) must be a p-adic unit" % x)
173+
raise ValueError(f"x(={x}) must be a p-adic unit")
170174
y = self.one()
171175
if p == 2:
172176
u = u % 8
@@ -189,7 +193,8 @@ def delta(self, r, prime=None):
189193
INPUT:
190194
191195
- ``r`` -- a non zero integer;
192-
if ``prime`` is ``None``, ``r`` must not be divisible by the defining primes of ``self``
196+
if ``prime`` is ``None``, ``r`` must not be divisible
197+
by the defining primes of ``self``
193198
194199
- ``prime`` --(default:``None``) a prime or `-1`
195200
@@ -217,17 +222,20 @@ def delta(self, r, prime=None):
217222
r = ZZ(r)
218223
if prime is None:
219224
if any(p.divides(r) for p in self._primes):
220-
raise ValueError("r must not be divisible by %s"%(self._primes,))
221-
return self.prod([self.to_square_class(r, p) for p in self._primes])
225+
raise ValueError(f"r must not be divisible by {self._primes}")
226+
return self.prod([self.to_square_class(r, p)
227+
for p in self._primes])
222228
prime = ZZ(prime)
223229
if prime == -1:
224230
r = r.sign()
225-
return self.prod([self.to_square_class(r, p) for p in self._primes])
231+
return self.prod([self.to_square_class(r, p)
232+
for p in self._primes])
226233
if prime not in self._primes:
227-
raise ValueError("prime must be among %s"%self._primes)
234+
raise ValueError("prime must be among %s" % self._primes)
228235
v, u = r.val_unit(prime)
229236
pv = prime**v
230-
y = self.prod([self.to_square_class(pv,q) for q in self._primes if q!=prime])
237+
y = self.prod([self.to_square_class(pv, q)
238+
for q in self._primes if q != prime])
231239
if prime in self._primes:
232240
y *= self.to_square_class(u, p=prime)
233241
return y
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
"""
2-
Creating A Random Quadratic Form
2+
Random Quadratic Forms
3+
4+
This file contains a set of routines to create a random quadratic form.
35
"""
46
from sage.quadratic_forms.quadratic_form import QuadraticForm
57
from sage.quadratic_forms.ternary_qf import TernaryQF
68
from sage.rings.ring import is_Ring
79
from sage.rings.integer_ring import ZZ
810

11+
912
################################################
10-
## Routines to create a random quadratic form ##
13+
# Routines to create a random quadratic form ##
1114
################################################
1215

1316
def random_quadraticform(R, n, rand_arg_list=[]):
@@ -31,51 +34,47 @@ def random_quadraticform(R, n, rand_arg_list=[]):
3134
3235
EXAMPLES::
3336
34-
sage: random_quadraticform(ZZ, 3, [1,5]) ## RANDOM
37+
sage: random_quadraticform(ZZ, 3, [1,5]) # random
3538
Quadratic form in 3 variables over Integer Ring with coefficients:
3639
[ 3 2 3 ]
3740
[ * 1 4 ]
3841
[ * * 3 ]
3942
40-
::
41-
42-
sage: random_quadraticform(ZZ, 3, [-5,5]) ## RANDOM
43+
sage: random_quadraticform(ZZ, 3, [-5,5]) # random
4344
Quadratic form in 3 variables over Integer Ring with coefficients:
4445
[ 3 2 -5 ]
4546
[ * 2 -2 ]
4647
[ * * -5 ]
4748
48-
::
49-
50-
sage: random_quadraticform(ZZ, 3, [-50,50]) ## RANDOM
49+
sage: random_quadraticform(ZZ, 3, [-50,50]) # random
5150
Quadratic form in 3 variables over Integer Ring with coefficients:
5251
[ 1 8 -23 ]
5352
[ * 0 0 ]
5453
[ * * 6 ]
54+
55+
TESTS::
56+
57+
sage: random_quadraticform(ZZ, 3, [1,2,3,4])
58+
Traceback (most recent call last):
59+
...
60+
TypeError: the list of randomness arguments can have at most 3 elements
5561
"""
56-
## Sanity Checks: We have a ring and there are at most 3 parameters for randomness!
5762
if len(rand_arg_list) > 3:
58-
raise TypeError("Oops! The list of randomness arguments can have at most 3 elements.")
63+
raise TypeError("the list of randomness arguments can have "
64+
"at most 3 elements")
5965
if not is_Ring(R):
60-
raise TypeError("Oops! The first argument must be a ring.")
61-
62-
## Create a list of upper-triangular entries for the quadratic form
63-
L = len(rand_arg_list)
64-
nn = int(n*(n+1)/2)
65-
if L == 0:
66-
rand_list = [R.random_element() for _ in range(nn)]
67-
elif L == 1:
68-
rand_list = [R.random_element(rand_arg_list[0]) for _ in range(nn)]
69-
elif L == 2:
70-
rand_list = [R.random_element(rand_arg_list[0], rand_arg_list[1]) for _ in range(nn)]
71-
elif L == 3:
72-
rand_list = [R.random_element(rand_arg_list[0], rand_arg_list[1], rand_arg_list[2]) for _ in range(nn)]
73-
74-
## Return the Quadratic Form
66+
raise TypeError("the first argument must be a ring")
67+
# Create a list of upper-triangular entries for the quadratic form
68+
n2 = (n * (n + 1)) // 2
69+
if not rand_arg_list:
70+
rand_list = [R.random_element() for _ in range(n2)]
71+
else:
72+
rand_list = [R.random_element(*rand_arg_list) for _ in range(n2)]
7573
return QuadraticForm(R, n, rand_list)
7674

7775

78-
def random_quadraticform_with_conditions(R, n, condition_list=[], rand_arg_list=[]):
76+
def random_quadraticform_with_conditions(R, n, condition_list=[],
77+
rand_arg_list=[]):
7978
"""
8079
Create a random quadratic form in `n` variables defined over the ring `R`
8180
satisfying a list of boolean (i.e. True/False) conditions.
@@ -90,38 +89,39 @@ def random_quadraticform_with_conditions(R, n, condition_list=[], rand_arg_list=
9089
9190
EXAMPLES::
9291
93-
sage: Q = random_quadraticform_with_conditions(ZZ, 3, [QuadraticForm.is_positive_definite], [-5, 5])
94-
sage: Q ## RANDOM
92+
sage: check = QuadraticForm.is_positive_definite
93+
sage: Q = random_quadraticform_with_conditions(ZZ, 3, [check], [-5, 5])
94+
sage: Q # random
9595
Quadratic form in 3 variables over Integer Ring with coefficients:
9696
[ 3 -2 -5 ]
9797
[ * 2 2 ]
9898
[ * * 3 ]
99-
10099
"""
101100
Q = random_quadraticform(R, n, rand_arg_list)
102-
Done_Flag = True
101+
done_flag = True
103102

104-
## Check that all conditions are satisfied
105-
while Done_Flag:
106-
Done_Flag = False
103+
# Check that all conditions are satisfied
104+
while done_flag:
105+
done_flag = False
107106
for c in condition_list:
108107

109-
## Check if condition c is satisfied
108+
# Check if condition c is satisfied
110109
try:
111110
bool_ans = Q.c()
112111
except Exception:
113112
bool_ans = c(Q)
114113

115-
## Create a new quadratic form if a condition fails
114+
# Create a new quadratic form if a condition fails
116115
if not bool_ans:
117116
Q = random_quadraticform(R, n, rand_arg_list)
118-
Done_Flag = True
117+
done_flag = True
119118
break
120119

121-
## Return the quadratic form
120+
# Return the quadratic form
122121
return Q
123122

124-
def random_ternaryqf(rand_arg_list = []):
123+
124+
def random_ternaryqf(rand_arg_list=[]):
125125
"""
126126
Create a random ternary quadratic form.
127127
@@ -140,37 +140,27 @@ def random_ternaryqf(rand_arg_list = []):
140140
141141
EXAMPLES::
142142
143-
sage: random_ternaryqf() ##RANDOM
143+
sage: random_ternaryqf() # random
144144
Ternary quadratic form with integer coefficients:
145145
[1 1 4]
146146
[-1 1 -1]
147-
sage: random_ternaryqf([-1, 2]) ##RANDOM
147+
sage: random_ternaryqf([-1, 2]) # random
148148
Ternary quadratic form with integer coefficients:
149149
[1 0 1]
150150
[-1 -1 -1]
151-
sage: random_ternaryqf([-10, 10, "uniform"]) ##RANDOM
151+
sage: random_ternaryqf([-10, 10, "uniform"]) # random
152152
Ternary quadratic form with integer coefficients:
153153
[7 -8 2]
154154
[0 3 -6]
155155
"""
156-
157-
158156
R = ZZ
159-
n = 6
160-
L = len(rand_arg_list)
161-
if L == 0:
162-
rand_list = [ R.random_element() for _ in range(n)]
163-
elif L == 1:
164-
rand_list = [ R.random_element(rand_arg_list[0]) for _ in range(6)]
165-
elif L == 2:
166-
rand_list = [ R.random_element(rand_arg_list[0], rand_arg_list[1]) for _ in range(6)]
167-
elif L == 3:
168-
rand_list = [ R.random_element(rand_arg_list[0], rand_arg_list[1], rand_arg_list[2]) for _ in range(6)]
169-
157+
if not rand_arg_list:
158+
rand_list = [R.random_element() for _ in range(6)]
159+
else:
160+
rand_list = [R.random_element(*rand_arg_list) for _ in range(6)]
170161
return TernaryQF(rand_list)
171162

172163

173-
174164
def random_ternaryqf_with_conditions(condition_list=[], rand_arg_list=[]):
175165
"""
176166
Create a random ternary quadratic form satisfying a list of boolean
@@ -186,30 +176,29 @@ def random_ternaryqf_with_conditions(condition_list=[], rand_arg_list=[]):
186176
187177
EXAMPLES::
188178
189-
sage: Q = random_ternaryqf_with_conditions([TernaryQF.is_positive_definite], [-5, 5])
190-
sage: Q ## RANDOM
179+
sage: check = TernaryQF.is_positive_definite
180+
sage: Q = random_ternaryqf_with_conditions([check], [-5, 5])
181+
sage: Q # random
191182
Ternary quadratic form with integer coefficients:
192183
[3 4 2]
193184
[2 -2 -1]
194185
"""
195-
196186
Q = random_ternaryqf(rand_arg_list)
197-
Done_Flag = True
187+
done_flag = True
198188

199-
## Check that all conditions are satisfied
200-
while Done_Flag:
201-
Done_Flag = False
189+
# Check that all conditions are satisfied
190+
while done_flag:
191+
done_flag = False
202192
for c in condition_list:
203-
204-
## Check if condition c is satisfied
193+
# Check if condition c is satisfied
205194
try:
206195
bool_ans = Q.c()
207196
except Exception:
208197
bool_ans = c(Q)
209198

210-
## Create a new quadratic form if a condition fails
199+
# Create a new quadratic form if a condition fails
211200
if not bool_ans:
212201
Q = random_ternaryqf(rand_arg_list)
213-
Done_Flag = True
202+
done_flag = True
214203
break
215204
return Q

0 commit comments

Comments
 (0)