@@ -125,7 +125,7 @@ def quadratic_form_from_invariants(F, rk, det, P, sminus):
125
125
"""
126
126
from sage .arith .misc import hilbert_symbol
127
127
# normalize input
128
- if F != QQ :
128
+ if F != QQ :
129
129
raise NotImplementedError ('base field must be QQ. If you want this over any field, implement weak approximation.' )
130
130
P = [ZZ (p ) for p in P ]
131
131
rk = ZZ (rk )
@@ -153,33 +153,33 @@ def quadratic_form_from_invariants(F, rk, det, P, sminus):
153
153
else :
154
154
a = ZZ (1 )
155
155
elif rk == 3 :
156
- Pprime = [p for p in P if hilbert_symbol (- 1 , - d , p )== 1 ]
157
- Pprime += [p for p in (2 * d ).prime_divisors ()
158
- if hilbert_symbol (- 1 , - d , p )== - 1 and p not in P ]
156
+ Pprime = [p for p in P if hilbert_symbol (- 1 , - d , p ) == 1 ]
157
+ Pprime += [p for p in (2 * d ).prime_divisors ()
158
+ if hilbert_symbol (- 1 , - d , p ) == - 1 and p not in P ]
159
159
if sminus > 0 :
160
160
a = ZZ (- 1 )
161
161
else :
162
162
a = ZZ (1 )
163
163
for p in Pprime :
164
164
if d .valuation (p ) % 2 == 0 :
165
165
a *= p
166
- assert all ((a * d ).valuation (p )% 2 == 1 for p in Pprime )
166
+ assert all ((a * d ).valuation (p ) % 2 == 1 for p in Pprime )
167
167
elif rk == 2 :
168
168
S = P
169
169
if sminus == 2 :
170
170
S += [- 1 ]
171
- a = QQ .hilbert_symbol_negative_at_S (S ,- d )
171
+ a = QQ .hilbert_symbol_negative_at_S (S , - d )
172
172
a = ZZ (a )
173
173
P = ([p for p in P if hilbert_symbol (a , - d , p ) == 1 ]
174
- + [p for p in (2 * a * d ).prime_divisors ()
175
- if hilbert_symbol (a , - d , p )== - 1 and p not in P ])
176
- sminus = max (0 , sminus - 1 )
174
+ + [p for p in (2 * a * d ).prime_divisors ()
175
+ if hilbert_symbol (a , - d , p ) == - 1 and p not in P ])
176
+ sminus = max (0 , sminus - 1 )
177
177
rk = rk - 1
178
- d = a * d
178
+ d = a * d
179
179
D .append (a .squarefree_part ())
180
180
d = d .squarefree_part ()
181
181
D .append (d )
182
- return DiagonalQuadraticForm (QQ ,D )
182
+ return DiagonalQuadraticForm (QQ , D )
183
183
184
184
185
185
class QuadraticForm (SageObject ):
@@ -606,10 +606,10 @@ def __init__(self, R, n=None, entries=None, unsafe_initialization=False, number_
606
606
self .__coeffs = []
607
607
for i in range (M .nrows ()):
608
608
for j in range (i , M .nrows ()):
609
- if ( i == j ) :
610
- self .__coeffs += [ M_ring (M [i ,j ] / 2 ) ]
609
+ if i == j :
610
+ self .__coeffs += [M_ring (M [i , j ] / 2 )]
611
611
else :
612
- self .__coeffs += [ M_ring (M [i ,j ]) ]
612
+ self .__coeffs += [M_ring (M [i , j ])]
613
613
614
614
return
615
615
@@ -649,8 +649,8 @@ def __init__(self, R, n=None, entries=None, unsafe_initialization=False, number_
649
649
# Set the number of automorphisms
650
650
if number_of_automorphisms is not None :
651
651
self .set_number_of_automorphisms (number_of_automorphisms )
652
- #self.__number_of_automorphisms = number_of_automorphisms
653
- #self.__external_initialization_list.append('number_of_automorphisms')
652
+ # self.__number_of_automorphisms = number_of_automorphisms
653
+ # self.__external_initialization_list.append('number_of_automorphisms')
654
654
655
655
# Set the determinant
656
656
if determinant is not None :
@@ -827,7 +827,7 @@ def __setitem__(self, ij, coeff):
827
827
828
828
# Set the entry
829
829
try :
830
- self .__coeffs [i * self .__n - i * (i - 1 )// 2 + j - i ] = self .__base_ring (coeff )
830
+ self .__coeffs [i * self .__n - i * (i - 1 )// 2 + j - i ] = self .__base_ring (coeff )
831
831
except Exception :
832
832
raise RuntimeError ("this coefficient cannot be coerced to an element of the base ring for the quadratic form" )
833
833
@@ -934,47 +934,42 @@ def sum_by_coefficients_with(self, right):
934
934
"""
935
935
if not isinstance (right , QuadraticForm ):
936
936
raise TypeError ("cannot add these objects since they are not both quadratic forms" )
937
- elif ( self .__n != right .__n ) :
937
+ elif self .__n != right .__n :
938
938
raise TypeError ("cannot add these since the quadratic forms do not have the same sizes" )
939
- elif ( self .__base_ring != right .__base_ring ) :
939
+ elif self .__base_ring != right .__base_ring :
940
940
raise TypeError ("cannot add these since the quadratic forms do not have the same base rings" )
941
- return QuadraticForm (self .__base_ring , self .__n , [self .__coeffs [i ] + right .__coeffs [i ] for i in range (len (self .__coeffs ))])
942
-
943
- # ======================== CHANGE THIS TO A TENSOR PRODUCT?!? Even in Characteristic 2?!? =======================
944
- # def __mul__(self, right):
945
- # """
946
- # Multiply (on the right) the quadratic form Q by an element of the ring that Q is defined over.
947
- #
948
- # EXAMPLES::
949
- #
950
- # sage: Q = QuadraticForm(ZZ, 2, [1,4,10])
951
- # sage: Q*2
952
- # Quadratic form in 2 variables over Integer Ring with coefficients:
953
- # [ 2 8 ]
954
- # [ * 20 ]
955
- #
956
- # sage: Q+Q == Q*2
957
- # True
958
- #
959
- # """
960
- # try:
961
- # c = self.base_ring()(right)
962
- # except Exception:
963
- # raise TypeError, "Oh no! The multiplier cannot be coerced into the base ring of the quadratic form. =("
964
- #
965
- # return QuadraticForm(self.base_ring(), self.dim(), [c * self.__coeffs[i] for i in range(len(self.__coeffs))])
966
-
967
- # =================================================================
941
+ return QuadraticForm (self .__base_ring , self .__n , [self .__coeffs [i ] + right .__coeffs [i ] for i in range (len (self .__coeffs ))])
942
+
943
+ # ======================== CHANGE THIS TO A TENSOR PRODUCT?!? Even in Characteristic 2?!? =======================
944
+ # def __mul__(self, right):
945
+ # """
946
+ # Multiply (on the right) the quadratic form Q by an element of the ring that Q is defined over.
947
+ #
948
+ # EXAMPLES::
949
+ #
950
+ # sage: Q = QuadraticForm(ZZ, 2, [1,4,10])
951
+ # sage: Q*2
952
+ # Quadratic form in 2 variables over Integer Ring with coefficients:
953
+ # [ 2 8 ]
954
+ # [ * 20 ]
955
+ #
956
+ # sage: Q+Q == Q*2
957
+ # True
958
+ # """
959
+ # try:
960
+ # c = self.base_ring()(right)
961
+ # except Exception:
962
+ # raise TypeError("the multiplier cannot be coerced into the base ring of the quadratic form")
963
+ # return QuadraticForm(self.base_ring(), self.dim(), [c * self.__coeffs[i] for i in range(len(self.__coeffs))])
968
964
969
965
def __call__ (self , v ):
970
966
r"""
971
967
Evaluate this quadratic form `Q` on a vector or matrix of elements
972
968
coercible to the base ring of the quadratic form.
973
969
974
- If a vector
975
- is given then the output will be the ring element `Q(v)`, but if a
976
- matrix is given then the output will be the quadratic form `Q'`
977
- which in matrix notation is given by:
970
+ If a vector is given then the output will be the ring element
971
+ `Q(v)`, but if a matrix is given then the output will be the
972
+ quadratic form `Q'` which in matrix notation is given by:
978
973
979
974
.. MATH::
980
975
@@ -1116,23 +1111,17 @@ def _is_even_symmetric_matrix_(self, A, R=None):
1116
1111
if not isinstance (R , Ring ):
1117
1112
raise TypeError ("R is not a ring." )
1118
1113
1119
- if not A .is_square ():
1114
+ if not ( A .is_square () and A . is_symmetric () ):
1120
1115
return False
1121
1116
1122
- # Test that the matrix is symmetric
1123
- n = A .nrows ()
1124
- for i in range (n ):
1125
- for j in range (i + 1 , n ):
1126
- if A [i ,j ] != A [j ,i ]:
1127
- return False
1128
-
1129
1117
# Test that all entries coerce to R
1118
+ n = A .nrows ()
1130
1119
if not ((A .base_ring () == R ) or ring_coerce_test ):
1131
1120
try :
1132
1121
for i in range (n ):
1133
1122
for j in range (i , n ):
1134
- R (A [i ,j ])
1135
- except Exception :
1123
+ R (A [i , j ])
1124
+ except ( TypeError , ValueError ) :
1136
1125
return False
1137
1126
1138
1127
# Test that the diagonal is even (if 1/2 isn't in R)
@@ -1180,10 +1169,10 @@ def Hessian_matrix(self):
1180
1169
mat_entries = []
1181
1170
for i in range (self .dim ()):
1182
1171
for j in range (self .dim ()):
1183
- if ( i == j ) :
1184
- mat_entries += [ 2 * self [i ,j ] ]
1172
+ if i == j :
1173
+ mat_entries += [2 * self [i , j ] ]
1185
1174
else :
1186
- mat_entries += [ self [i ,j ] ]
1175
+ mat_entries += [self [i , j ] ]
1187
1176
1188
1177
return matrix (self .base_ring (), self .dim (), self .dim (), mat_entries )
1189
1178
@@ -1384,11 +1373,11 @@ def from_polynomial(poly):
1384
1373
if not isinstance (R , MPolynomialRing_base ):
1385
1374
raise TypeError (f'not a multivariate polynomial ring: { R } ' )
1386
1375
if not all (mon .degree () == 2 for mon in poly .monomials ()):
1387
- raise ValueError (f 'polynomial has monomials of degree != 2' )
1376
+ raise ValueError ('polynomial has monomials of degree != 2' )
1388
1377
base = R .base_ring ()
1389
1378
vs = R .gens ()
1390
1379
coeffs = []
1391
- for i ,v in enumerate (vs ):
1380
+ for i , v in enumerate (vs ):
1392
1381
for w in vs [i :]:
1393
1382
coeffs .append (poly .monomial_coefficient (v * w ))
1394
1383
return QuadraticForm (base , len (vs ), coeffs )
@@ -1625,7 +1614,7 @@ def level(self):
1625
1614
# Warn the user if the form is defined over a field!
1626
1615
if self .base_ring ().is_field ():
1627
1616
warn ("Warning -- The level of a quadratic form over a field is always 1. Do you really want to do this?!?" )
1628
- #raise RuntimeError, "Warning -- The level of a quadratic form over a field is always 1. Do you really want to do this?!?"
1617
+ # raise RuntimeError( "Warning -- The level of a quadratic form over a field is always 1. Do you really want to do this?!?")
1629
1618
1630
1619
# Check invertibility and find the inverse
1631
1620
try :
@@ -1747,7 +1736,7 @@ def bilinear_map(self, v, w):
1747
1736
raise TypeError ("vectors must have length " + str (self .dim ()))
1748
1737
if self .base_ring ().characteristic () == 2 :
1749
1738
raise TypeError ("not defined for rings of characteristic 2" )
1750
- return (self (v + w ) - self (v ) - self (w ))/ 2
1739
+ return (self (v + w ) - self (v ) - self (w )) / 2
1751
1740
1752
1741
1753
1742
def DiagonalQuadraticForm (R , diag ):
0 commit comments