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

Commit

Permalink
Fix the docstrings, and enhance integrate method.
Browse files Browse the repository at this point in the history
* fix formats and typos in docstrings
* a tweak in to_latte_polynomial using ETuple to handle both univariate and multivariate polynomials.
    - note that exponents() of a multivariate polyomial returns a list of ETuples, while exponents() of a univariate
      one returns a plain list of elements, hence not iterable.
* a fix in integrate method that allows to handle a Polyhedron in RDF base ring, by transforming the vertices to QQ.

to-do:

* add volume engine
  • Loading branch information
mforets committed Mar 9, 2017
1 parent 17911f7 commit 91d885e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 35 deletions.
68 changes: 39 additions & 29 deletions src/sage/geometry/polyhedron/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3973,7 +3973,7 @@ def integrate(self, polynomial, **kwds):
- ``P`` -- Polyhedron.
- ``f`` -- A multivariate polynomial or a valid LattE description string for
- ``polynomial`` -- A multivariate polynomial or a valid LattE description string for
polynomials.
- ``**kwds`` -- additional keyword arguments that are passed to the engine.
Expand All @@ -3982,49 +3982,59 @@ def integrate(self, polynomial, **kwds):
The integral of the polynomial over the polytope.
NOTES:
.. NOTE::
The polytope triangulation algorithm is used. This function depends on
LattE (latte_int) optional package.
The polytope triangulation algorithm is used. This function depends
on LattE (i.e., the ``latte_int`` optional package).
EXAMPLES::
sage: P = polytopes.cube()
sage: x, y, z = polygens(QQ, 'x, y, z')
sage: P.integrate(x^2*y^2*z^2) # optional - latte_int
8/27
sage: P = polytopes.cube()
sage: x, y, z = polygens(QQ, 'x, y, z')
sage: P.integrate(x^2*y^2*z^2) # optional - latte_int
8/27
TESTS::
Testing a three-dimensional integral:
Testing a three-dimensional integral::
sage: P = polytopes.octahedron()
sage: x, y, z = polygens(QQ, 'x, y, z')
sage: P.integrate(2*x^2*y^4*z^6+z^2) # optional - latte_int
630632/4729725
sage: P = polytopes.octahedron()
sage: x, y, z = polygens(QQ, 'x, y, z')
sage: P.integrate(2*x^2*y^4*z^6+z^2) # optional - latte_int
630632/4729725
Testing a polytope with non-rational vertices:
Testing a polytope with non-rational vertices::
sage: P = polytopes.icosahedron()
sage: P.integrate(x^2*y^2*z^2) # optional - latte_int
Traceback (most recent call last):
...
TypeError: The base ring must be ZZ, QQ, or RDF
sage: P = polytopes.icosahedron()
sage: P.integrate(x^2*y^2*z^2) # optional - latte_int
Traceback (most recent call last):
...
TypeError: The base ring must be ZZ, QQ, or RDF
Testing a non full-dimensional case:
Testing a non full-dimensional case::
sage: P = Polyhedron(vertices=[[0,0],[1,1]])
sage: x, y = polygens(QQ, 'x, y')
sage: P.integrate(x) # optional - latte_int
Traceback (most recent call last):
...
RuntimeError: LattE integrale program failed (exit code -6):
...
SetLength: can't change this vector's length
sage: P = Polyhedron(vertices=[[0,0],[1,1]])
sage: x = polygen(QQ, 'x')
sage: P.integrate(x) # optional - latte_int
Traceback (most recent call last):
...
RuntimeError: LattE integrale program failed (exit code -6):
...
SetLength: can't change this vector's length
Testing a polytope with floating point coordinates::
sage: P = Polyhedron(vertices = [[0, 0], [1, 0], [1.1,1.1], [0,1]])
sage: P.integrate('[[1,[2,2]]]') # optional - latte_int
384659/2250000
"""
if is_package_installed('latte_int'):
from sage.interfaces.latte import integrate
return integrate(self.cdd_Hrepresentation(), polynomial, cdd=True)
if self.base_ring() == RDF:
self_QQ = Polyhedron(vertices=[[QQ(self) for vi in v] for v in self.vertex_generator()])
return integrate(self_QQ.cdd_Hrepresentation(), polynomial, cdd=True)
else:
return integrate(self.cdd_Hrepresentation(), polynomial, cdd=True)

else:
raise NotImplementedError('You must install the optional latte_int package for this function to work.')
Expand Down
25 changes: 19 additions & 6 deletions src/sage/interfaces/latte.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,12 @@ def integrate(arg, polynomial=None, algorithm='triangulate', raw_output=False, v
sage: integrate(P.cdd_Vrepresentation(), cdd=True) # optional - latte_int
64
Polynomials given as a string in LattE description are also accepted:
Polynomials given as a string in LattE description are also accepted::
sage: integrate(P.cdd_Hrepresentation(), ``[1,[2,2,2]]``, cdd=True) # optional - latte_int
sage: integrate(P.cdd_Hrepresentation(), '[[1,[2,2,2]]]', cdd=True) # optional - latte_int
4096/27
TESTS:
TESTS::
Testing raw output::
Expand Down Expand Up @@ -385,19 +385,32 @@ def to_latte_polynomial(polynomial):
A string that describes the monomials list and exponent vectors.
TESTS:
TESTS::
Testing a polynomial in three variables::
sage: from sage.interfaces.latte import to_latte_polynomial
sage: x, y, z = polygen(QQ, 'x, y, z')
sage: x, y, z = polygens(QQ, 'x, y, z')
sage: f = 3*x^2*y^4*z^6 + 7*y^3*z^5
sage: to_latte_polynomial(f)
'[[3, [2, 4, 6]], [7, [0, 3, 5]]]'
Testing a univariate polynomial::
sage: x = polygen(QQ, 'x')
sage: to_latte_polynomial((x-1)^2)
'[[1, [0]], [-2, [1]], [1, [2]]]'
"""
from sage.rings.polynomial.polydict import ETuple

coefficients_list = polynomial.coefficients()
exponents_list = [list(exponent_vector_i) for exponent_vector_i in polynomial.exponents()]

# transform list of exponents into a list of lists.
# this branch handles the multivariate/univariate case
if isinstance(polynomial.exponents()[0], ETuple):
exponents_list = [list(exponent_vector_i) for exponent_vector_i in polynomial.exponents()]
else:
exponents_list = [[exponent_vector_i] for exponent_vector_i in polynomial.exponents()]

# assuming that the order in coefficients() and exponents() methods match
monomials_list = zip(coefficients_list, exponents_list)
Expand Down

0 comments on commit 91d885e

Please sign in to comment.