def hyperplane_parameters(poly, vertices=None): """A helper function to return the hyperplane parameters of which the facets of the polytope are a part of. Parameters ========== poly : The input 2/3-Polytope. vertices : Vertex indices of 3-Polytope. Examples ======== >>> from sympy.geometry.point import Point >>> from sympy.geometry.polygon import Polygon >>> from sympy.integrals.intpoly import hyperplane_parameters >>> hyperplane_parameters(Polygon(Point(0, 3), Point(5, 3), Point(1, 1))) [((0, 1), 3), ((1, -2), -1), ((-2, -1), -3)] >>> cube = [[(0, 0, 0), (0, 0, 5), (0, 5, 0), (0, 5, 5), (5, 0, 0),\ (5, 0, 5), (5, 5, 0), (5, 5, 5)],\ [2, 6, 7, 3], [3, 7, 5, 1], [7, 6, 4, 5], [1, 5, 4, 0],\ [3, 1, 0, 2], [0, 4, 6, 2]] >>> hyperplane_parameters(cube[1:], cube[0]) [([0, -1, 0], -5), ([0, 0, -1], -5), ([-1, 0, 0], -5), ([0, 1, 0], 0), ([1, 0, 0], 0), ([0, 0, 1], 0)] """ if isinstance(poly, Polygon): vertices = list(poly.vertices) + [poly.vertices[0] ] # Close the polygon params = [None] * (len(vertices) - 1) for i in range(len(vertices) - 1): v1 = vertices[i] v2 = vertices[i + 1] a1 = v1[1] - v2[1] a2 = v2[0] - v1[0] b = v2[0] * v1[1] - v2[1] * v1[0] factor = gcd_list([a1, a2, b]) b = S(b) / factor a = (S(a1) / factor, S(a2) / factor) params[i] = (a, b) else: params = [None] * len(poly) for i, polygon in enumerate(poly): v1, v2, v3 = [vertices[vertex] for vertex in polygon[:3]] normal = cross_product(v1, v2, v3) b = sum([normal[j] * v1[j] for j in range(0, 3)]) fac = gcd_list(normal) if fac.is_zero: fac = 1 normal = [j / fac for j in normal] b = b / fac params[i] = (normal, b) return params
def hyperplane_parameters(poly, vertices=None): """A helper function to return the hyperplane parameters of which the facets of the polytope are a part of. Parameters ========== poly : The input 2/3-Polytope vertices : Vertex indices of 3-Polytope Examples ======== >>> from sympy.geometry.point import Point >>> from sympy.geometry.polygon import Polygon >>> from sympy.integrals.intpoly import hyperplane_parameters >>> hyperplane_parameters(Polygon(Point(0, 3), Point(5, 3), Point(1, 1))) [((0, 1), 3), ((1, -2), -1), ((-2, -1), -3)] >>> cube = [[(0, 0, 0), (0, 0, 5), (0, 5, 0), (0, 5, 5), (5, 0, 0),\ (5, 0, 5), (5, 5, 0), (5, 5, 5)],\ [2, 6, 7, 3], [3, 7, 5, 1], [7, 6, 4, 5], [1, 5, 4, 0],\ [3, 1, 0, 2], [0, 4, 6, 2]] >>> hyperplane_parameters(cube[1:], cube[0]) [([0, -1, 0], -5), ([0, 0, -1], -5), ([-1, 0, 0], -5), ([0, 1, 0], 0), ([1, 0, 0], 0), ([0, 0, 1], 0)] """ if isinstance(poly, Polygon): vertices = list(poly.vertices) + [poly.vertices[0]] # Close the polygon params = [None] * (len(vertices) - 1) for i in range(len(vertices) - 1): v1 = vertices[i] v2 = vertices[i + 1] a1 = v1[1] - v2[1] a2 = v2[0] - v1[0] b = v2[0] * v1[1] - v2[1] * v1[0] factor = gcd_list([a1, a2, b]) b = S(b) / factor a = (S(a1) / factor, S(a2) / factor) params[i] = (a, b) else: params = [None] * len(poly) for i, polygon in enumerate(poly): v1, v2, v3 = [vertices[vertex] for vertex in polygon[:3]] normal = cross_product(v1, v2, v3) b = sum([normal[j] * v1[j] for j in range(0, 3)]) fac = gcd_list(normal) if fac is S.Zero: fac = 1 normal = [j / fac for j in normal] b = b / fac params[i] = (normal, b) return params
def hyperplane_parameters(poly, vertices=None): """A helper function to return the hyperplane parameters of which the facets of the polygon are a part of. Currently works for only 2-Polytopes. Parameters ========== poly : The input Polygon Optional Parameters ---------------------- vertices : Vertex indices of faces of 3-Polytope Examples ======== >>> from sympy.geometry.point import Point >>> from sympy.geometry.polygon import Polygon >>> from sympy.integrals.intpoly import hyperplane_parameters >>> hyperplane_parameters(Polygon(Point(0, 3), Point(5, 3), Point(1, 1))) [((0, 1), 3), ((1, -2), -1), ((-2, -1), -3)] """ if isinstance(poly, Polygon): vertices = list(poly.vertices) + [poly.vertices[0] ] # Close the polygon params = [None] * (len(vertices) - 1) for i in range(len(vertices) - 1): v1 = vertices[i] v2 = vertices[i + 1] a1 = v1[1] - v2[1] a2 = v2[0] - v1[0] b = v2[0] * v1[1] - v2[1] * v1[0] factor = gcd_list([a1, a2, b]) b = S(b) / factor a = (S(a1) / factor, S(a2) / factor) params[i] = (a, b) else: params = [None] * len(poly) for i, polygon in enumerate(poly): v1, v2, v3 = [vertices[vertex] for vertex in polygon[:3]] normal = cross_product(v1, v2, v3) b = sum([normal[j] * v1[j] for j in range(0, 3)]) fac = gcd_list(normal) if fac is S.Zero: fac = 1 normal = [j / fac for j in normal] b = b / fac params[i] = (normal, b) return params
def hyperplane_parameters(poly, vertices=None): """A helper function to return the hyperplane parameters of which the facets of the polygon are a part of. Currently works for only 2-Polytopes. Parameters ========== poly : The input Polygon Optional Parameters ---------------------- vertices : Vertex indices of faces of 3-Polytope Examples ======== >>> from sympy.geometry.point import Point >>> from sympy.geometry.polygon import Polygon >>> from sympy.integrals.intpoly import hyperplane_parameters >>> hyperplane_parameters(Polygon(Point(0, 3), Point(5, 3), Point(1, 1))) [((0, 1), 3), ((1, -2), -1), ((-2, -1), -3)] """ if isinstance(poly, Polygon): vertices = list(poly.vertices) + [poly.vertices[0]] # Close the polygon params = [None] * (len(vertices) - 1) for i in range(len(vertices) - 1): v1 = vertices[i] v2 = vertices[i + 1] a1 = v1[1] - v2[1] a2 = v2[0] - v1[0] b = v2[0] * v1[1] - v2[1] * v1[0] factor = gcd_list([a1, a2, b]) b = S(b) / factor a = (S(a1) / factor, S(a2) / factor) params[i] = (a, b) else: params = [None] * len(poly) for i, polygon in enumerate(poly): v1, v2, v3 = [vertices[vertex] for vertex in polygon[:3]] normal = cross_product(v1, v2, v3) b = sum([normal[j] * v1[j] for j in range(0, 3)]) fac = gcd_list(normal) if fac is S.Zero: fac = 1 normal = [j / fac for j in normal] b = b / fac params[i] = (normal, b) return params
def _integer_basis(poly): """Compute coefficient basis for a polynomial over integers. """ monoms, coeffs = zip(*poly.terms()) monoms, = zip(*monoms) coeffs = map(abs, coeffs) if coeffs[0] < coeffs[-1]: coeffs = list(reversed(coeffs)) else: return None monoms = monoms[:-1] coeffs = coeffs[:-1] divs = reversed(divisors(gcd_list(coeffs))[1:]) try: div = divs.next() except StopIteration: return None while True: for monom, coeff in zip(monoms, coeffs): if coeff % div**monom != 0: try: div = divs.next() except StopIteration: return None else: break else: return div
def hyperplane_parameters(poly): """A helper function to return the hyperplane parameters of which the facets of the polygon are a part of. Currently works for only 2-Polytopes. Parameters ========== poly : The input Polygon Examples ======== >>> from sympy.geometry.point import Point >>> from sympy.geometry.polygon import Polygon >>> from sympy.integrals.intpoly import hyperplane_parameters >>> hyperplane_parameters(Polygon(Point(0, 3), Point(5, 3), Point(1, 1))) [((0, 1), 3), ((1, -2), -1), ((-2, -1), -3)] """ vertices = list(poly.vertices) + [poly.vertices[0]] # Close the polygon. params = [None] * (len(vertices) - 1) for i in range(len(vertices) - 1): v1 = vertices[i] v2 = vertices[i + 1] a1 = v1[1] - v2[1] a2 = v2[0] - v1[0] b = v2[0] * v1[1] - v2[1] * v1[0] factor = gcd_list([a1, a2, b]) b = S(b)/factor a = (S(a1)/factor, S(a2)/factor) params[i] = (a, b) return params
def hyperplane_parameters(poly): """A helper function to return the hyperplane parameters of which the facets of the polygon are a part of. Currently works for only 2-Polytopes. Parameters ========== poly : The input Polygon Examples ======== >>> from sympy.geometry.point import Point >>> from sympy.geometry.polygon import Polygon >>> from sympy.integrals.intpoly import hyperplane_parameters >>> hyperplane_parameters(Polygon(Point(0, 3), Point(5, 3), Point(1, 1))) [((0, 1), 3), ((1, -2), -1), ((-2, -1), -3)] """ vertices = list(poly.vertices) + [poly.vertices[0]] # Close the polygon. params = [None] * (len(vertices) - 1) for i in range(len(vertices) - 1): v1 = vertices[i] v2 = vertices[i + 1] a1 = v1[1] - v2[1] a2 = v2[0] - v1[0] b = v2[0] * v1[1] - v2[1] * v1[0] factor = gcd_list([a1, a2, b]) b = S(b) / factor a = (S(a1) / factor, S(a2) / factor) params[i] = (a, b) return params
def _integer_basis(poly): """Compute coefficient basis for a polynomial over integers. Returns the integer ``div`` such that substituting ``x = div*y`` ``p(x) = m*q(y)`` where the coefficients of ``q`` are smaller than those of ``p``. For example ``x**5 + 512*x + 1024 = 0`` with ``div = 4`` becomes ``y**5 + 2*y + 1 = 0`` Returns the integer ``div`` or ``None`` if there is no possible scaling. Examples ======== >>> from sympy.polys import Poly >>> from sympy.abc import x >>> from sympy.polys.polyroots import _integer_basis >>> p = Poly(x**5 + 512*x + 1024, x, domain='ZZ') >>> _integer_basis(p) 4 """ monoms, coeffs = list(zip(*poly.terms())) monoms, = list(zip(*monoms)) coeffs = list(map(abs, coeffs)) if coeffs[0] < coeffs[-1]: coeffs = list(reversed(coeffs)) n = monoms[0] monoms = [n - i for i in reversed(monoms)] else: return None monoms = monoms[:-1] coeffs = coeffs[:-1] divs = reversed(divisors(gcd_list(coeffs))[1:]) try: div = next(divs) except StopIteration: return None while True: for monom, coeff in zip(monoms, coeffs): if coeff % div**monom != 0: try: div = next(divs) except StopIteration: return None else: break else: return div