Skip to content

Commit d871894

Browse files
author
Release Manager
committed
Trac #34195: sage.geometry.polyhedron: More # optional - sage.rings.number_field
We also remove some module-level imports of `AA` etc. Split out from #32432. URL: https://trac.sagemath.org/34195 Reported by: mkoeppe Ticket author(s): Matthias Koeppe Reviewer(s): Jonathan Kliem
2 parents b8a75b6 + da3d534 commit d871894

File tree

9 files changed

+103
-72
lines changed

9 files changed

+103
-72
lines changed

src/sage/geometry/convex_set.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -873,10 +873,14 @@ def _test_contains(self, tester=None, **options):
873873
tester.assertEqual(contains_space_point, self.contains(ambient_point))
874874
tester.assertEqual(contains_space_point, self.contains(space_coords))
875875
if space.base_ring().is_exact():
876-
from sage.rings.qqbar import AA
877-
ext_space = self.ambient_vector_space(AA)
878-
ext_space_point = ext_space(space_point)
879-
tester.assertEqual(contains_space_point, self.contains(ext_space_point))
876+
try:
877+
from sage.rings.qqbar import AA
878+
except ImportError:
879+
pass
880+
else:
881+
ext_space = self.ambient_vector_space(AA)
882+
ext_space_point = ext_space(space_point)
883+
tester.assertEqual(contains_space_point, self.contains(ext_space_point))
880884
try:
881885
from sage.symbolic.ring import SR
882886
symbolic_space = self.ambient_vector_space(SR)

src/sage/geometry/polyhedron/backend_normaliz.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
lazy_import('PyNormaliz', ['NmzResult', 'NmzCompute', 'NmzCone', 'NmzConeCopy'],
3131
feature=sage.features.normaliz.PyNormaliz())
3232

33-
from sage.rings.all import ZZ, QQ, QQbar
34-
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
33+
from sage.rings.integer_ring import ZZ
34+
from sage.rings.rational_field import QQ
3535
from sage.arith.functions import LCM_list
3636
from sage.misc.functional import denominator
3737
from sage.matrix.constructor import vector
@@ -1082,10 +1082,11 @@ def _number_field_triple(normaliz_field):
10821082
sage: Pn._number_field_triple(QuadraticField(5)) # optional - sage.rings.number_field
10831083
['a^2 - 5', 'a', '[2.236067977499789 +/- 8.06e-16]']
10841084
"""
1085-
from sage.rings.real_arb import RealBallField
10861085
R = normaliz_field
10871086
if R is QQ:
10881087
return None
1088+
from sage.rings.real_arb import RealBallField
1089+
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
10891090
emb = RealBallField(53)(R.gen(0))
10901091
gen = 'a'
10911092
R_a = PolynomialRing(QQ, gen)
@@ -2336,8 +2337,9 @@ class functions.
23362337
((t^4 + 3*t^3 + 8*t^2 + 3*t + 1)/(t + 1), (3*t^3 + 2*t^2 + 3*t)/(t + 1))
23372338
"""
23382339
from sage.groups.conjugacy_classes import ConjugacyClassGAP
2339-
from sage.rings.all import CyclotomicField
2340-
from sage.matrix.all import MatrixSpace
2340+
from sage.rings.number_field.number_field import CyclotomicField
2341+
from sage.rings.qqbar import QQbar
2342+
from sage.matrix.matrix_space import MatrixSpace
23412343
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
23422344
from sage.matrix.special import identity_matrix
23432345
# Setting the group
@@ -2475,6 +2477,8 @@ def _Hstar_as_rat_fct(self, initial_Hstar):
24752477
[ 1 1 -1 -1 1]
24762478
[ 2 0 0 0 -2]
24772479
"""
2480+
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
2481+
from sage.rings.qqbar import QQbar
24782482
chi_vars = ','.join('chi_{}'.format(i) for i in range(len(initial_Hstar)))
24792483
Chi_ring = PolynomialRing(QQbar, chi_vars)
24802484
virtual_ring = PolynomialRing(Chi_ring, initial_Hstar.base_ring().gens())

src/sage/geometry/polyhedron/base0.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -1366,11 +1366,9 @@ def cdd_Hrepresentation(self):
13661366
except AttributeError:
13671367
from sage.rings.integer_ring import ZZ
13681368
from sage.rings.rational_field import QQ
1369-
from sage.rings.real_double import RDF
1370-
13711369
if self.base_ring() is ZZ or self.base_ring() is QQ:
13721370
cdd_type = 'rational'
1373-
elif self.base_ring() is RDF:
1371+
elif isinstance(self.base_ring(), sage.rings.abc.RealDoubleField):
13741372
cdd_type = 'real'
13751373
else:
13761374
raise TypeError('the base ring must be ZZ, QQ, or RDF')
@@ -1431,11 +1429,9 @@ def cdd_Vrepresentation(self):
14311429
except AttributeError:
14321430
from sage.rings.integer_ring import ZZ
14331431
from sage.rings.rational_field import QQ
1434-
from sage.rings.real_double import RDF
1435-
14361432
if self.base_ring() is ZZ or self.base_ring() is QQ:
14371433
cdd_type = 'rational'
1438-
elif self.base_ring() is RDF:
1434+
elif isinstance(self.base_ring(), sage.rings.abc.RealDoubleField):
14391435
cdd_type = 'real'
14401436
else:
14411437
raise TypeError('the base ring must be ZZ, QQ, or RDF')

src/sage/geometry/polyhedron/base3.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ def slack_matrix(self):
135135
[1 0 1 0 0 1]
136136
[1 0 0 0 1 1]
137137
138-
sage: P = polytopes.dodecahedron().faces(2)[0].as_polyhedron()
139-
sage: P.slack_matrix()
138+
sage: P = polytopes.dodecahedron().faces(2)[0].as_polyhedron() # optional - sage.rings.number_field
139+
sage: P.slack_matrix() # optional - sage.rings.number_field
140140
[1/2*sqrt5 - 1/2 0 0 1 1/2*sqrt5 - 1/2 0]
141141
[ 0 0 1/2*sqrt5 - 1/2 1/2*sqrt5 - 1/2 1 0]
142142
[ 0 1/2*sqrt5 - 1/2 1 0 1/2*sqrt5 - 1/2 0]
@@ -1870,6 +1870,7 @@ def _test_combinatorial_face_as_combinatorial_polyhedron(self, tester=None, **op
18701870
D2._test_bitsets(tester, **options)
18711871
try:
18721872
import sage.graphs.graph
1873+
assert sage.graphs.graph # to muffle pyflakes
18731874
except ImportError:
18741875
pass
18751876
else:

src/sage/geometry/polyhedron/base6.py

+26-21
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
from sage.modules.vector_space_morphism import linear_transformation
3535
from sage.matrix.constructor import matrix
3636
from sage.modules.free_module_element import vector
37-
from sage.rings.qqbar import AA
3837
from sage.geometry.convex_set import AffineHullProjectionData
3938
from .base5 import Polyhedron_base5
4039

@@ -741,6 +740,7 @@ def _test_gale_transform(self, tester=None, **options):
741740

742741
try:
743742
import sage.graphs.graph
743+
assert sage.graphs.graph # to muffle pyflakes
744744
except ImportError:
745745
pass
746746
else:
@@ -1008,6 +1008,7 @@ def _affine_hull_projection(self, *,
10081008
except TypeError:
10091009
if not extend:
10101010
raise ValueError('the base ring needs to be extended; try with "extend=True"')
1011+
from sage.rings.qqbar import AA
10111012
M = matrix(AA, M)
10121013
A = M.gram_schmidt(orthonormal=orthonormal)[0]
10131014
if minimal:
@@ -1217,9 +1218,9 @@ def affine_hull_projection(self,
12171218
A vertex at (2, 0, 0),
12181219
A vertex at (1, 3/2, 0),
12191220
A vertex at (1, 1/2, 4/3))
1220-
sage: A = S.affine_hull_projection(orthonormal=True, extend=True); A
1221+
sage: A = S.affine_hull_projection(orthonormal=True, extend=True); A # optional - sage.rings.number_field
12211222
A 3-dimensional polyhedron in AA^3 defined as the convex hull of 4 vertices
1222-
sage: A.vertices()
1223+
sage: A.vertices() # optional - sage.rings.number_field
12231224
(A vertex at (0.7071067811865475?, 0.4082482904638630?, 1.154700538379252?),
12241225
A vertex at (0.7071067811865475?, 1.224744871391589?, 0.?e-18),
12251226
A vertex at (1.414213562373095?, 0.?e-18, 0.?e-18),
@@ -1228,11 +1229,11 @@ def affine_hull_projection(self,
12281229
With the parameter ``minimal`` one can get a minimal base ring::
12291230
12301231
sage: s = polytopes.simplex(3)
1231-
sage: s_AA = s.affine_hull_projection(orthonormal=True, extend=True)
1232-
sage: s_AA.base_ring()
1232+
sage: s_AA = s.affine_hull_projection(orthonormal=True, extend=True) # optional - sage.rings.number_field
1233+
sage: s_AA.base_ring() # optional - sage.rings.number_field
12331234
Algebraic Real Field
1234-
sage: s_full = s.affine_hull_projection(orthonormal=True, extend=True, minimal=True)
1235-
sage: s_full.base_ring()
1235+
sage: s_full = s.affine_hull_projection(orthonormal=True, extend=True, minimal=True) # optional - sage.rings.number_field
1236+
sage: s_full.base_ring() # optional - sage.rings.number_field
12361237
Number Field in a with defining polynomial y^4 - 4*y^2 + 1 with a = 0.5176380902050415?
12371238
12381239
More examples with the ``orthonormal`` parameter::
@@ -1486,21 +1487,25 @@ def _test_affine_hull_projection(self, tester=None, verbose=False, **options):
14861487
# Avoid very long doctests.
14871488
return
14881489

1489-
data_sets = [None]*4
1490-
data_sets[0] = self.affine_hull_projection(return_all_data=True)
1490+
try:
1491+
from sage.rings.qqbar import AA
1492+
except ImportError:
1493+
AA = None
1494+
1495+
data_sets = []
1496+
data_sets.append(self.affine_hull_projection(return_all_data=True))
14911497
if self.is_compact():
1492-
data_sets[1] = self.affine_hull_projection(return_all_data=True,
1493-
orthogonal=True,
1494-
extend=True)
1495-
data_sets[2] = self.affine_hull_projection(return_all_data=True,
1496-
orthonormal=True,
1497-
extend=True)
1498-
data_sets[3] = self.affine_hull_projection(return_all_data=True,
1499-
orthonormal=True,
1500-
extend=True,
1501-
minimal=True)
1502-
else:
1503-
data_sets = data_sets[:1]
1498+
data_sets.append(self.affine_hull_projection(return_all_data=True,
1499+
orthogonal=True,
1500+
extend=True))
1501+
if AA is not None:
1502+
data_sets.append(self.affine_hull_projection(return_all_data=True,
1503+
orthonormal=True,
1504+
extend=True))
1505+
data_sets.append(self.affine_hull_projection(return_all_data=True,
1506+
orthonormal=True,
1507+
extend=True,
1508+
minimal=True))
15041509

15051510
for i, data in enumerate(data_sets):
15061511
if verbose:

src/sage/geometry/polyhedron/base7.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
from sage.modules.free_module_element import vector
3636
from sage.rings.integer_ring import ZZ
3737
from sage.rings.rational_field import QQ
38-
from sage.rings.qqbar import AA
3938
from .base6 import Polyhedron_base6
4039

4140
class Polyhedron_base7(Polyhedron_base6):
@@ -712,6 +711,7 @@ def volume(self, measure='ambient', engine='auto', **kwds):
712711
if Adet.is_square():
713712
sqrt_Adet = Adet.sqrt()
714713
else:
714+
from sage.rings.qqbar import AA
715715
sqrt_Adet = AA(Adet).sqrt()
716716
scaled_volume = AA(scaled_volume)
717717
return scaled_volume / sqrt_Adet
@@ -908,6 +908,7 @@ def integrate(self, function, measure='ambient', **kwds):
908908
A = affine_hull_data.projection_linear_map.matrix()
909909
Adet = (A.transpose() * A).det()
910910
try:
911+
from sage.rings.qqbar import AA
911912
Adet = AA.coerce(Adet)
912913
except TypeError:
913914
pass

src/sage/geometry/polyhedron/base_ZZ.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from sage.misc.cachefunc import cached_method
1818
from sage.modules.free_module_element import vector
1919
from .base_QQ import Polyhedron_QQ
20-
from sage.arith.all import gcd
20+
from sage.arith.misc import gcd
2121

2222

2323
#########################################################################

src/sage/geometry/polyhedron/parent.py

+51-30
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def Polyhedra(ambient_space_or_base_ring=None, ambient_dim=None, backend=None, *
6565
EXAMPLES::
6666
6767
sage: from sage.geometry.polyhedron.parent import Polyhedra
68-
sage: Polyhedra(AA, 3)
68+
sage: Polyhedra(AA, 3) # optional - sage.rings.number_field
6969
Polyhedra in AA^3
7070
sage: Polyhedra(ZZ, 3)
7171
Polyhedra in ZZ^3
@@ -105,11 +105,11 @@ def Polyhedra(ambient_space_or_base_ring=None, ambient_dim=None, backend=None, *
105105
Traceback (most recent call last):
106106
...
107107
ValueError: no default backend for computations with Real Field with 53 bits of precision
108-
sage: Polyhedra(QQ[I], 2)
108+
sage: Polyhedra(QQ[I], 2) # optional - sage.rings.number_field
109109
Traceback (most recent call last):
110110
...
111111
ValueError: invalid base ring: Number Field in I with defining polynomial x^2 + 1 with I = 1*I cannot be coerced to a real field
112-
sage: Polyhedra(AA, 3, backend='polymake') # optional - jupymake
112+
sage: Polyhedra(AA, 3, backend='polymake') # optional - jupymake # optional - sage.rings.number_field
113113
Traceback (most recent call last):
114114
...
115115
ValueError: the 'polymake' backend for polyhedron cannot be used with Algebraic Real Field
@@ -174,8 +174,9 @@ def Polyhedra(ambient_space_or_base_ring=None, ambient_dim=None, backend=None, *
174174
elif backend == 'polymake':
175175
base_field = base_ring.fraction_field()
176176
try:
177-
from sage.interfaces.polymake import polymake
177+
from sage.interfaces.polymake import polymake, PolymakeElement
178178
polymake_base_field = polymake(base_field)
179+
assert isinstance(polymake_base_field, PolymakeElement) # to muffle pyflakes
179180
except TypeError:
180181
raise ValueError(f"the 'polymake' backend for polyhedron cannot be used with {base_field}")
181182
return Polyhedra_polymake(base_field, ambient_dim, backend)
@@ -264,13 +265,13 @@ def list(self):
264265
sage: P.cardinality()
265266
+Infinity
266267
267-
sage: P = Polyhedra(AA, 0)
268-
sage: P.category()
268+
sage: P = Polyhedra(AA, 0) # optional - sage.rings.number_field
269+
sage: P.category() # optional - sage.rings.number_field
269270
Category of finite enumerated polyhedral sets over Algebraic Real Field
270-
sage: P.list()
271+
sage: P.list() # optional - sage.rings.number_field
271272
[The empty polyhedron in AA^0,
272273
A 0-dimensional polyhedron in AA^0 defined as the convex hull of 1 vertex]
273-
sage: P.cardinality()
274+
sage: P.cardinality() # optional - sage.rings.number_field
274275
2
275276
"""
276277
if self.ambient_dim():
@@ -495,6 +496,35 @@ def Hrepresentation_space(self):
495496
from sage.modules.free_module import FreeModule
496497
return FreeModule(self.base_ring(), self.ambient_dim()+1)
497498

499+
def _repr_base_ring(self):
500+
"""
501+
Return an abbreviated string representation of the base ring.
502+
503+
EXAMPLES::
504+
505+
sage: from sage.geometry.polyhedron.parent import Polyhedra
506+
sage: Polyhedra(QQ, 3)._repr_base_ring()
507+
'QQ'
508+
sage: K.<sqrt3> = NumberField(x^2 - 3, embedding=AA(3).sqrt()) # optional - sage.rings.number_field
509+
sage: Polyhedra(K, 4)._repr_base_ring() # optional - sage.rings.number_field
510+
'(Number Field in sqrt3 with defining polynomial x^2 - 3 with sqrt3 = 1.732050807568878?)'
511+
"""
512+
513+
if self.base_ring() is ZZ:
514+
return 'ZZ'
515+
if self.base_ring() is QQ:
516+
return 'QQ'
517+
if self.base_ring() is RDF:
518+
return 'RDF'
519+
try:
520+
from sage.rings.qqbar import AA
521+
except ImportError:
522+
pass
523+
else:
524+
if self.base_ring() is AA:
525+
return 'AA'
526+
return '({0})'.format(self.base_ring())
527+
498528
def _repr_ambient_module(self):
499529
"""
500530
Return an abbreviated string representation of the ambient
@@ -509,21 +539,11 @@ def _repr_ambient_module(self):
509539
sage: from sage.geometry.polyhedron.parent import Polyhedra
510540
sage: Polyhedra(QQ, 3)._repr_ambient_module()
511541
'QQ^3'
512-
sage: K.<sqrt3> = NumberField(x^2 - 3, embedding=AA(3).sqrt())
513-
sage: Polyhedra(K, 4)._repr_ambient_module()
542+
sage: K.<sqrt3> = NumberField(x^2 - 3, embedding=AA(3).sqrt()) # optional - sage.rings.number_field
543+
sage: Polyhedra(K, 4)._repr_ambient_module() # optional - sage.rings.number_field
514544
'(Number Field in sqrt3 with defining polynomial x^2 - 3 with sqrt3 = 1.732050807568878?)^4'
515545
"""
516-
from sage.rings.qqbar import AA
517-
if self.base_ring() is ZZ:
518-
s = 'ZZ'
519-
elif self.base_ring() is QQ:
520-
s = 'QQ'
521-
elif self.base_ring() is RDF:
522-
s = 'RDF'
523-
elif self.base_ring() is AA:
524-
s = 'AA'
525-
else:
526-
s = '({0})'.format(self.base_ring())
546+
s = self._repr_base_ring()
527547
s += '^' + repr(self.ambient_dim())
528548
return s
529549

@@ -602,11 +622,11 @@ def _element_constructor_(self, *args, **kwds):
602622
603623
When the parent of the object is not ``self``, the default is not to copy::
604624
605-
sage: Q = P.base_extend(AA)
606-
sage: q = Q._element_constructor_(p)
607-
sage: q is p
625+
sage: Q = P.base_extend(AA) # optional - sage.rings.number_field
626+
sage: q = Q._element_constructor_(p) # optional - sage.rings.number_field
627+
sage: q is p # optional - sage.rings.number_field
608628
False
609-
sage: q = Q._element_constructor_(p, copy=False)
629+
sage: q = Q._element_constructor_(p, copy=False) # optional - sage.rings.number_field
610630
Traceback (most recent call last):
611631
...
612632
ValueError: you need to make a copy when changing the parent
@@ -693,9 +713,10 @@ def _element_constructor_polyhedron(self, polyhedron, **kwds):
693713
sage: P(p)
694714
A 3-dimensional polyhedron in QQ^3 defined as the convex hull of 4 vertices
695715
696-
sage: P = Polyhedra(AA, 3, backend='field')
697-
sage: p = Polyhedron(vertices=[(0, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1)])
698-
sage: P(p)
716+
sage: P = Polyhedra(AA, 3, backend='field') # optional - sage.rings.number_field
717+
sage: vertices = [(0, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1)]
718+
sage: p = Polyhedron(vertices=vertices) # optional - sage.rings.number_field
719+
sage: P(p) # optional - sage.rings.number_field
699720
A 3-dimensional polyhedron in AA^3 defined as the convex hull of 4 vertices
700721
"""
701722
Vrep = None
@@ -1234,9 +1255,9 @@ def does_backend_handle_base_ring(base_ring, backend):
12341255
sage: from sage.geometry.polyhedron.parent import does_backend_handle_base_ring
12351256
sage: does_backend_handle_base_ring(QQ, 'ppl')
12361257
True
1237-
sage: does_backend_handle_base_ring(QQ[sqrt(5)], 'ppl')
1258+
sage: does_backend_handle_base_ring(QQ[sqrt(5)], 'ppl') # optional - sage.rings.number_field
12381259
False
1239-
sage: does_backend_handle_base_ring(QQ[sqrt(5)], 'field')
1260+
sage: does_backend_handle_base_ring(QQ[sqrt(5)], 'field') # optional - sage.rings.number_field
12401261
True
12411262
"""
12421263
try:

0 commit comments

Comments
 (0)