Skip to content

Commit

Permalink
Trac #34195: sage.geometry.polyhedron: More # optional - sage.rings.n…
Browse files Browse the repository at this point in the history
…umber_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
  • Loading branch information
Release Manager committed Sep 25, 2022
2 parents b8a75b6 + da3d534 commit d871894
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 72 deletions.
12 changes: 8 additions & 4 deletions src/sage/geometry/convex_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,10 +873,14 @@ def _test_contains(self, tester=None, **options):
tester.assertEqual(contains_space_point, self.contains(ambient_point))
tester.assertEqual(contains_space_point, self.contains(space_coords))
if space.base_ring().is_exact():
from sage.rings.qqbar import AA
ext_space = self.ambient_vector_space(AA)
ext_space_point = ext_space(space_point)
tester.assertEqual(contains_space_point, self.contains(ext_space_point))
try:
from sage.rings.qqbar import AA
except ImportError:
pass
else:
ext_space = self.ambient_vector_space(AA)
ext_space_point = ext_space(space_point)
tester.assertEqual(contains_space_point, self.contains(ext_space_point))
try:
from sage.symbolic.ring import SR
symbolic_space = self.ambient_vector_space(SR)
Expand Down
14 changes: 9 additions & 5 deletions src/sage/geometry/polyhedron/backend_normaliz.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
lazy_import('PyNormaliz', ['NmzResult', 'NmzCompute', 'NmzCone', 'NmzConeCopy'],
feature=sage.features.normaliz.PyNormaliz())

from sage.rings.all import ZZ, QQ, QQbar
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
from sage.rings.integer_ring import ZZ
from sage.rings.rational_field import QQ
from sage.arith.functions import LCM_list
from sage.misc.functional import denominator
from sage.matrix.constructor import vector
Expand Down Expand Up @@ -1082,10 +1082,11 @@ def _number_field_triple(normaliz_field):
sage: Pn._number_field_triple(QuadraticField(5)) # optional - sage.rings.number_field
['a^2 - 5', 'a', '[2.236067977499789 +/- 8.06e-16]']
"""
from sage.rings.real_arb import RealBallField
R = normaliz_field
if R is QQ:
return None
from sage.rings.real_arb import RealBallField
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
emb = RealBallField(53)(R.gen(0))
gen = 'a'
R_a = PolynomialRing(QQ, gen)
Expand Down Expand Up @@ -2336,8 +2337,9 @@ class functions.
((t^4 + 3*t^3 + 8*t^2 + 3*t + 1)/(t + 1), (3*t^3 + 2*t^2 + 3*t)/(t + 1))
"""
from sage.groups.conjugacy_classes import ConjugacyClassGAP
from sage.rings.all import CyclotomicField
from sage.matrix.all import MatrixSpace
from sage.rings.number_field.number_field import CyclotomicField
from sage.rings.qqbar import QQbar
from sage.matrix.matrix_space import MatrixSpace
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
from sage.matrix.special import identity_matrix
# Setting the group
Expand Down Expand Up @@ -2475,6 +2477,8 @@ def _Hstar_as_rat_fct(self, initial_Hstar):
[ 1 1 -1 -1 1]
[ 2 0 0 0 -2]
"""
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
from sage.rings.qqbar import QQbar
chi_vars = ','.join('chi_{}'.format(i) for i in range(len(initial_Hstar)))
Chi_ring = PolynomialRing(QQbar, chi_vars)
virtual_ring = PolynomialRing(Chi_ring, initial_Hstar.base_ring().gens())
Expand Down
8 changes: 2 additions & 6 deletions src/sage/geometry/polyhedron/base0.py
Original file line number Diff line number Diff line change
Expand Up @@ -1366,11 +1366,9 @@ def cdd_Hrepresentation(self):
except AttributeError:
from sage.rings.integer_ring import ZZ
from sage.rings.rational_field import QQ
from sage.rings.real_double import RDF

if self.base_ring() is ZZ or self.base_ring() is QQ:
cdd_type = 'rational'
elif self.base_ring() is RDF:
elif isinstance(self.base_ring(), sage.rings.abc.RealDoubleField):
cdd_type = 'real'
else:
raise TypeError('the base ring must be ZZ, QQ, or RDF')
Expand Down Expand Up @@ -1431,11 +1429,9 @@ def cdd_Vrepresentation(self):
except AttributeError:
from sage.rings.integer_ring import ZZ
from sage.rings.rational_field import QQ
from sage.rings.real_double import RDF

if self.base_ring() is ZZ or self.base_ring() is QQ:
cdd_type = 'rational'
elif self.base_ring() is RDF:
elif isinstance(self.base_ring(), sage.rings.abc.RealDoubleField):
cdd_type = 'real'
else:
raise TypeError('the base ring must be ZZ, QQ, or RDF')
Expand Down
5 changes: 3 additions & 2 deletions src/sage/geometry/polyhedron/base3.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ def slack_matrix(self):
[1 0 1 0 0 1]
[1 0 0 0 1 1]
sage: P = polytopes.dodecahedron().faces(2)[0].as_polyhedron()
sage: P.slack_matrix()
sage: P = polytopes.dodecahedron().faces(2)[0].as_polyhedron() # optional - sage.rings.number_field
sage: P.slack_matrix() # optional - sage.rings.number_field
[1/2*sqrt5 - 1/2 0 0 1 1/2*sqrt5 - 1/2 0]
[ 0 0 1/2*sqrt5 - 1/2 1/2*sqrt5 - 1/2 1 0]
[ 0 1/2*sqrt5 - 1/2 1 0 1/2*sqrt5 - 1/2 0]
Expand Down Expand Up @@ -1870,6 +1870,7 @@ def _test_combinatorial_face_as_combinatorial_polyhedron(self, tester=None, **op
D2._test_bitsets(tester, **options)
try:
import sage.graphs.graph
assert sage.graphs.graph # to muffle pyflakes
except ImportError:
pass
else:
Expand Down
47 changes: 26 additions & 21 deletions src/sage/geometry/polyhedron/base6.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
from sage.modules.vector_space_morphism import linear_transformation
from sage.matrix.constructor import matrix
from sage.modules.free_module_element import vector
from sage.rings.qqbar import AA
from sage.geometry.convex_set import AffineHullProjectionData
from .base5 import Polyhedron_base5

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

try:
import sage.graphs.graph
assert sage.graphs.graph # to muffle pyflakes
except ImportError:
pass
else:
Expand Down Expand Up @@ -1008,6 +1008,7 @@ def _affine_hull_projection(self, *,
except TypeError:
if not extend:
raise ValueError('the base ring needs to be extended; try with "extend=True"')
from sage.rings.qqbar import AA
M = matrix(AA, M)
A = M.gram_schmidt(orthonormal=orthonormal)[0]
if minimal:
Expand Down Expand Up @@ -1217,9 +1218,9 @@ def affine_hull_projection(self,
A vertex at (2, 0, 0),
A vertex at (1, 3/2, 0),
A vertex at (1, 1/2, 4/3))
sage: A = S.affine_hull_projection(orthonormal=True, extend=True); A
sage: A = S.affine_hull_projection(orthonormal=True, extend=True); A # optional - sage.rings.number_field
A 3-dimensional polyhedron in AA^3 defined as the convex hull of 4 vertices
sage: A.vertices()
sage: A.vertices() # optional - sage.rings.number_field
(A vertex at (0.7071067811865475?, 0.4082482904638630?, 1.154700538379252?),
A vertex at (0.7071067811865475?, 1.224744871391589?, 0.?e-18),
A vertex at (1.414213562373095?, 0.?e-18, 0.?e-18),
Expand All @@ -1228,11 +1229,11 @@ def affine_hull_projection(self,
With the parameter ``minimal`` one can get a minimal base ring::
sage: s = polytopes.simplex(3)
sage: s_AA = s.affine_hull_projection(orthonormal=True, extend=True)
sage: s_AA.base_ring()
sage: s_AA = s.affine_hull_projection(orthonormal=True, extend=True) # optional - sage.rings.number_field
sage: s_AA.base_ring() # optional - sage.rings.number_field
Algebraic Real Field
sage: s_full = s.affine_hull_projection(orthonormal=True, extend=True, minimal=True)
sage: s_full.base_ring()
sage: s_full = s.affine_hull_projection(orthonormal=True, extend=True, minimal=True) # optional - sage.rings.number_field
sage: s_full.base_ring() # optional - sage.rings.number_field
Number Field in a with defining polynomial y^4 - 4*y^2 + 1 with a = 0.5176380902050415?
More examples with the ``orthonormal`` parameter::
Expand Down Expand Up @@ -1486,21 +1487,25 @@ def _test_affine_hull_projection(self, tester=None, verbose=False, **options):
# Avoid very long doctests.
return

data_sets = [None]*4
data_sets[0] = self.affine_hull_projection(return_all_data=True)
try:
from sage.rings.qqbar import AA
except ImportError:
AA = None

data_sets = []
data_sets.append(self.affine_hull_projection(return_all_data=True))
if self.is_compact():
data_sets[1] = self.affine_hull_projection(return_all_data=True,
orthogonal=True,
extend=True)
data_sets[2] = self.affine_hull_projection(return_all_data=True,
orthonormal=True,
extend=True)
data_sets[3] = self.affine_hull_projection(return_all_data=True,
orthonormal=True,
extend=True,
minimal=True)
else:
data_sets = data_sets[:1]
data_sets.append(self.affine_hull_projection(return_all_data=True,
orthogonal=True,
extend=True))
if AA is not None:
data_sets.append(self.affine_hull_projection(return_all_data=True,
orthonormal=True,
extend=True))
data_sets.append(self.affine_hull_projection(return_all_data=True,
orthonormal=True,
extend=True,
minimal=True))

for i, data in enumerate(data_sets):
if verbose:
Expand Down
3 changes: 2 additions & 1 deletion src/sage/geometry/polyhedron/base7.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
from sage.modules.free_module_element import vector
from sage.rings.integer_ring import ZZ
from sage.rings.rational_field import QQ
from sage.rings.qqbar import AA
from .base6 import Polyhedron_base6

class Polyhedron_base7(Polyhedron_base6):
Expand Down Expand Up @@ -712,6 +711,7 @@ def volume(self, measure='ambient', engine='auto', **kwds):
if Adet.is_square():
sqrt_Adet = Adet.sqrt()
else:
from sage.rings.qqbar import AA
sqrt_Adet = AA(Adet).sqrt()
scaled_volume = AA(scaled_volume)
return scaled_volume / sqrt_Adet
Expand Down Expand Up @@ -908,6 +908,7 @@ def integrate(self, function, measure='ambient', **kwds):
A = affine_hull_data.projection_linear_map.matrix()
Adet = (A.transpose() * A).det()
try:
from sage.rings.qqbar import AA
Adet = AA.coerce(Adet)
except TypeError:
pass
Expand Down
2 changes: 1 addition & 1 deletion src/sage/geometry/polyhedron/base_ZZ.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from sage.misc.cachefunc import cached_method
from sage.modules.free_module_element import vector
from .base_QQ import Polyhedron_QQ
from sage.arith.all import gcd
from sage.arith.misc import gcd


#########################################################################
Expand Down
81 changes: 51 additions & 30 deletions src/sage/geometry/polyhedron/parent.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def Polyhedra(ambient_space_or_base_ring=None, ambient_dim=None, backend=None, *
EXAMPLES::
sage: from sage.geometry.polyhedron.parent import Polyhedra
sage: Polyhedra(AA, 3)
sage: Polyhedra(AA, 3) # optional - sage.rings.number_field
Polyhedra in AA^3
sage: Polyhedra(ZZ, 3)
Polyhedra in ZZ^3
Expand Down Expand Up @@ -105,11 +105,11 @@ def Polyhedra(ambient_space_or_base_ring=None, ambient_dim=None, backend=None, *
Traceback (most recent call last):
...
ValueError: no default backend for computations with Real Field with 53 bits of precision
sage: Polyhedra(QQ[I], 2)
sage: Polyhedra(QQ[I], 2) # optional - sage.rings.number_field
Traceback (most recent call last):
...
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
sage: Polyhedra(AA, 3, backend='polymake') # optional - jupymake
sage: Polyhedra(AA, 3, backend='polymake') # optional - jupymake # optional - sage.rings.number_field
Traceback (most recent call last):
...
ValueError: the 'polymake' backend for polyhedron cannot be used with Algebraic Real Field
Expand Down Expand Up @@ -174,8 +174,9 @@ def Polyhedra(ambient_space_or_base_ring=None, ambient_dim=None, backend=None, *
elif backend == 'polymake':
base_field = base_ring.fraction_field()
try:
from sage.interfaces.polymake import polymake
from sage.interfaces.polymake import polymake, PolymakeElement
polymake_base_field = polymake(base_field)
assert isinstance(polymake_base_field, PolymakeElement) # to muffle pyflakes
except TypeError:
raise ValueError(f"the 'polymake' backend for polyhedron cannot be used with {base_field}")
return Polyhedra_polymake(base_field, ambient_dim, backend)
Expand Down Expand Up @@ -264,13 +265,13 @@ def list(self):
sage: P.cardinality()
+Infinity
sage: P = Polyhedra(AA, 0)
sage: P.category()
sage: P = Polyhedra(AA, 0) # optional - sage.rings.number_field
sage: P.category() # optional - sage.rings.number_field
Category of finite enumerated polyhedral sets over Algebraic Real Field
sage: P.list()
sage: P.list() # optional - sage.rings.number_field
[The empty polyhedron in AA^0,
A 0-dimensional polyhedron in AA^0 defined as the convex hull of 1 vertex]
sage: P.cardinality()
sage: P.cardinality() # optional - sage.rings.number_field
2
"""
if self.ambient_dim():
Expand Down Expand Up @@ -495,6 +496,35 @@ def Hrepresentation_space(self):
from sage.modules.free_module import FreeModule
return FreeModule(self.base_ring(), self.ambient_dim()+1)

def _repr_base_ring(self):
"""
Return an abbreviated string representation of the base ring.
EXAMPLES::
sage: from sage.geometry.polyhedron.parent import Polyhedra
sage: Polyhedra(QQ, 3)._repr_base_ring()
'QQ'
sage: K.<sqrt3> = NumberField(x^2 - 3, embedding=AA(3).sqrt()) # optional - sage.rings.number_field
sage: Polyhedra(K, 4)._repr_base_ring() # optional - sage.rings.number_field
'(Number Field in sqrt3 with defining polynomial x^2 - 3 with sqrt3 = 1.732050807568878?)'
"""

if self.base_ring() is ZZ:
return 'ZZ'
if self.base_ring() is QQ:
return 'QQ'
if self.base_ring() is RDF:
return 'RDF'
try:
from sage.rings.qqbar import AA
except ImportError:
pass
else:
if self.base_ring() is AA:
return 'AA'
return '({0})'.format(self.base_ring())

def _repr_ambient_module(self):
"""
Return an abbreviated string representation of the ambient
Expand All @@ -509,21 +539,11 @@ def _repr_ambient_module(self):
sage: from sage.geometry.polyhedron.parent import Polyhedra
sage: Polyhedra(QQ, 3)._repr_ambient_module()
'QQ^3'
sage: K.<sqrt3> = NumberField(x^2 - 3, embedding=AA(3).sqrt())
sage: Polyhedra(K, 4)._repr_ambient_module()
sage: K.<sqrt3> = NumberField(x^2 - 3, embedding=AA(3).sqrt()) # optional - sage.rings.number_field
sage: Polyhedra(K, 4)._repr_ambient_module() # optional - sage.rings.number_field
'(Number Field in sqrt3 with defining polynomial x^2 - 3 with sqrt3 = 1.732050807568878?)^4'
"""
from sage.rings.qqbar import AA
if self.base_ring() is ZZ:
s = 'ZZ'
elif self.base_ring() is QQ:
s = 'QQ'
elif self.base_ring() is RDF:
s = 'RDF'
elif self.base_ring() is AA:
s = 'AA'
else:
s = '({0})'.format(self.base_ring())
s = self._repr_base_ring()
s += '^' + repr(self.ambient_dim())
return s

Expand Down Expand Up @@ -602,11 +622,11 @@ def _element_constructor_(self, *args, **kwds):
When the parent of the object is not ``self``, the default is not to copy::
sage: Q = P.base_extend(AA)
sage: q = Q._element_constructor_(p)
sage: q is p
sage: Q = P.base_extend(AA) # optional - sage.rings.number_field
sage: q = Q._element_constructor_(p) # optional - sage.rings.number_field
sage: q is p # optional - sage.rings.number_field
False
sage: q = Q._element_constructor_(p, copy=False)
sage: q = Q._element_constructor_(p, copy=False) # optional - sage.rings.number_field
Traceback (most recent call last):
...
ValueError: you need to make a copy when changing the parent
Expand Down Expand Up @@ -693,9 +713,10 @@ def _element_constructor_polyhedron(self, polyhedron, **kwds):
sage: P(p)
A 3-dimensional polyhedron in QQ^3 defined as the convex hull of 4 vertices
sage: P = Polyhedra(AA, 3, backend='field')
sage: p = Polyhedron(vertices=[(0, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1)])
sage: P(p)
sage: P = Polyhedra(AA, 3, backend='field') # optional - sage.rings.number_field
sage: vertices = [(0, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1)]
sage: p = Polyhedron(vertices=vertices) # optional - sage.rings.number_field
sage: P(p) # optional - sage.rings.number_field
A 3-dimensional polyhedron in AA^3 defined as the convex hull of 4 vertices
"""
Vrep = None
Expand Down Expand Up @@ -1234,9 +1255,9 @@ def does_backend_handle_base_ring(base_ring, backend):
sage: from sage.geometry.polyhedron.parent import does_backend_handle_base_ring
sage: does_backend_handle_base_ring(QQ, 'ppl')
True
sage: does_backend_handle_base_ring(QQ[sqrt(5)], 'ppl')
sage: does_backend_handle_base_ring(QQ[sqrt(5)], 'ppl') # optional - sage.rings.number_field
False
sage: does_backend_handle_base_ring(QQ[sqrt(5)], 'field')
sage: does_backend_handle_base_ring(QQ[sqrt(5)], 'field') # optional - sage.rings.number_field
True
"""
try:
Expand Down
Loading

0 comments on commit d871894

Please sign in to comment.