Skip to content

Commit

Permalink
Trac #28770: Polyhedra coercion of base rings fails for number fields
Browse files Browse the repository at this point in the history
Currently coercion of polyhedra with number fields does not work. The
following results in a type error:

{{{
sage: z = QQ['z'].0
sage: K = NumberField(z^2 - 2,'s')
sage: K.gen()*polytopes.simplex(backend='field')
}}}

But the backend can surely handle it, as the following does work:

{{{
sage: z = QQ['z'].0
sage: K = NumberField(z^2 - 2,'s')
sage: K.gen()*polytopes.simplex(backend='field', base_ring=K)
A 3-dimensional polyhedron in (Number Field in s with defining
polynomial z^2 - 2)^4 defined as the convex hull of 4 vertices
}}}

The underlying error:

{{{
sage: z = QQ['z'].0
sage: K = NumberField(z^2 - 2,'s')
sage: parent = polytopes.simplex().parent()
sage: parent._coerce_base_ring(K)
Rational Field
}}}

But it should be `K`.

The problem is that `_coerce_base_ring` of `Polyhedra_base` just takes
the base ring of `K`, which are the rationals.

We fix this, by not taking the base ring, if the object is already a
ring.

URL: https://trac.sagemath.org/28770
Reported by: gh-kliem
Ticket author(s): Jonathan Kliem
Reviewer(s): Léo Brunswic
  • Loading branch information
Release Manager committed Nov 29, 2019
2 parents 910e84c + a0068cc commit 671567e
Showing 1 changed file with 37 additions and 15 deletions.
52 changes: 37 additions & 15 deletions src/sage/geometry/polyhedron/parent.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,24 +672,46 @@ def _coerce_base_ring(self, other):
Rational Field
sage: triangle_QQ._coerce_base_ring(0.5)
Real Double Field
TESTS:
Test that :trac:`28770` is fixed::
sage: z = QQ['z'].0
sage: K = NumberField(z^2 - 2,'s')
sage: triangle_QQ._coerce_base_ring(K)
Number Field in s with defining polynomial z^2 - 2
sage: triangle_QQ._coerce_base_ring(K.gen())
Number Field in s with defining polynomial z^2 - 2
sage: z = QQ['z'].0
sage: K = NumberField(z^2 - 2,'s')
sage: K.gen()*polytopes.simplex(backend='field')
A 3-dimensional polyhedron in (Number Field in s with defining polynomial z^2 - 2)^4 defined as the convex hull of 4 vertices
"""
try:
other_ring = other.base_ring()
except AttributeError:
from sage.structure.element import Element
if isinstance(other, Element):
other = other.parent()
if hasattr(other, "is_ring") and other.is_ring():
other_ring = other
else:
try:
# other is a constant?
other_ring = other.parent()
other_ring = other.base_ring()
except AttributeError:
other_ring = None
for ring in (ZZ, QQ, RDF):
try:
ring.coerce(other)
other_ring = ring
break
except TypeError:
pass
if other_ring is None:
raise TypeError('Could not coerce '+str(other)+' into ZZ, QQ, or RDF.')
try:
# other is a constant?
other_ring = other.parent()
except AttributeError:
other_ring = None
for ring in (ZZ, QQ, RDF):
try:
ring.coerce(other)
other_ring = ring
break
except TypeError:
pass
if other_ring is None:
raise TypeError('Could not coerce '+str(other)+' into ZZ, QQ, or RDF.')

if not other_ring.is_exact():
other_ring = RDF # the only supported floating-point numbers for now
Expand Down

0 comments on commit 671567e

Please sign in to comment.