Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
add _test_dilation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Kliem committed Jun 19, 2020
1 parent 3a6559b commit 3cb69aa
Showing 1 changed file with 59 additions and 21 deletions.
80 changes: 59 additions & 21 deletions src/sage/geometry/polyhedron/base.py
Expand Up @@ -4733,27 +4733,6 @@ def dilation(self, scalar):
()
sage: (0*p).lines()
()
Dilation respects backend::
sage: P = polytopes.simplex(backend='field')
sage: P.dilation(3).backend()
'field'
Dilation with both Vrep and Hrep works correctly::
sage: def test_dilation(P):
....: Q = P.change_ring(P.base_ring(), backend='field')
....: assert 2*Q == 2*P
....: assert 1/2*Q == 1/2*P
....: assert (-3)*Q == (-3)*P
....: assert (-1/2)*Q == (-1/2)*P
sage: test_dilation(polytopes.cube())
sage: test_dilation(polytopes.cross_polytope(3))
sage: test_dilation(polytopes.simplex(4))
sage: test_dilation(polytopes.permutahedron(3))
sage: test_dilation(polytopes.permutahedron(3)*Polyhedron(rays=[[0,0,1],[0,1,1],[1,2,3]]))
sage: test_dilation(polytopes.permutahedron(3)*Polyhedron(rays=[[0,0,1],[0,1,1]], lines=[[1,0,0]]))
"""
parent = self.parent().base_extend(scalar)

Expand Down Expand Up @@ -4781,6 +4760,65 @@ def dilation(self, scalar):
[new_inequalities, new_equations],
Vrep_minimal=True, Hrep_minimal=True, pref_rep=pref_rep)

def _test_dilation(self, tester=None, **options):
"""
Run tests on the method :meth:`.dilation`
TESTS::
sage: polytopes.cross_polytope(3)._test_dilation()
"""
if tester is None:
tester = self._tester(**options)

# Testing that the backend is preserved.
tester.assertEqual(self.dilation(2*self.base_ring().gen()).backend(), self.backend())
tester.assertEqual(self.dilation(ZZ(3)).backend(), self.backend())

if self.n_vertices() > 40:
# Avoid long time computations.
return

# Testing that the double description is set up correctly.
if not self.backend() is 'field' and self.base_ring().is_exact():
p = self.base_extend(self.base_ring(), backend='field')
tester.assertEqual(ZZ(2)*p, ZZ(2)*self)
tester.assertEqual(ZZ(1)/2*p, ZZ(1)/2*self)
tester.assertEqual(ZZ(-3)*p, ZZ(-3)*self)
tester.assertEqual((-ZZ(1)/2)*p, (-ZZ(1)/2)*self)
else:
tester.assertIsInstance(ZZ(1)/3*self, Polyhedron_base)

if self.n_vertices() > 20:
# Avoid long time computations.
return

# Some sanity check on the volume (only run for relativly small instances).
if self.dim() > -1 and self.is_compact() and self.base_ring().is_exact():
tester.assertEqual(self.dilation(3).volume(measure='induced'), self.volume(measure='induced')*3**self.dim())

# Testing coercion with algebraic numbers.
from sage.rings.number_field.number_field import QuadraticField
K1 = QuadraticField(2, embedding=AA(2).sqrt())
sqrt2 = K1.gen()
K2 = QuadraticField(3, embedding=AA(3).sqrt())
sqrt3 = K2.gen()

if self.base_ring() in (QQ,ZZ,AA,RDF):
tester.assertIsInstance(sqrt2*self, Polyhedron_base)
tester.assertIsInstance(sqrt3*self, Polyhedron_base)
elif hasattr(self.base_ring(), "composite_fields"):
for scalar, K in ((sqrt2, K1), (sqrt3, K2)):
new_ring = None
try:
new_ring = self.base_ring().composite_fields()[0]
except:
# This isn't about testing composite fields.
pass
if new_ring:
p = self.change_ring(new_ring)
tester.assertIsInstance(scalar*p, Polyhedron_base)

def linear_transformation(self, linear_transf):
"""
Return the linear transformation of ``self``.
Expand Down

0 comments on commit 3cb69aa

Please sign in to comment.