Skip to content

Commit f1b9e57

Browse files
committed
minor cleanup in binary quadratic forms over ZZ
1 parent 26f5a09 commit f1b9e57

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

src/sage/quadratic_forms/binary_qf.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@ def __init__(self, a, b=None, c=None):
137137
"""
138138
from sage.rings.polynomial.multi_polynomial import MPolynomial
139139
if b is None and c is None:
140-
if (isinstance(a, (list, tuple))
141-
and len(a) == 3):
140+
if isinstance(a, (list, tuple)) and len(a) == 3:
142141
a, b, c = a
143142
elif a == 0:
144143
a = b = c = 0
@@ -215,8 +214,8 @@ def __mul__(self, right):
215214
return BinaryQF(self.__pari__().qfbcompraw(right))
216215
# ...or a 2x2 matrix...
217216
if (isinstance(right.parent(), MatrixSpace)
218-
and right.nrows() == right.ncols() == 2):
219-
aa,bb,cc,dd = right.list()
217+
and right.nrows() == right.ncols() == 2):
218+
aa, bb, cc, dd = right.list()
220219
A = self.polynomial()(aa, cc)
221220
C = self.polynomial()(bb, dd)
222221
B = self.polynomial()(aa + bb, cc + dd) - A - C
@@ -536,10 +535,10 @@ def from_polynomial(poly):
536535
if not isinstance(R, MPolynomialRing_base) or R.ngens() != 2:
537536
raise TypeError(f'not a bivariate polynomial ring: {R}')
538537
if not all(mon.degree() == 2 for mon in poly.monomials()):
539-
raise ValueError(f'polynomial has monomials of degree != 2')
540-
x,y = R.gens()
538+
raise ValueError('polynomial has monomials of degree != 2')
539+
x, y = R.gens()
541540
coeffs = (poly.monomial_coefficient(mon) for mon in (x**2, x*y, y**2))
542-
a,b,c = map(ZZ, coeffs)
541+
a, b, c = map(ZZ, coeffs)
543542
return BinaryQF(a, b, c)
544543

545544
@cached_method
@@ -924,7 +923,7 @@ def reduced_form(self, transformation=False, algorithm="default"):
924923
'supported using PARI')
925924

926925
if transformation:
927-
y,g = self.__pari__().qfbredsl2()
926+
y, g = self.__pari__().qfbredsl2()
928927
return BinaryQF(y), Matrix(ZZ, g)
929928
return BinaryQF(self.__pari__().qfbred())
930929

@@ -1153,7 +1152,7 @@ def cycle(self, proper=False):
11531152
'implemented for non-square discriminants')
11541153
if proper:
11551154
# Prop 6.10.5 in Buchmann Vollmer
1156-
C = list(self.cycle(proper=False)) # make a copy so we can modify it
1155+
C = list(self.cycle(proper=False)) # make a copy that we can modify
11571156
if len(C) % 2:
11581157
C += C
11591158
for i in range(len(C)//2):
@@ -1316,7 +1315,7 @@ def is_equivalent(self, other, proper=True):
13161315
sage: Q1.is_equivalent(Q2, proper=True) # optional - sage.libs.pari
13171316
True
13181317
"""
1319-
if type(other) != type(self):
1318+
if not isinstance(other, BinaryQF):
13201319
raise TypeError("%s is not a BinaryQF" % other)
13211320
if self.discriminant() != other.discriminant():
13221321
return False
@@ -1342,7 +1341,7 @@ def is_equivalent(self, other, proper=True):
13421341
return is_properly_equiv
13431342
else:
13441343
g = gcd(a, b)
1345-
return is_properly_equiv or ((gcd(ao,b) == g) and ((a*ao - g**2) % (b*g) == 0))
1344+
return is_properly_equiv or ((gcd(ao, b) == g) and ((a*ao - g**2) % (b*g) == 0))
13461345

13471346
proper_cycle = otherred.cycle(proper=True)
13481347

@@ -1654,13 +1653,13 @@ def solve_integer(self, n, *, algorithm="general"):
16541653
# https://math.stackexchange.com/a/980075
16551654
w = self.discriminant().sqrt()
16561655
r = (-self._b + (w if w != self._b else -w)) / (2*self._a)
1657-
p,q = r.as_integer_ratio()
1658-
g,u,v = p.xgcd(q)
1659-
M = Matrix(ZZ, [[v,p],[-u,q]])
1656+
p, q = r.as_integer_ratio()
1657+
g, u, v = p.xgcd(q)
1658+
M = Matrix(ZZ, [[v, p], [-u, q]])
16601659
elif self._c:
1661-
M = Matrix(ZZ, [[0,1],[1,0]])
1660+
M = Matrix(ZZ, [[0, 1], [1, 0]])
16621661
else:
1663-
M = Matrix(ZZ, [[1,0],[0,1]])
1662+
M = Matrix(ZZ, [[1, 0], [0, 1]])
16641663
assert M.is_unit()
16651664
Q = self.matrix_action_right(M)
16661665
assert not Q._c
@@ -1845,7 +1844,7 @@ def BinaryQF_reduced_representatives(D, primitive_only=False, proper=True):
18451844
c = ZZ(0)
18461845
# -b/2 < a <= b/2
18471846
for a in xsrange((-b/2).floor() + 1, (b/2).floor() + 1):
1848-
if (not primitive_only) or (gcd([a,b,c]) == 1):
1847+
if not primitive_only or (gcd([a, b, c]) == 1):
18491848
form_list.append(BinaryQF(a, b, c))
18501849
# We follow the description of Buchmann/Vollmer 6.7.1. They
18511850
# enumerate all reduced forms. We only want representatives.
@@ -1879,14 +1878,14 @@ def BinaryQF_reduced_representatives(D, primitive_only=False, proper=True):
18791878
a4 = 4*a
18801879
s = D + a*a4
18811880
w = 1+(s-1).isqrt() if s > 0 else 0
1882-
if w%2 != D%2:
1881+
if w % 2 != D % 2:
18831882
w += 1
18841883
for b in xsrange(w, a+1, 2):
18851884
t = b*b-D
18861885
if t % a4 == 0:
18871886
c = t // a4
1888-
if (not primitive_only) or gcd([a, b, c]) == 1:
1889-
if b>0 and a>b and c>a:
1887+
if not primitive_only or gcd([a, b, c]) == 1:
1888+
if c > a > b > 0:
18901889
form_list.append(BinaryQF([a, -b, c]))
18911890
form_list.append(BinaryQF([a, b, c]))
18921891
if not proper or D > 0:

0 commit comments

Comments
 (0)