def elim(pols, v): deg = lambda p: p.degree(v) els = [] for p in pols: if deg(p) < 2: continue for r in trunc(p, v): for j in range(deg(r) - 1): els.append(sRes(r, r.diff(v), j)) tps = truncs(pols) for r in tps: for s in tps: if deg(r) > deg(s): for j in range(deg(s)): els.append(sRes(r, s, j)) elif deg(s) > deg(r): for j in range(deg(r)): els.append(sRes(s, r, j)) else: rb = LC(s, v) * r - LC(R, v) * s for j in range(deg(rb)): els.append(sRes(s, rb, j)) for r in tps: els.append(LC(r, v)) return els
def strip(monom): if monom == S.Zero: return 0, 0 elif monom.is_number: return monom, 1 else: coeff = LC(monom) return coeff, S(monom) / coeff
def strip(monom): if monom.is_zero: return S.Zero, S.Zero elif monom.is_number: return monom, S.One else: coeff = LC(monom) return coeff, monom / coeff
def PSC(F, G, x): n = min(degree(F, x), degree(G, x)) if n < 0: return [] subs = sRes(F, G, -1, x)[2:] # subresultants PRS s = [] for p in reversed(subs): s.append(LC(p, x)) return s
def sres(j): n = p + q - 2 * j if j == p: return sign(LC(P, v)) elif p > j > q: return 0 sh = SyHa(P, Q, j, v) assert sh.shape[0] == n return (sh[:, :n]).det()
def proj2(poly_set, x): p_out = [] # F is a polynomial in A for i in range(len(poly_set)): F = poly_set[i] R = reducta(F, x) for j in range(i + 1, len(poly_set)): G = poly_set[j] S = reducta(G, x) for H in R: if degree(H, x) > 0: for I in S: if degree(I, x) > 0: if degree(H, x) > degree(I, x): psc = PSC(H, I, x) if len(psc) > 0: if degree(I, x) == 1: p_out.append(poly(psc[0])) else: for aux in psc[0:degree(I, x) - 1]: p_out.append(poly(aux)) elif degree(H, x) < degree(I, x): psc = PSC(I, H, x) if len(psc) > 0: if degree(H, x) == 1: p_out.append(poly(psc[0])) else: for aux in psc[0:degree(H, x) - 1]: p_out.append(poly(aux)) elif degree(H, x) == degree(I, x): HH = H.mul_ground( LC(I)).add(-I.mul_ground(LC(H))) psc = PSC(I, HH, x) if len(psc) > 0: if degree(HH, x) == 1: p_out.append(poly(psc[0])) else: for aux in psc[0:degree(HH, x) - 1]: p_out.append(poly(aux)) return p_out
def sres(j): n = p + q - 2 * j if j == p: return sign(LC(P, v)) return (SyHa(P, Q, j, v)[:, :n]).det()
def polytope_integrate(poly, expr, **kwargs): """Integrates polynomials over 2/3-Polytopes. This function accepts the polytope in `poly` and the function in `expr` (uni/bi/trivariate polynomials are implemented) and returns the exact integral of `expr` over `poly`. Parameters ========== poly : The input Polygon. expr : The input polynomial. Optional Parameters: -------------------- clockwise : Binary value to sort input points of the 2-Polytope clockwise. max_degree : The maximum degree of any monomial of the input polynomial. Examples ======== >>> from sympy.abc import x, y >>> from sympy.geometry.polygon import Polygon >>> from sympy.geometry.point import Point >>> from sympy.integrals.intpoly import polytope_integrate >>> polygon = Polygon(Point(0,0), Point(0,1), Point(1,1), Point(1,0)) >>> polys = [1, x, y, x*y, x**2*y, x*y**2] >>> expr = x*y >>> polytope_integrate(polygon, expr) 1/4 >>> polytope_integrate(polygon, polys, max_degree=3) {1: 1, x: 1/2, y: 1/2, x*y: 1/4, x*y**2: 1/6, x**2*y: 1/6} """ clockwise = kwargs.get('clockwise', False) max_degree = kwargs.get('max_degree', None) if clockwise is True: if isinstance(poly, Polygon): poly = point_sort(poly) else: raise TypeError("clockwise=True works for only 2-Polytope" "V-representation input") expr = S(expr) if isinstance(poly, Polygon): # For Vertex Representation(2D case) hp_params = hyperplane_parameters(poly) facets = poly.sides elif len(poly[0]) == 2: # For Hyperplane Representation(2D case) plen = len(poly) if len(poly[0][0]) == 2: intersections = [intersection(poly[(i - 1) % plen], poly[i], "plane2D") for i in range(0, plen)] hp_params = poly lints = len(intersections) facets = [Segment2D(intersections[i], intersections[(i + 1) % lints]) for i in range(0, lints)] else: raise NotImplementedError("Integration for H-representation 3D" "case not implemented yet.") else: # For Vertex Representation(3D case) vertices = poly[0] facets = poly[1:] hp_params = hyperplane_parameters(facets, vertices) return main_integrate3d(expr, facets, vertices, hp_params) if max_degree is not None: result = {} if not isinstance(expr, list): raise TypeError('Input polynomials must be list of expressions') result_dict = main_integrate(0, facets, hp_params, max_degree) for poly in expr: if poly not in result: if poly is S.Zero: result[S.Zero] = S.Zero continue integral_value = S.Zero monoms = decompose(poly, separate=True) for monom in monoms: if monom.is_number: integral_value += result_dict[1] * monom else: coeff = LC(monom) integral_value += result_dict[monom / coeff] * coeff result[poly] = integral_value return result return main_integrate(expr, facets, hp_params)