25
25
from sage .matrix .matrix_space import MatrixSpace
26
26
from sage .misc .lazy_import import lazy_import
27
27
from sage .structure .element import is_Matrix
28
+ from sage .categories .rings import Rings
29
+ from sage .categories .fields import Fields
30
+ from sage .categories .principal_ideal_domains import PrincipalIdealDomains
28
31
from sage .rings .integer_ring import IntegerRing , ZZ
29
- from sage .rings .ring import Ring
30
32
from sage .misc .functional import denominator , is_even
31
33
from sage .arith .misc import GCD
32
34
from sage .arith .functions import lcm as LCM
33
35
from sage .rings .ideal import Ideal
34
36
from sage .rings .rational_field import QQ
35
- from sage .rings .ring import is_Ring , PrincipalIdealDomain
36
37
from sage .structure .element import is_Vector
37
38
from sage .rings .polynomial .polynomial_ring_constructor import PolynomialRing
38
39
from sage .rings .polynomial .polynomial_element import Polynomial
@@ -552,7 +553,7 @@ def __init__(self, R, n=None, entries=None, unsafe_initialization=False, number_
552
553
"""
553
554
# Deal with: QuadraticForm(ring, matrix)
554
555
matrix_init_flag = False
555
- if isinstance ( R , Ring ):
556
+ if R in Rings ( ):
556
557
if is_Matrix (n ):
557
558
# Test if n is symmetric and has even diagonal
558
559
if not self ._is_even_symmetric_matrix_ (n , R ):
@@ -1110,7 +1111,7 @@ def _is_even_symmetric_matrix_(self, A, R=None):
1110
1111
R = A .base_ring ()
1111
1112
ring_coerce_test = False
1112
1113
1113
- if not isinstance ( R , Ring ):
1114
+ if R not in Rings ( ):
1114
1115
raise TypeError ("R is not a ring." )
1115
1116
1116
1117
if not (A .is_square () and A .is_symmetric ()):
@@ -1260,17 +1261,16 @@ def has_integral_Gram_matrix(self):
1260
1261
1261
1262
"""
1262
1263
# Warning over fields
1263
- if self .base_ring (). is_field ():
1264
+ if self .base_ring () in Fields ():
1264
1265
warn ("Warning -- A quadratic form over a field always has integral Gram matrix. Do you really want to do this?!?" )
1265
1266
1266
1267
# Determine integrality of the Gram matrix
1267
- flag = True
1268
1268
try :
1269
1269
self .Gram_matrix ()
1270
- except Exception :
1271
- flag = False
1272
-
1273
- return flag
1270
+ except TypeError :
1271
+ return False
1272
+ else :
1273
+ return True
1274
1274
1275
1275
def gcd (self ):
1276
1276
"""
@@ -1297,9 +1297,9 @@ def polynomial(self, names='x'):
1297
1297
1298
1298
INPUT:
1299
1299
1300
- - ``self`` - a quadratic form over a commutative ring
1300
+ - ``self`` -- a quadratic form over a commutative ring
1301
1301
1302
- - ``names`` - specification of the names of the variables; see :func:`PolynomialRing`
1302
+ - ``names`` -- specification of the names of the variables; see :func:`PolynomialRing`
1303
1303
1304
1304
OUTPUT: The polynomial form of the quadratic form.
1305
1305
@@ -1328,21 +1328,19 @@ def polynomial(self, names='x'):
1328
1328
sage: Q.polynomial()
1329
1329
Traceback (most recent call last):
1330
1330
...
1331
- ValueError: Can only create polynomial rings over commutative rings.
1331
+ ValueError: Can only create polynomial rings over commutative rings
1332
1332
"""
1333
1333
B = self .base_ring ()
1334
+ if B not in Rings ().Commutative ():
1335
+ raise ValueError ('Can only create polynomial rings over commutative rings' )
1334
1336
n = self .dim ()
1335
1337
M = matrix (B , n )
1336
1338
for i in range (n ):
1337
1339
for j in range (i , n ):
1338
1340
M [i , j ] = self [i , j ]
1339
- try :
1340
- R = PolynomialRing (self .base_ring (), names , n )
1341
- except Exception :
1342
- raise ValueError ('Can only create polynomial rings over commutative rings.' )
1341
+ R = PolynomialRing (self .base_ring (), names , n )
1343
1342
V = vector (R .gens ())
1344
- P = (V * M ).dot_product (V )
1345
- return P
1343
+ return (V * M ).dot_product (V )
1346
1344
1347
1345
@staticmethod
1348
1346
def from_polynomial (poly ):
@@ -1568,7 +1566,7 @@ def change_ring(self, R):
1568
1566
1
1569
1567
"""
1570
1568
# Check that a canonical coercion is possible
1571
- if not is_Ring ( R ):
1569
+ if R not in Rings ( ):
1572
1570
raise TypeError ("R is not a ring" )
1573
1571
if not R .has_coerce_map_from (self .base_ring ()):
1574
1572
raise TypeError (f"there is no canonical coercion from { self .base_ring ()} to R" )
@@ -1609,11 +1607,11 @@ def level(self):
1609
1607
except AttributeError :
1610
1608
1611
1609
# Check that the base ring is a PID
1612
- if not isinstance ( self .base_ring (), PrincipalIdealDomain ):
1610
+ if self .base_ring () not in PrincipalIdealDomains ( ):
1613
1611
raise TypeError ("the level (as a number) is only defined over a Principal Ideal Domain ; try using level_ideal()" )
1614
1612
1615
1613
# Warn the user if the form is defined over a field!
1616
- if self .base_ring (). is_field ():
1614
+ if self .base_ring () in Fields ():
1617
1615
warn ("Warning -- The level of a quadratic form over a field is always 1. Do you really want to do this?!?" )
1618
1616
# raise RuntimeError("Warning -- The level of a quadratic form over a field is always 1. Do you really want to do this?!?")
1619
1617
0 commit comments