Skip to content

Commit 869d241

Browse files
committed
minor details in quadratic forms
1 parent 3dd953c commit 869d241

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

src/sage/quadratic_forms/quadratic_form.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@
2525
from sage.matrix.matrix_space import MatrixSpace
2626
from sage.misc.lazy_import import lazy_import
2727
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
2831
from sage.rings.integer_ring import IntegerRing, ZZ
29-
from sage.rings.ring import Ring
3032
from sage.misc.functional import denominator, is_even
3133
from sage.arith.misc import GCD
3234
from sage.arith.functions import lcm as LCM
3335
from sage.rings.ideal import Ideal
3436
from sage.rings.rational_field import QQ
35-
from sage.rings.ring import is_Ring, PrincipalIdealDomain
3637
from sage.structure.element import is_Vector
3738
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
3839
from sage.rings.polynomial.polynomial_element import Polynomial
@@ -552,7 +553,7 @@ def __init__(self, R, n=None, entries=None, unsafe_initialization=False, number_
552553
"""
553554
# Deal with: QuadraticForm(ring, matrix)
554555
matrix_init_flag = False
555-
if isinstance(R, Ring):
556+
if R in Rings():
556557
if is_Matrix(n):
557558
# Test if n is symmetric and has even diagonal
558559
if not self._is_even_symmetric_matrix_(n, R):
@@ -1110,7 +1111,7 @@ def _is_even_symmetric_matrix_(self, A, R=None):
11101111
R = A.base_ring()
11111112
ring_coerce_test = False
11121113

1113-
if not isinstance(R, Ring):
1114+
if R not in Rings():
11141115
raise TypeError("R is not a ring.")
11151116

11161117
if not (A.is_square() and A.is_symmetric()):
@@ -1260,17 +1261,16 @@ def has_integral_Gram_matrix(self):
12601261
12611262
"""
12621263
# Warning over fields
1263-
if self.base_ring().is_field():
1264+
if self.base_ring() in Fields():
12641265
warn("Warning -- A quadratic form over a field always has integral Gram matrix. Do you really want to do this?!?")
12651266

12661267
# Determine integrality of the Gram matrix
1267-
flag = True
12681268
try:
12691269
self.Gram_matrix()
1270-
except Exception:
1271-
flag = False
1272-
1273-
return flag
1270+
except TypeError:
1271+
return False
1272+
else:
1273+
return True
12741274

12751275
def gcd(self):
12761276
"""
@@ -1297,9 +1297,9 @@ def polynomial(self, names='x'):
12971297
12981298
INPUT:
12991299
1300-
- ``self`` - a quadratic form over a commutative ring
1300+
- ``self`` -- a quadratic form over a commutative ring
13011301
1302-
- ``names`` - specification of the names of the variables; see :func:`PolynomialRing`
1302+
- ``names`` -- specification of the names of the variables; see :func:`PolynomialRing`
13031303
13041304
OUTPUT: The polynomial form of the quadratic form.
13051305
@@ -1328,21 +1328,19 @@ def polynomial(self, names='x'):
13281328
sage: Q.polynomial()
13291329
Traceback (most recent call last):
13301330
...
1331-
ValueError: Can only create polynomial rings over commutative rings.
1331+
ValueError: Can only create polynomial rings over commutative rings
13321332
"""
13331333
B = self.base_ring()
1334+
if B not in Rings().Commutative():
1335+
raise ValueError('Can only create polynomial rings over commutative rings')
13341336
n = self.dim()
13351337
M = matrix(B, n)
13361338
for i in range(n):
13371339
for j in range(i, n):
13381340
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)
13431342
V = vector(R.gens())
1344-
P = (V*M).dot_product(V)
1345-
return P
1343+
return (V * M).dot_product(V)
13461344

13471345
@staticmethod
13481346
def from_polynomial(poly):
@@ -1568,7 +1566,7 @@ def change_ring(self, R):
15681566
1
15691567
"""
15701568
# Check that a canonical coercion is possible
1571-
if not is_Ring(R):
1569+
if R not in Rings():
15721570
raise TypeError("R is not a ring")
15731571
if not R.has_coerce_map_from(self.base_ring()):
15741572
raise TypeError(f"there is no canonical coercion from {self.base_ring()} to R")
@@ -1609,11 +1607,11 @@ def level(self):
16091607
except AttributeError:
16101608

16111609
# Check that the base ring is a PID
1612-
if not isinstance(self.base_ring(), PrincipalIdealDomain):
1610+
if self.base_ring() not in PrincipalIdealDomains():
16131611
raise TypeError("the level (as a number) is only defined over a Principal Ideal Domain ; try using level_ideal()")
16141612

16151613
# 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():
16171615
warn("Warning -- The level of a quadratic form over a field is always 1. Do you really want to do this?!?")
16181616
# raise RuntimeError("Warning -- The level of a quadratic form over a field is always 1. Do you really want to do this?!?")
16191617

0 commit comments

Comments
 (0)