Skip to content

Commit

Permalink
Trac #29223: Pickle cached value of volume and f-vector
Browse files Browse the repository at this point in the history
Volume, f-vector and Ehrhart polynomial of polyhedra can be hard to
compute, but take little memory when saving.

So we pickle those values, e.g.

{{{#!diff
+    @cached_method(do_pickle=True)
-    @cached_method
     def f_vector(self):
}}}

URL: https://trac.sagemath.org/29223
Reported by: gh-kliem
Ticket author(s): Jonathan Kliem
Reviewer(s): Laith Rastanawi
  • Loading branch information
Release Manager committed Feb 29, 2020
2 parents 5cb0f80 + cd13c82 commit 7a7ffbe
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
24 changes: 22 additions & 2 deletions src/sage/geometry/polyhedron/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5946,7 +5946,7 @@ def facets(self):
"""
return self.faces(self.dimension()-1)

@cached_method
@cached_method(do_pickle=True)
def f_vector(self):
r"""
Return the f-vector.
Expand Down Expand Up @@ -5996,6 +5996,15 @@ def f_vector(self):
sage: P.f_vector().is_immutable()
True
The cache of the f-vector is being pickled::
sage: P = polytopes.cube()
sage: P.f_vector()
(1, 8, 12, 6, 1)
sage: Q = loads(dumps(P))
sage: Q.f_vector.is_in_cache()
True
"""
return self.combinatorial_polyhedron().f_vector()

Expand Down Expand Up @@ -6827,7 +6836,7 @@ def _volume_normaliz(self, measure='induced'):
"""
raise TypeError("the backend should be normaliz")

@cached_method
@cached_method(do_pickle=True)
def volume(self, measure='ambient', engine='auto', **kwds):
"""
Return the volume of the polytope.
Expand Down Expand Up @@ -6999,6 +7008,17 @@ def volume(self, measure='ambient', engine='auto', **kwds):
The empty polyhedron in ZZ^0
sage: P.volume()
0
TESTS:
The cache of the volume is being pickled::
sage: P = polytopes.cube()
sage: P.volume()
8
sage: Q = loads(dumps(P))
sage: Q.volume.is_in_cache()
True
"""
from sage.features import FeatureNotPresentError, PythonModule
if measure == 'induced_rational' and engine not in ['auto', 'latte', 'normaliz']:
Expand Down
13 changes: 12 additions & 1 deletion src/sage/geometry/polyhedron/base_ZZ.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ def _ehrhart_polynomial_normaliz(self, variable='t'):
"""
raise TypeError("The polyhedron's backend should be 'normaliz'")

@cached_method
@cached_method(do_pickle=True)
def ehrhart_polynomial(self, engine=None, variable='t', verbose=False, dual=None,
irrational_primal=None, irrational_all_primal=None, maxdet=None,
no_decomposition=None, compute_vertex_cones=None, smith_form=None,
Expand Down Expand Up @@ -439,6 +439,17 @@ def ehrhart_polynomial(self, engine=None, variable='t', verbose=False, dual=None
Traceback (most recent call last):
...
ValueError: Ehrhart polynomial only defined for compact polyhedra
TESTS:
The cache of the Ehrhart polynomial is being pickled::
sage: P = polytopes.cube()
sage: P.ehrhart_polynomial() # optional - latte_int
8*t^3 + 12*t^2 + 6*t + 1
sage: Q = loads(dumps(P))
sage: Q.ehrhart_polynomial.is_in_cache() # optional - latte_int
True
"""
if self.is_empty():
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
Expand Down

0 comments on commit 7a7ffbe

Please sign in to comment.