def projection(self, pt): """Project the given point onto the plane along the plane normal. Parameters ========== Point or Point3D Returns ======= Point3D Examples ======== >>> from sympy import Plane, Point, Point3D >>> A = Plane(Point3D(1, 1, 2), normal_vector=(1, 1, 1)) The projection is along the normal vector direction, not the z axis, so (1, 1) does not project to (1, 1, 2) on the plane A: >>> b = Point3D(1, 1) >>> A.projection(b) Point3D(5/3, 5/3, 2/3) >>> _ in A True But the point (1, 1, 2) projects to (1, 1) on the XY-plane: >>> XY = Plane((0, 0, 0), (0, 0, 1)) >>> XY.projection((1, 1, 2)) Point3D(1, 1, 0) """ rv = Point(pt, dim=3) if rv in self: return rv return self.intersection(Line3D(rv, rv + Point3D(self.normal_vector)))[0]
def scale(self, x=1, y=1, pt=None): """Override GeometryEntity.scale since Curve is not made up of Points. Returns ======= Curve : returns scaled curve. Examples ======== >>> from sympy import Curve >>> from sympy.abc import x >>> Curve((x, x), (x, 0, 1)).scale(2) Curve((2*x, x), (x, 0, 1)) """ if pt: pt = Point(pt, dim=2) return self.translate(*(-pt).args).scale(x, y).translate(*pt.args) fx, fy = self.functions return self.func((fx*x, fy*y), self.limits)
def perpendicular_plane(self, *pts): """ Return a perpendicular passing through the given points. If the direction ratio between the points is the same as the Plane's normal vector then, to select from the infinite number of possible planes, a third point will be chosen on the z-axis (or the y-axis if the normal vector is already parallel to the z-axis). If less than two points are given they will be supplied as follows: if no point is given then pt1 will be self.p1; if a second point is not given it will be a point through pt1 on a line parallel to the z-axis (if the normal is not already the z-axis, otherwise on the line parallel to the y-axis). Parameters ========== pts: 0, 1 or 2 Point3D Returns ======= Plane Examples ======== >>> from sympy import Plane, Point3D, Line3D >>> a, b = Point3D(0, 0, 0), Point3D(0, 1, 0) >>> Z = (0, 0, 1) >>> p = Plane(a, normal_vector=Z) >>> p.perpendicular_plane(a, b) Plane(Point3D(0, 0, 0), (1, 0, 0)) """ if len(pts) > 2: raise ValueError("No more than 2 pts should be provided.") pts = list(pts) if len(pts) == 0: pts.append(self.p1) if len(pts) == 1: x, y, z = self.normal_vector if x == y == 0: dir = (0, 1, 0) else: dir = (0, 0, 1) pts.append(pts[0] + Point3D(*dir)) p1, p2 = [Point(i, dim=3) for i in pts] l = Line3D(p1, p2) n = Line3D(p1, direction_ratio=self.normal_vector) if l in n: # XXX should an error be raised instead? # there are infinitely many perpendicular planes; x, y, z = self.normal_vector if x == y == 0: # the z axis is the normal so pick a pt on the y-axis p3 = Point3D(0, 1, 0) # case 1 else: # else pick a pt on the z axis p3 = Point3D(0, 0, 1) # case 2 # in case that point is already given, move it a bit if p3 in l: p3 *= 2 # case 3 else: p3 = p1 + Point3D(*self.normal_vector) # case 4 return Plane(p1, p2, p3)
def intersection(self, o): """ The intersection with other geometrical entity. Parameters ========== Point, Point3D, LinearEntity, LinearEntity3D, Plane Returns ======= List Examples ======== >>> from sympy import Point, Point3D, Line, Line3D, Plane >>> a = Plane(Point3D(1, 2, 3), normal_vector=(1, 1, 1)) >>> b = Point3D(1, 2, 3) >>> a.intersection(b) [Point3D(1, 2, 3)] >>> c = Line3D(Point3D(1, 4, 7), Point3D(2, 2, 2)) >>> a.intersection(c) [Point3D(2, 2, 2)] >>> d = Plane(Point3D(6, 0, 0), normal_vector=(2, -5, 3)) >>> e = Plane(Point3D(2, 0, 0), normal_vector=(3, 4, -3)) >>> d.intersection(e) [Line3D(Point3D(78/23, -24/23, 0), Point3D(147/23, 321/23, 23))] """ from sympy.geometry.line import LinearEntity, LinearEntity3D if not isinstance(o, GeometryEntity): o = Point(o, dim=3) if isinstance(o, Point): if o in self: return [o] else: return [] if isinstance(o, (LinearEntity, LinearEntity3D)): # recast to 3D p1, p2 = o.p1, o.p2 if isinstance(o, Segment): o = Segment3D(p1, p2) elif isinstance(o, Ray): o = Ray3D(p1, p2) elif isinstance(o, Line): o = Line3D(p1, p2) else: raise ValueError("unhandled linear entity: %s" % o.func) if o in self: return [o] else: t = Dummy() # unnamed else it may clash with a symbol in o a = Point3D(o.arbitrary_point(t)) p1, n = self.p1, Point3D(self.normal_vector) # TODO: Replace solve with solveset, when this line is tested c = solve((a - p1).dot(n), t) if not c: return [] else: c = [i for i in c if i.is_real is not False] if len(c) > 1: c = [i for i in c if i.is_real] if len(c) != 1: raise Undecidable("not sure which point is real") p = a.subs(t, c[0]) if p not in o: return [] # e.g. a segment might not intersect a plane return [p] if isinstance(o, Plane): if self.equals(o): return [self] if self.is_parallel(o): return [] else: x, y, z = map(Dummy, "xyz") a, b = Matrix([self.normal_vector]), Matrix([o.normal_vector]) c = list(a.cross(b)) d = self.equation(x, y, z) e = o.equation(x, y, z) result = list(linsolve([d, e], x, y, z))[0] for i in (x, y, z): result = result.subs(i, 0) return [Line3D(Point3D(result), direction_ratio=c)]
def test_polytope_integrate(): # Convex 2-Polytopes # Vertex representation assert polytope_integrate(Polygon(Point(0, 0), Point(0, 2), Point(4, 0)), 1, dims=(x, y)) == 4 assert polytope_integrate(Polygon(Point(0, 0), Point(0, 1), Point(1, 1), Point(1, 0)), x * y) ==\ S(1)/4 assert polytope_integrate(Polygon(Point(0, 3), Point(5, 3), Point(1, 1)), 6*x**2 - 40*y) == S(-935)/3 assert polytope_integrate(Polygon(Point(0, 0), Point(0, sqrt(3)), Point(sqrt(3), sqrt(3)), Point(sqrt(3), 0)), 1) == 3 hexagon = Polygon(Point(0, 0), Point(-sqrt(3) / 2, S(1)/2), Point(-sqrt(3) / 2, S(3) / 2), Point(0, 2), Point(sqrt(3) / 2, S(3) / 2), Point(sqrt(3) / 2, S(1)/2)) assert polytope_integrate(hexagon, 1) == S(3*sqrt(3)) / 2 # Hyperplane representation assert polytope_integrate([((-1, 0), 0), ((1, 2), 4), ((0, -1), 0)], 1, dims=(x, y)) == 4 assert polytope_integrate([((-1, 0), 0), ((0, 1), 1), ((1, 0), 1), ((0, -1), 0)], x * y) == S(1)/4 assert polytope_integrate([((0, 1), 3), ((1, -2), -1), ((-2, -1), -3)], 6*x**2 - 40*y) == S(-935)/3 assert polytope_integrate([((-1, 0), 0), ((0, sqrt(3)), 3), ((sqrt(3), 0), 3), ((0, -1), 0)], 1) == 3 hexagon = [((-S(1) / 2, -sqrt(3) / 2), 0), ((-1, 0), sqrt(3) / 2), ((-S(1) / 2, sqrt(3) / 2), sqrt(3)), ((S(1) / 2, sqrt(3) / 2), sqrt(3)), ((1, 0), sqrt(3) / 2), ((S(1) / 2, -sqrt(3) / 2), 0)] assert polytope_integrate(hexagon, 1) == S(3*sqrt(3)) / 2 # Non-convex polytopes # Vertex representation assert polytope_integrate(Polygon(Point(-1, -1), Point(-1, 1), Point(1, 1), Point(0, 0), Point(1, -1)), 1) == 3 assert polytope_integrate(Polygon(Point(-1, -1), Point(-1, 1), Point(0, 0), Point(1, 1), Point(1, -1), Point(0, 0)), 1) == 2 # Hyperplane representation assert polytope_integrate([((-1, 0), 1), ((0, 1), 1), ((1, -1), 0), ((1, 1), 0), ((0, -1), 1)], 1) == 3 assert polytope_integrate([((-1, 0), 1), ((1, 1), 0), ((-1, 1), 0), ((1, 0), 1), ((-1, -1), 0), ((1, -1), 0)], 1) == 2 # Tests for 2D polytopes mentioned in Chin et al(Page 10): # http://dilbert.engr.ucdavis.edu/~suku/quadrature/cls-integration.pdf fig1 = Polygon(Point(1.220, -0.827), Point(-1.490, -4.503), Point(-3.766, -1.622), Point(-4.240, -0.091), Point(-3.160, 4), Point(-0.981, 4.447), Point(0.132, 4.027)) assert polytope_integrate(fig1, x**2 + x*y + y**2) ==\ S(2031627344735367)/(8*10**12) fig2 = Polygon(Point(4.561, 2.317), Point(1.491, -1.315), Point(-3.310, -3.164), Point(-4.845, -3.110), Point(-4.569, 1.867)) assert polytope_integrate(fig2, x**2 + x*y + y**2) ==\ S(517091313866043)/(16*10**11) fig3 = Polygon(Point(-2.740, -1.888), Point(-3.292, 4.233), Point(-2.723, -0.697), Point(-0.643, -3.151)) assert polytope_integrate(fig3, x**2 + x*y + y**2) ==\ S(147449361647041)/(8*10**12) fig4 = Polygon(Point(0.211, -4.622), Point(-2.684, 3.851), Point(0.468, 4.879), Point(4.630, -1.325), Point(-0.411, -1.044)) assert polytope_integrate(fig4, x**2 + x*y + y**2) ==\ S(180742845225803)/(10**12) # Tests for many polynomials with maximum degree given(2D case). tri = Polygon(Point(0, 3), Point(5, 3), Point(1, 1)) polys = [] expr1 = x**9*y + x**7*y**3 + 2*x**2*y**8 expr2 = x**6*y**4 + x**5*y**5 + 2*y**10 expr3 = x**10 + x**9*y + x**8*y**2 + x**5*y**5 polys.extend((expr1, expr2, expr3)) result_dict = polytope_integrate(tri, polys, max_degree=10) assert result_dict[expr1] == S(615780107)/594 assert result_dict[expr2] == S(13062161)/27 assert result_dict[expr3] == S(1946257153)/924 # Tests when all integral of all monomials up to a max_degree is to be # calculated. assert polytope_integrate(Polygon(Point(0, 0), Point(0, 1), Point(1, 1), Point(1, 0)), max_degree=4) == {0: 0, 1: 1, x: S(1) / 2, x ** 2 * y ** 2: S(1) / 9, x ** 4: S(1) / 5, y ** 4: S(1) / 5, y: S(1) / 2, x * y ** 2: S(1) / 6, y ** 2: S(1) / 3, x ** 3: S(1) / 4, x ** 2 * y: S(1) / 6, x ** 3 * y: S(1) / 8, x * y: S(1) / 4, y ** 3: S(1) / 4, x ** 2: S(1) / 3, x * y ** 3: S(1) / 8} # Tests for 3D polytopes cube1 = [[(0, 0, 0), (0, 6, 6), (6, 6, 6), (3, 6, 0), (0, 6, 0), (6, 0, 6), (3, 0, 0), (0, 0, 6)], [1, 2, 3, 4], [3, 2, 5, 6], [1, 7, 5, 2], [0, 6, 5, 7], [1, 4, 0, 7], [0, 4, 3, 6]] assert polytope_integrate(cube1, 1) == S(162) # 3D Test cases in Chin et al(2015) cube2 = [[(0, 0, 0), (0, 0, 5), (0, 5, 0), (0, 5, 5), (5, 0, 0), (5, 0, 5), (5, 5, 0), (5, 5, 5)], [3, 7, 6, 2], [1, 5, 7, 3], [5, 4, 6, 7], [0, 4, 5, 1], [2, 0, 1, 3], [2, 6, 4, 0]] cube3 = [[(0, 0, 0), (5, 0, 0), (5, 4, 0), (3, 2, 0), (3, 5, 0), (0, 5, 0), (0, 0, 5), (5, 0, 5), (5, 4, 5), (3, 2, 5), (3, 5, 5), (0, 5, 5)], [6, 11, 5, 0], [1, 7, 6, 0], [5, 4, 3, 2, 1, 0], [11, 10, 4, 5], [10, 9, 3, 4], [9, 8, 2, 3], [8, 7, 1, 2], [7, 8, 9, 10, 11, 6]] cube4 = [[(0, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1), (S(1) / 4, S(1) / 4, S(1) / 4)], [0, 2, 1], [1, 3, 0], [4, 2, 3], [4, 3, 1], [0, 1, 2], [2, 4, 1], [0, 3, 2]] assert polytope_integrate(cube2, x ** 2 + y ** 2 + x * y + z ** 2) ==\ S(15625)/4 assert polytope_integrate(cube3, x ** 2 + y ** 2 + x * y + z ** 2) ==\ S(33835) / 12 assert polytope_integrate(cube4, x ** 2 + y ** 2 + x * y + z ** 2) ==\ S(37) / 960 # Test cases from Mathematica's PolyhedronData library octahedron = [[(S(-1) / sqrt(2), 0, 0), (0, S(1) / sqrt(2), 0), (0, 0, S(-1) / sqrt(2)), (0, 0, S(1) / sqrt(2)), (0, S(-1) / sqrt(2), 0), (S(1) / sqrt(2), 0, 0)], [3, 4, 5], [3, 5, 1], [3, 1, 0], [3, 0, 4], [4, 0, 2], [4, 2, 5], [2, 0, 1], [5, 2, 1]] assert polytope_integrate(octahedron, 1) == sqrt(2) / 3 great_stellated_dodecahedron =\ [[(-0.32491969623290634095, 0, 0.42532540417601993887), (0.32491969623290634095, 0, -0.42532540417601993887), (-0.52573111211913359231, 0, 0.10040570794311363956), (0.52573111211913359231, 0, -0.10040570794311363956), (-0.10040570794311363956, -0.3090169943749474241, 0.42532540417601993887), (-0.10040570794311363956, 0.30901699437494742410, 0.42532540417601993887), (0.10040570794311363956, -0.3090169943749474241, -0.42532540417601993887), (0.10040570794311363956, 0.30901699437494742410, -0.42532540417601993887), (-0.16245984811645317047, -0.5, 0.10040570794311363956), (-0.16245984811645317047, 0.5, 0.10040570794311363956), (0.16245984811645317047, -0.5, -0.10040570794311363956), (0.16245984811645317047, 0.5, -0.10040570794311363956), (-0.42532540417601993887, -0.3090169943749474241, -0.10040570794311363956), (-0.42532540417601993887, 0.30901699437494742410, -0.10040570794311363956), (-0.26286555605956679615, 0.1909830056250525759, -0.42532540417601993887), (-0.26286555605956679615, -0.1909830056250525759, -0.42532540417601993887), (0.26286555605956679615, 0.1909830056250525759, 0.42532540417601993887), (0.26286555605956679615, -0.1909830056250525759, 0.42532540417601993887), (0.42532540417601993887, -0.3090169943749474241, 0.10040570794311363956), (0.42532540417601993887, 0.30901699437494742410, 0.10040570794311363956)], [12, 3, 0, 6, 16], [17, 7, 0, 3, 13], [9, 6, 0, 7, 8], [18, 2, 1, 4, 14], [15, 5, 1, 2, 19], [11, 4, 1, 5, 10], [8, 19, 2, 18, 9], [10, 13, 3, 12, 11], [16, 14, 4, 11, 12], [13, 10, 5, 15, 17], [14, 16, 6, 9, 18], [19, 8, 7, 17, 15]] # Actual volume is : 0.163118960624632 assert Abs(polytope_integrate(great_stellated_dodecahedron, 1) -\ 0.163118960624632) < 1e-12 expr = x **2 + y ** 2 + z ** 2 octahedron_five_compound = [[(0, -0.7071067811865475244, 0), (0, 0.70710678118654752440, 0), (0.1148764602736805918, -0.35355339059327376220, -0.60150095500754567366), (0.1148764602736805918, 0.35355339059327376220, -0.60150095500754567366), (0.18587401723009224507, -0.57206140281768429760, 0.37174803446018449013), (0.18587401723009224507, 0.57206140281768429760, 0.37174803446018449013), (0.30075047750377283683, -0.21850801222441053540, 0.60150095500754567366), (0.30075047750377283683, 0.21850801222441053540, 0.60150095500754567366), (0.48662449473386508189, -0.35355339059327376220, -0.37174803446018449013), (0.48662449473386508189, 0.35355339059327376220, -0.37174803446018449013), (-0.60150095500754567366, 0, -0.37174803446018449013), (-0.30075047750377283683, -0.21850801222441053540, -0.60150095500754567366), (-0.30075047750377283683, 0.21850801222441053540, -0.60150095500754567366), (0.60150095500754567366, 0, 0.37174803446018449013), (0.4156269377774534286, -0.57206140281768429760, 0), (0.4156269377774534286, 0.57206140281768429760, 0), (0.37174803446018449013, 0, -0.60150095500754567366), (-0.4156269377774534286, -0.57206140281768429760, 0), (-0.4156269377774534286, 0.57206140281768429760, 0), (-0.67249851196395732696, -0.21850801222441053540, 0), (-0.67249851196395732696, 0.21850801222441053540, 0), (0.67249851196395732696, -0.21850801222441053540, 0), (0.67249851196395732696, 0.21850801222441053540, 0), (-0.37174803446018449013, 0, 0.60150095500754567366), (-0.48662449473386508189, -0.35355339059327376220, 0.37174803446018449013), (-0.48662449473386508189, 0.35355339059327376220, 0.37174803446018449013), (-0.18587401723009224507, -0.57206140281768429760, -0.37174803446018449013), (-0.18587401723009224507, 0.57206140281768429760, -0.37174803446018449013), (-0.11487646027368059176, -0.35355339059327376220, 0.60150095500754567366), (-0.11487646027368059176, 0.35355339059327376220, 0.60150095500754567366)], [0, 10, 16], [23, 10, 0], [16, 13, 0], [0, 13, 23], [16, 10, 1], [1, 10, 23], [1, 13, 16], [23, 13, 1], [2, 4, 19], [22, 4, 2], [2, 19, 27], [27, 22, 2], [20, 5, 3], [3, 5, 21], [26, 20, 3], [3, 21, 26], [29, 19, 4], [4, 22, 29], [5, 20, 28], [28, 21, 5], [6, 8, 15], [17, 8, 6], [6, 15, 25], [25, 17, 6], [14, 9, 7], [7, 9, 18], [24, 14, 7], [7, 18, 24], [8, 12, 15], [17, 12, 8], [14, 11, 9], [9, 11, 18], [11, 14, 24], [24, 18, 11], [25, 15, 12], [12, 17, 25], [29, 27, 19], [20, 26, 28], [28, 26, 21], [22, 27, 29]] assert Abs(polytope_integrate(octahedron_five_compound, expr)) - 0.353553\ < 1e-6 cube_five_compound = [[(-0.1624598481164531631, -0.5, -0.6881909602355867691), (-0.1624598481164531631, 0.5, -0.6881909602355867691), (0.1624598481164531631, -0.5, 0.68819096023558676910), (0.1624598481164531631, 0.5, 0.68819096023558676910), (-0.52573111211913359231, 0, -0.6881909602355867691), (0.52573111211913359231, 0, 0.68819096023558676910), (-0.26286555605956679615, -0.8090169943749474241, -0.1624598481164531631), (-0.26286555605956679615, 0.8090169943749474241, -0.1624598481164531631), (0.26286555605956680301, -0.8090169943749474241, 0.1624598481164531631), (0.26286555605956680301, 0.8090169943749474241, 0.1624598481164531631), (-0.42532540417601993887, -0.3090169943749474241, 0.68819096023558676910), (-0.42532540417601993887, 0.30901699437494742410, 0.68819096023558676910), (0.42532540417601996609, -0.3090169943749474241, -0.6881909602355867691), (0.42532540417601996609, 0.30901699437494742410, -0.6881909602355867691), (-0.6881909602355867691, -0.5, 0.1624598481164531631), (-0.6881909602355867691, 0.5, 0.1624598481164531631), (0.68819096023558676910, -0.5, -0.1624598481164531631), (0.68819096023558676910, 0.5, -0.1624598481164531631), (-0.85065080835203998877, 0, -0.1624598481164531631), (0.85065080835203993218, 0, 0.1624598481164531631)], [18, 10, 3, 7], [13, 19, 8, 0], [18, 0, 8, 10], [3, 19, 13, 7], [18, 7, 13, 0], [8, 19, 3, 10], [6, 2, 11, 18], [1, 9, 19, 12], [11, 9, 1, 18], [6, 12, 19, 2], [1, 12, 6, 18], [11, 2, 19, 9], [4, 14, 11, 7], [17, 5, 8, 12], [4, 12, 8, 14], [11, 5, 17, 7], [4, 7, 17, 12], [8, 5, 11, 14], [6, 10, 15, 4], [13, 9, 5, 16], [15, 9, 13, 4], [6, 16, 5, 10], [13, 16, 6, 4], [15, 10, 5, 9], [14, 15, 1, 0], [16, 17, 3, 2], [14, 2, 3, 15], [1, 17, 16, 0], [14, 0, 16, 2], [3, 17, 1, 15]] assert Abs(polytope_integrate(cube_five_compound, expr) - 1.25) < 1e-12 echidnahedron = [[(0, 0, -2.4898982848827801995), (0, 0, 2.4898982848827802734), (0, -4.2360679774997896964, -2.4898982848827801995), (0, -4.2360679774997896964, 2.4898982848827802734), (0, 4.2360679774997896964, -2.4898982848827801995), (0, 4.2360679774997896964, 2.4898982848827802734), (-4.0287400534704067567, -1.3090169943749474241, -2.4898982848827801995), (-4.0287400534704067567, -1.3090169943749474241, 2.4898982848827802734), (-4.0287400534704067567, 1.3090169943749474241, -2.4898982848827801995), (-4.0287400534704067567, 1.3090169943749474241, 2.4898982848827802734), (4.0287400534704069747, -1.3090169943749474241, -2.4898982848827801995), (4.0287400534704069747, -1.3090169943749474241, 2.4898982848827802734), (4.0287400534704069747, 1.3090169943749474241, -2.4898982848827801995), (4.0287400534704069747, 1.3090169943749474241, 2.4898982848827802734), (-2.4898982848827801995, -3.4270509831248422723, -2.4898982848827801995), (-2.4898982848827801995, -3.4270509831248422723, 2.4898982848827802734), (-2.4898982848827801995, 3.4270509831248422723, -2.4898982848827801995), (-2.4898982848827801995, 3.4270509831248422723, 2.4898982848827802734), (2.4898982848827802734, -3.4270509831248422723, -2.4898982848827801995), (2.4898982848827802734, -3.4270509831248422723, 2.4898982848827802734), (2.4898982848827802734, 3.4270509831248422723, -2.4898982848827801995), (2.4898982848827802734, 3.4270509831248422723, 2.4898982848827802734), (-4.7169310137059934362, -0.8090169943749474241, -1.1135163644116066184), (-4.7169310137059934362, 0.8090169943749474241, -1.1135163644116066184), (4.7169310137059937438, -0.8090169943749474241, 1.11351636441160673519), (4.7169310137059937438, 0.8090169943749474241, 1.11351636441160673519), (-4.2916056095299737777, -2.1180339887498948482, 1.11351636441160673519), (-4.2916056095299737777, 2.1180339887498948482, 1.11351636441160673519), (4.2916056095299737777, -2.1180339887498948482, -1.1135163644116066184), (4.2916056095299737777, 2.1180339887498948482, -1.1135163644116066184), (-3.6034146492943870399, 0, -3.3405490932348205213), (3.6034146492943870399, 0, 3.3405490932348202056), (-3.3405490932348205213, -3.4270509831248422723, 1.11351636441160673519), (-3.3405490932348205213, 3.4270509831248422723, 1.11351636441160673519), (3.3405490932348202056, -3.4270509831248422723, -1.1135163644116066184), (3.3405490932348202056, 3.4270509831248422723, -1.1135163644116066184), (-2.9152236890588002395, -2.1180339887498948482, 3.3405490932348202056), (-2.9152236890588002395, 2.1180339887498948482, 3.3405490932348202056), (2.9152236890588002395, -2.1180339887498948482, -3.3405490932348205213), (2.9152236890588002395, 2.1180339887498948482, -3.3405490932348205213), (-2.2270327288232132368, 0, -1.1135163644116066184), (-2.2270327288232132368, -4.2360679774997896964, -1.1135163644116066184), (-2.2270327288232132368, 4.2360679774997896964, -1.1135163644116066184), (2.2270327288232134704, 0, 1.11351636441160673519), (2.2270327288232134704, -4.2360679774997896964, 1.11351636441160673519), (2.2270327288232134704, 4.2360679774997896964, 1.11351636441160673519), (-1.8017073246471935200, -1.3090169943749474241, 1.11351636441160673519), (-1.8017073246471935200, 1.3090169943749474241, 1.11351636441160673519), (1.8017073246471935043, -1.3090169943749474241, -1.1135163644116066184), (1.8017073246471935043, 1.3090169943749474241, -1.1135163644116066184), (-1.3763819204711735382, 0, -4.7169310137059934362), (-1.3763819204711735382, 0, 0.26286555605956679615), (1.37638192047117353821, 0, 4.7169310137059937438), (1.37638192047117353821, 0, -0.26286555605956679615), (-1.1135163644116066184, -3.4270509831248422723, -3.3405490932348205213), (-1.1135163644116066184, -0.8090169943749474241, 4.7169310137059937438), (-1.1135163644116066184, -0.8090169943749474241, -0.26286555605956679615), (-1.1135163644116066184, 0.8090169943749474241, 4.7169310137059937438), (-1.1135163644116066184, 0.8090169943749474241, -0.26286555605956679615), (-1.1135163644116066184, 3.4270509831248422723, -3.3405490932348205213), (1.11351636441160673519, -3.4270509831248422723, 3.3405490932348202056), (1.11351636441160673519, -0.8090169943749474241, -4.7169310137059934362), (1.11351636441160673519, -0.8090169943749474241, 0.26286555605956679615), (1.11351636441160673519, 0.8090169943749474241, -4.7169310137059934362), (1.11351636441160673519, 0.8090169943749474241, 0.26286555605956679615), (1.11351636441160673519, 3.4270509831248422723, 3.3405490932348202056), (-0.85065080835203998877, 0, 1.11351636441160673519), (0.85065080835203993218, 0, -1.1135163644116066184), (-0.6881909602355867691, -0.5, -1.1135163644116066184), (-0.6881909602355867691, 0.5, -1.1135163644116066184), (-0.6881909602355867691, -4.7360679774997896964, -1.1135163644116066184), (-0.6881909602355867691, -2.1180339887498948482, -1.1135163644116066184), (-0.6881909602355867691, 2.1180339887498948482, -1.1135163644116066184), (-0.6881909602355867691, 4.7360679774997896964, -1.1135163644116066184), (0.68819096023558676910, -0.5, 1.11351636441160673519), (0.68819096023558676910, 0.5, 1.11351636441160673519), (0.68819096023558676910, -4.7360679774997896964, 1.11351636441160673519), (0.68819096023558676910, -2.1180339887498948482, 1.11351636441160673519), (0.68819096023558676910, 2.1180339887498948482, 1.11351636441160673519), (0.68819096023558676910, 4.7360679774997896964, 1.11351636441160673519), (-0.42532540417601993887, -1.3090169943749474241, -4.7169310137059934362), (-0.42532540417601993887, -1.3090169943749474241, 0.26286555605956679615), (-0.42532540417601993887, 1.3090169943749474241, -4.7169310137059934362), (-0.42532540417601993887, 1.3090169943749474241, 0.26286555605956679615), (-0.26286555605956679615, -0.8090169943749474241, 1.11351636441160673519), (-0.26286555605956679615, 0.8090169943749474241, 1.11351636441160673519), (0.26286555605956679615, -0.8090169943749474241, -1.1135163644116066184), (0.26286555605956679615, 0.8090169943749474241, -1.1135163644116066184), (0.42532540417601996609, -1.3090169943749474241, 4.7169310137059937438), (0.42532540417601996609, -1.3090169943749474241, -0.26286555605956679615), (0.42532540417601996609, 1.3090169943749474241, 4.7169310137059937438), (0.42532540417601996609, 1.3090169943749474241, -0.26286555605956679615)], [9, 66, 47], [44, 62, 77], [20, 91, 49], [33, 47, 83], [3, 77, 84], [12, 49, 53], [36, 84, 66], [28, 53, 62], [73, 83, 91], [15, 84, 46], [25, 64, 43], [16, 58, 72], [26, 46, 51], [11, 43, 74], [4, 72, 91], [60, 74, 84], [35, 91, 64], [23, 51, 58], [19, 74, 77], [79, 83, 78], [6, 56, 40], [76, 77, 81], [21, 78, 75], [8, 40, 58], [31, 75, 74], [42, 58, 83], [41, 81, 56], [13, 75, 43], [27, 51, 47], [2, 89, 71], [24, 43, 62], [17, 47, 85], [14, 71, 56], [65, 85, 75], [22, 56, 51], [34, 62, 89], [5, 85, 78], [32, 81, 46], [10, 53, 48], [45, 78, 64], [7, 46, 66], [18, 48, 89], [37, 66, 85], [70, 89, 81], [29, 64, 53], [88, 74, 1], [38, 67, 48], [42, 83, 72], [57, 1, 85], [34, 48, 62], [59, 72, 87], [19, 62, 74], [63, 87, 67], [17, 85, 83], [52, 75, 1], [39, 87, 49], [22, 51, 40], [55, 1, 66], [29, 49, 64], [30, 40, 69], [13, 64, 75], [82, 69, 87], [7, 66, 51], [90, 85, 1], [59, 69, 72], [70, 81, 71], [88, 1, 84], [73, 72, 83], [54, 71, 68], [5, 83, 85], [50, 68, 69], [3, 84, 81], [57, 66, 1], [30, 68, 40], [28, 62, 48], [52, 1, 74], [23, 40, 51], [38, 48, 86], [9, 51, 66], [80, 86, 68], [11, 74, 62], [55, 84, 1], [54, 86, 71], [35, 64, 49], [90, 1, 75], [41, 71, 81], [39, 49, 67], [15, 81, 84], [61, 67, 86], [21, 75, 64], [24, 53, 43], [50, 69, 0], [37, 85, 47], [31, 43, 75], [61, 0, 67], [27, 47, 58], [10, 67, 53], [8, 58, 69], [90, 75, 85], [45, 91, 78], [80, 68, 0], [36, 66, 46], [65, 78, 85], [63, 0, 87], [32, 46, 56], [20, 87, 91], [14, 56, 68], [57, 85, 66], [33, 58, 47], [61, 86, 0], [60, 84, 77], [37, 47, 66], [82, 0, 69], [44, 77, 89], [16, 69, 58], [18, 89, 86], [55, 66, 84], [26, 56, 46], [63, 67, 0], [31, 74, 43], [36, 46, 84], [50, 0, 68], [25, 43, 53], [6, 68, 56], [12, 53, 67], [88, 84, 74], [76, 89, 77], [82, 87, 0], [65, 75, 78], [60, 77, 74], [80, 0, 86], [79, 78, 91], [2, 86, 89], [4, 91, 87], [52, 74, 75], [21, 64, 78], [18, 86, 48], [23, 58, 40], [5, 78, 83], [28, 48, 53], [6, 40, 68], [25, 53, 64], [54, 68, 86], [33, 83, 58], [17, 83, 47], [12, 67, 49], [41, 56, 71], [9, 47, 51], [35, 49, 91], [2, 71, 86], [79, 91, 83], [38, 86, 67], [26, 51, 56], [7, 51, 46], [4, 87, 72], [34, 89, 48], [15, 46, 81], [42, 72, 58], [10, 48, 67], [27, 58, 51], [39, 67, 87], [76, 81, 89], [3, 81, 77], [8, 69, 40], [29, 53, 49], [19, 77, 62], [22, 40, 56], [20, 49, 87], [32, 56, 81], [59, 87, 69], [24, 62, 53], [11, 62, 43], [14, 68, 71], [73, 91, 72], [13, 43, 64], [70, 71, 89], [16, 72, 69], [44, 89, 62], [30, 69, 68], [45, 64, 91]] # Actual volume is : 51.405764746872634 assert Abs(polytope_integrate(echidnahedron, 1) - 51.4057647468726) < 1e-12 assert Abs(polytope_integrate(echidnahedron, expr) - 253.569603474519) <\ 1e-12 # Tests for many polynomials with maximum degree given(2D case). assert polytope_integrate(cube2, [x**2, y*z], max_degree=2) == \ {y * z: 3125 / S(4), x ** 2: 3125 / S(3)} assert polytope_integrate(cube2, max_degree=2) == \ {1: 125, x: 625 / S(2), x * z: 3125 / S(4), y: 625 / S(2), y * z: 3125 / S(4), z ** 2: 3125 / S(3), y ** 2: 3125 / S(3), z: 625 / S(2), x * y: 3125 / S(4), x ** 2: 3125 / S(3)}
def test_polytopes_intersecting_sides(): fig5 = Polygon(Point(-4.165, -0.832), Point(-3.668, 1.568), Point(-3.266, 1.279), Point(-1.090, -2.080), Point(3.313, -0.683), Point(3.033, -4.845), Point(-4.395, 4.840), Point(-1.007, -3.328)) assert polytope_integrate(fig5, x**2 + x*y + y**2) ==\ S(1633405224899363)/(24*10**12) fig6 = Polygon(Point(-3.018, -4.473), Point(-0.103, 2.378), Point(-1.605, -2.308), Point(4.516, -0.771), Point(4.203, 0.478)) assert polytope_integrate(fig6, x**2 + x*y + y**2) ==\ S(88161333955921)/(3*10**12)
def test_best_origin(): expr1 = y ** 2 * x ** 5 + y ** 5 * x ** 7 + 7 * x + x ** 12 + y ** 7 * x l1 = Segment2D(Point(0, 3), Point(1, 1)) l2 = Segment2D(Point(S(3) / 2, 0), Point(S(3) / 2, 3)) l3 = Segment2D(Point(0, S(3) / 2), Point(3, S(3) / 2)) l4 = Segment2D(Point(0, 2), Point(2, 0)) l5 = Segment2D(Point(0, 2), Point(1, 1)) l6 = Segment2D(Point(2, 0), Point(1, 1)) assert best_origin((2, 1), 3, l1, expr1) == (0, 3) assert best_origin((2, 0), 3, l2, x ** 7) == (S(3) / 2, 0) assert best_origin((0, 2), 3, l3, x ** 7) == (0, S(3) / 2) assert best_origin((1, 1), 2, l4, x ** 7 * y ** 3) == (0, 2) assert best_origin((1, 1), 2, l4, x ** 3 * y ** 7) == (2, 0) assert best_origin((1, 1), 2, l5, x ** 2 * y ** 9) == (0, 2) assert best_origin((1, 1), 2, l6, x ** 9 * y ** 2) == (2, 0)
def encloses(self, o): """ Return True if o is inside (not on or outside) the boundaries of self. The object will be decomposed into Points and individual Entities need only define an encloses_point method for their class. See Also ======== sympy.geometry.ellipse.Ellipse.encloses_point sympy.geometry.polygon.Polygon.encloses_point Examples ======== >>> from sympy import RegularPolygon, Point, Polygon >>> t = Polygon(*RegularPolygon(Point(0, 0), 1, 3).vertices) >>> t2 = Polygon(*RegularPolygon(Point(0, 0), 2, 3).vertices) >>> t2.encloses(t) True >>> t.encloses(t2) False """ from sympy.geometry.point import Point from sympy.geometry.line import Segment, Ray, Line from sympy.geometry.ellipse import Ellipse from sympy.geometry.polygon import Polygon, RegularPolygon if isinstance(o, Point): return self.encloses_point(o) elif isinstance(o, Segment): return all(self.encloses_point(x) for x in o.points) elif isinstance(o, Ray) or isinstance(o, Line): return False elif isinstance(o, Ellipse): return self.encloses_point(o.center) and not self.intersection(o) and self.encloses_point(Point(o.center.x+o.hradius,o.center.y)) elif isinstance(o, Polygon): if isinstance(o, RegularPolygon): if not self.encloses_point(o.center): return False return all(self.encloses_point(v) for v in o.vertices) raise NotImplementedError()
def reflect(self, line): """ Reflects an object across a line. Parameters ========== line: Line Examples ======== >>> from sympy import pi, sqrt, Line, RegularPolygon >>> l = Line((0, pi), slope=sqrt(2)) >>> pent = RegularPolygon((1, 2), 1, 5) >>> rpent = pent.reflect(l) >>> rpent RegularPolygon(Point2D(-2*sqrt(2)*pi/3 - 1/3 + 4*sqrt(2)/3, 2/3 + 2*sqrt(2)/3 + 2*pi/3), -1, 5, -atan(2*sqrt(2)) + 3*pi/5) >>> from sympy import pi, Line, Circle, Point >>> l = Line((0, pi), slope=1) >>> circ = Circle(Point(0, 0), 5) >>> rcirc = circ.reflect(l) >>> rcirc Circle(Point2D(-pi, pi), -5) """ from sympy import atan, Point, Dummy, oo g = self l = line o = Point(0, 0) if l.slope.is_zero: y = l.args[0].y if not y: # x-axis return g.scale(y=-1) reps = [(p, p.translate(y=2 * (y - p.y))) for p in g.atoms(Point)] elif l.slope is oo: x = l.args[0].x if not x: # y-axis return g.scale(x=-1) reps = [(p, p.translate(x=2 * (x - p.x))) for p in g.atoms(Point)] else: if not hasattr(g, "reflect") and not all( isinstance(arg, Point) for arg in g.args ): raise NotImplementedError( "reflect undefined or non-Point args in %s" % g ) a = atan(l.slope) c = l.coefficients d = -c[-1] / c[1] # y-intercept # apply the transform to a single point x, y = Dummy(), Dummy() xf = Point(x, y) xf = ( xf.translate(y=-d).rotate(-a, o).scale(y=-1).rotate(a, o).translate(y=d) ) # replace every point using that transform reps = [(p, xf.xreplace({x: p.x, y: p.y})) for p in g.atoms(Point)] return g.xreplace(dict(reps))
def test_polytopes_intersecting_sides(): # Intersecting polygons not implemented yet in SymPy. Will be implemented # soon. As of now, the intersection point will have to be manually # supplied by user. fig5 = Polygon(Point(-4.165, -0.832), Point(-3.668, 1.568), Point(-3.266, 1.279), Point(-1.090, -2.080), Point(3.313, -0.683), Point(3.033, -4.845), Point(-4.395, 4.840), Point(-1.007, -3.328)) assert polytope_integrate(fig5, x**2 + x*y + y**2) ==\ S(1633405224899363)/(24*10**12) fig6 = Polygon(Point(-3.018, -4.473), Point(-0.103, 2.378), Point(-1.605, -2.308), Point(4.516, -0.771), Point(4.203, 0.478)) assert polytope_integrate(fig6, x**2 + x*y + y**2) ==\ S(88161333955921)/(3*10**12)
def test_polytope_integrate(): # Convex 2-Polytopes # Vertex representation assert polytope_integrate(Polygon(Point(0, 0), Point(0, 2), Point(4, 0)), 1, dims=(x, y)) == 4 assert polytope_integrate(Polygon(Point(0, 0), Point(0, 1), Point(1, 1), Point(1, 0)), x * y) ==\ S(1)/4 assert polytope_integrate(Polygon(Point(0, 3), Point(5, 3), Point(1, 1)), 6 * x**2 - 40 * y) == S(-935) / 3 assert polytope_integrate( Polygon(Point(0, 0), Point(0, sqrt(3)), Point(sqrt(3), sqrt(3)), Point(sqrt(3), 0)), 1) == 3 hexagon = Polygon(Point(0, 0), Point(-sqrt(3) / 2, S(1) / 2), Point(-sqrt(3) / 2, 3 / 2), Point(0, 2), Point(sqrt(3) / 2, 3 / 2), Point(sqrt(3) / 2, S(1) / 2)) assert polytope_integrate(hexagon, 1) == S(3 * sqrt(3)) / 2 # Hyperplane representation assert polytope_integrate([((-1, 0), 0), ((1, 2), 4), ((0, -1), 0)], 1, dims=(x, y)) == 4 assert polytope_integrate([((-1, 0), 0), ((0, 1), 1), ((1, 0), 1), ((0, -1), 0)], x * y) == S(1) / 4 assert polytope_integrate([((0, 1), 3), ((1, -2), -1), ((-2, -1), -3)], 6 * x**2 - 40 * y) == S(-935) / 3 assert polytope_integrate([((-1, 0), 0), ((0, sqrt(3)), 3), ((sqrt(3), 0), 3), ((0, -1), 0)], 1) == 3 hexagon = [((-1 / 2, -sqrt(3) / 2), 0), ((-1, 0), sqrt(3) / 2), ((-1 / 2, sqrt(3) / 2), sqrt(3)), ((1 / 2, sqrt(3) / 2), sqrt(3)), ((1, 0), sqrt(3) / 2), ((1 / 2, -sqrt(3) / 2), 0)] assert polytope_integrate(hexagon, 1) == S(3 * sqrt(3)) / 2 # Non-convex polytopes # Vertex representation assert polytope_integrate( Polygon(Point(-1, -1), Point(-1, 1), Point(1, 1), Point(0, 0), Point(1, -1)), 1) == 3 assert polytope_integrate( Polygon(Point(-1, -1), Point(-1, 1), Point(0, 0), Point(1, 1), Point(1, -1), Point(0, 0)), 1) == 2 # Hyperplane representation assert polytope_integrate([((-1, 0), 1), ((0, 1), 1), ((1, -1), 0), ((1, 1), 0), ((0, -1), 1)], 1) == 3 assert polytope_integrate([((-1, 0), 1), ((1, 1), 0), ((-1, 1), 0), ((1, 0), 1), ((-1, -1), 0), ((1, -1), 0)], 1) == 2 # Tests for 2D polytopes mentioned in Chin et al(Page 10): # http://dilbert.engr.ucdavis.edu/~suku/quadrature/cls-integration.pdf fig1 = Polygon(Point(1.220, -0.827), Point(-1.490, -4.503), Point(-3.766, -1.622), Point(-4.240, -0.091), Point(-3.160, 4), Point(-0.981, 4.447), Point(0.132, 4.027)) assert polytope_integrate(fig1, x**2 + x*y + y**2) ==\ S(2031627344735367)/(8*10**12) fig2 = Polygon(Point(4.561, 2.317), Point(1.491, -1.315), Point(-3.310, -3.164), Point(-4.845, -3.110), Point(-4.569, 1.867)) assert polytope_integrate(fig2, x**2 + x*y + y**2) ==\ S(517091313866043)/(16*10**11) fig3 = Polygon(Point(-2.740, -1.888), Point(-3.292, 4.233), Point(-2.723, -0.697), Point(-0.643, -3.151)) assert polytope_integrate(fig3, x**2 + x*y + y**2) ==\ S(147449361647041)/(8*10**12) fig4 = Polygon(Point(0.211, -4.622), Point(-2.684, 3.851), Point(0.468, 4.879), Point(4.630, -1.325), Point(-0.411, -1.044)) assert polytope_integrate(fig4, x**2 + x*y + y**2) ==\ S(180742845225803)/(10**12) # Tests for many polynomials with maximum degree given. tri = Polygon(Point(0, 3), Point(5, 3), Point(1, 1)) polys = [] expr1 = x**9 * y + x**7 * y**3 + 2 * x**2 * y**8 expr2 = x**6 * y**4 + x**5 * y**5 + 2 * y**10 expr3 = x**10 + x**9 * y + x**8 * y**2 + x**5 * y**5 polys.extend((expr1, expr2, expr3)) result_dict = polytope_integrate(tri, polys, max_degree=10) assert result_dict[expr1] == 615780107 / 594 assert result_dict[expr2] == 13062161 / 27 assert result_dict[expr3] == 1946257153 / 924
def test_parabola_geom(): a, b = symbols('a b') p1 = Point(0, 0) p2 = Point(3, 7) p3 = Point(0, 4) p4 = Point(6, 0) p5 = Point(a, a) d1 = Line(Point(4, 0), Point(4, 9)) d2 = Line(Point(7, 6), Point(3, 6)) d3 = Line(Point(4, 0), slope=oo) d4 = Line(Point(7, 6), slope=0) d5 = Line(Point(b, a), slope=oo) d6 = Line(Point(a, b), slope=0) half = S.Half pa1 = Parabola(None, d2) pa2 = Parabola(directrix=d1) pa3 = Parabola(p1, d1) pa4 = Parabola(p2, d2) pa5 = Parabola(p2, d4) pa6 = Parabola(p3, d2) pa7 = Parabola(p2, d1) pa8 = Parabola(p4, d1) pa9 = Parabola(p4, d3) pa10 = Parabola(p5, d5) pa11 = Parabola(p5, d6) d = Line(Point(3, 7), Point(2, 9)) pa12 = Parabola(Point(7, 8), d) pa12r = Parabola(Point(7, 8).reflect(d), d) raises(ValueError, lambda: Parabola(Point(7, 8, 9), Line(Point(6, 7), Point(7, 7)))) raises(ValueError, lambda: Parabola(Point(0, 2), Line(Point(7, 2), Point(6, 2)))) raises(ValueError, lambda: Parabola(Point(7, 8), Point(3, 8))) # Basic Stuff assert pa1.focus == Point(0, 0) assert pa1.ambient_dimension == S(2) assert pa2 == pa3 assert pa4 != pa7 assert pa6 != pa7 assert pa6.focus == Point2D(0, 4) assert pa6.focal_length == 1 assert pa6.p_parameter == -1 assert pa6.vertex == Point2D(0, 5) assert pa6.eccentricity == 1 assert pa7.focus == Point2D(3, 7) assert pa7.focal_length == half assert pa7.p_parameter == -half assert pa7.vertex == Point2D(7 * half, 7) assert pa4.focal_length == half assert pa4.p_parameter == half assert pa4.vertex == Point2D(3, 13 * half) assert pa8.focal_length == 1 assert pa8.p_parameter == 1 assert pa8.vertex == Point2D(5, 0) assert pa4.focal_length == pa5.focal_length assert pa4.p_parameter == pa5.p_parameter assert pa4.vertex == pa5.vertex assert pa4.equation() == pa5.equation() assert pa8.focal_length == pa9.focal_length assert pa8.p_parameter == pa9.p_parameter assert pa8.vertex == pa9.vertex assert pa8.equation() == pa9.equation() assert pa10.focal_length == pa11.focal_length == sqrt( (a - b)**2) / 2 # if a, b real == abs(a - b)/2 assert pa11.vertex == Point(*pa10.vertex[::-1]) == Point( a, a - sqrt( (a - b)**2) * sign(a - b) / 2) # change axis x->y, y->x on pa10 aos = pa12.axis_of_symmetry assert aos == Line(Point(7, 8), Point(5, 7)) assert pa12.directrix == Line(Point(3, 7), Point(2, 9)) assert pa12.directrix.angle_between(aos) == S.Pi / 2 assert pa12.eccentricity == 1 assert pa12.equation( x, y) == (x - 7)**2 + (y - 8)**2 - (-2 * x - y + 13)**2 / 5 assert pa12.focal_length == 9 * sqrt(5) / 10 assert pa12.focus == Point(7, 8) assert pa12.p_parameter == 9 * sqrt(5) / 10 assert pa12.vertex == Point2D(S(26) / 5, S(71) / 10) assert pa12r.focal_length == 9 * sqrt(5) / 10 assert pa12r.focus == Point(-S(1) / 5, S(22) / 5) assert pa12r.p_parameter == -9 * sqrt(5) / 10 assert pa12r.vertex == Point(S(8) / 5, S(53) / 10)
def test_parabola_intersection(): l1 = Line(Point(1, -2), Point(-1, -2)) l2 = Line(Point(1, 2), Point(-1, 2)) l3 = Line(Point(1, 0), Point(-1, 0)) p1 = Point(0, 0) p2 = Point(0, -2) p3 = Point(120, -12) parabola1 = Parabola(p1, l1) # parabola with parabola assert parabola1.intersection(parabola1) == [parabola1] assert parabola1.intersection(Parabola( p1, l2)) == [Point2D(-2, 0), Point2D(2, 0)] assert parabola1.intersection(Parabola(p2, l3)) == [Point2D(0, -1)] assert parabola1.intersection(Parabola(Point(16, 0), l1)) == [Point2D(8, 15)] assert parabola1.intersection(Parabola(Point( 0, 16), l1)) == [Point2D(-6, 8), Point2D(6, 8)] assert parabola1.intersection(Parabola(p3, l3)) == [] # parabola with point assert parabola1.intersection(p1) == [] assert parabola1.intersection(Point2D(0, -1)) == [Point2D(0, -1)] assert parabola1.intersection(Point2D(4, 3)) == [Point2D(4, 3)] # parabola with line assert parabola1.intersection(Line(Point2D(-7, 3), Point( 12, 3))) == [Point2D(-4, 3), Point2D(4, 3)] assert parabola1.intersection(Line(Point(-4, -1), Point(4, -1))) == [Point(0, -1)] assert parabola1.intersection(Line(Point(2, 0), Point(0, -2))) == [Point2D(2, 0)] raises( TypeError, lambda: parabola1.intersection(Line(Point(0, 0, 0), Point(1, 1, 1)))) # parabola with segment assert parabola1.intersection(Segment2D( (-4, -5), (4, 3))) == [Point2D(0, -1), Point2D(4, 3)] assert parabola1.intersection(Segment2D((0, -5), (0, 6))) == [Point2D(0, -1)] assert parabola1.intersection(Segment2D((-12, -65), (14, -68))) == [] # parabola with ray assert parabola1.intersection(Ray2D( (-4, -5), (4, 3))) == [Point2D(0, -1), Point2D(4, 3)] assert parabola1.intersection(Ray2D( (0, 7), (1, 14))) == [Point2D(14 + 2 * sqrt(57), 105 + 14 * sqrt(57))] assert parabola1.intersection(Ray2D((0, 7), (0, 14))) == [] # parabola with ellipse/circle assert parabola1.intersection(Circle( p1, 2)) == [Point2D(-2, 0), Point2D(2, 0)] assert parabola1.intersection(Circle( p2, 1)) == [Point2D(0, -1), Point2D(0, -1)] assert parabola1.intersection(Ellipse( p2, 2, 1)) == [Point2D(0, -1), Point2D(0, -1)] assert parabola1.intersection(Ellipse(Point(0, 19), 5, 7)) == [] assert parabola1.intersection(Ellipse((0, 3), 12, 4)) == \ [Point2D(0, -1), Point2D(0, -1), Point2D(-4*sqrt(17)/3, Rational(59, 9)), Point2D(4*sqrt(17)/3, Rational(59, 9))] # parabola with unsupported type raises(TypeError, lambda: parabola1.intersection(2))
def test_parabola_geom(): a, b = symbols('a b') p1 = Point(0, 0) p2 = Point(3, 7) p3 = Point(0, 4) p4 = Point(6, 0) p5 = Point(a, a) d1 = Line(Point(4, 0), Point(4, 9)) d2 = Line(Point(7, 6), Point(3, 6)) d3 = Line(Point(4, 0), slope=oo) d4 = Line(Point(7, 6), slope=0) d5 = Line(Point(b, a), slope=oo) d6 = Line(Point(a, b), slope=0) half = S.Half pa1 = Parabola(None, d2) pa2 = Parabola(directrix=d1) pa3 = Parabola(p1, d1) pa4 = Parabola(p2, d2) pa5 = Parabola(p2, d4) pa6 = Parabola(p3, d2) pa7 = Parabola(p2, d1) pa8 = Parabola(p4, d1) pa9 = Parabola(p4, d3) pa10 = Parabola(p5, d5) pa11 = Parabola(p5, d6) raises(ValueError, lambda: Parabola(Point(7, 8, 9), Line(Point(6, 7), Point(7, 7)))) raises(NotImplementedError, lambda: Parabola(Point(7, 8), Line(Point(3, 7), Point(2, 9)))) raises(ValueError, lambda: Parabola(Point(0, 2), Line(Point(7, 2), Point(6, 2)))) raises(ValueError, lambda: Parabola(Point(7, 8), Point(3, 8))) # Basic Stuff assert pa1.focus == Point(0, 0) assert pa1.ambient_dimension == S(2) assert pa2 == pa3 assert pa4 != pa7 assert pa6 != pa7 assert pa6.focus == Point2D(0, 4) assert pa6.focal_length == 1 assert pa6.p_parameter == -1 assert pa6.vertex == Point2D(0, 5) assert pa6.eccentricity == 1 assert pa7.focus == Point2D(3, 7) assert pa7.focal_length == half assert pa7.p_parameter == -half assert pa7.vertex == Point2D(7 * half, 7) assert pa4.focal_length == half assert pa4.p_parameter == half assert pa4.vertex == Point2D(3, 13 * half) assert pa8.focal_length == 1 assert pa8.p_parameter == 1 assert pa8.vertex == Point2D(5, 0) assert pa4.focal_length == pa5.focal_length assert pa4.p_parameter == pa5.p_parameter assert pa4.vertex == pa5.vertex assert pa4.equation() == pa5.equation() assert pa8.focal_length == pa9.focal_length assert pa8.p_parameter == pa9.p_parameter assert pa8.vertex == pa9.vertex assert pa8.equation() == pa9.equation() assert pa10.focal_length == pa11.focal_length == sqrt( (a - b)**2) / 2 # if a, b real == abs(a - b)/2 assert pa11.vertex == Point(*pa10.vertex[::-1]) == Point( a, a - sqrt( (a - b)**2) * sign(a - b) / 2) # change axis x->y, y->x on pa10
def _eval_subs(self, old, new): from sympy.geometry.point import Point if is_sequence(old) or is_sequence(new): old = Point(old) new = Point(new) return self._subs(old, new)
def intersection(self, o): """The intersection of the parabola and another geometrical entity `o`. Parameters ========== o : GeometryEntity, LinearEntity Returns ======= intersection : list of GeometryEntity objects Examples ======== >>> from sympy import Parabola, Point, Ellipse, Line, Segment >>> p1 = Point(0,0) >>> l1 = Line(Point(1, -2), Point(-1,-2)) >>> parabola1 = Parabola(p1, l1) >>> parabola1.intersection(Ellipse(Point(0, 0), 2, 5)) [Point2D(-2, 0), Point2D(2, 0)] >>> parabola1.intersection(Line(Point(-7, 3), Point(12, 3))) [Point2D(-4, 3), Point2D(4, 3)] >>> parabola1.intersection(Segment((-12, -65), (14, -68))) [] """ x, y = symbols('x y', real=True) parabola_eq = self.equation() if isinstance(o, Parabola): if o in self: return [o] else: return list( ordered([ Point(i) for i in solve([parabola_eq, o.equation()], [x, y]) ])) elif isinstance(o, Point2D): if simplify(parabola_eq.subs(([(x, o._args[0]), (y, o._args[1])]))) == 0: return [o] else: return [] elif isinstance(o, (Segment2D, Ray2D)): result = solve( [parabola_eq, Line2D(o.points[0], o.points[1]).equation()], [x, y]) return list(ordered([Point2D(i) for i in result if i in o])) elif isinstance(o, (Line2D, Ellipse)): return list( ordered([ Point2D(i) for i in solve([parabola_eq, o.equation()], [x, y]) ])) elif isinstance(o, LinearEntity3D): raise TypeError( 'Entity must be two dimensional, not three dimensional') else: raise TypeError('Wrong type of argument were put')
def test_integration_reduction(): triangle = Polygon(Point(0, 3), Point(5, 3), Point(1, 1)) facets = triangle.sides a, b = hyperplane_parameters(triangle)[0] assert integration_reduction(facets, 0, a, b, 1, (x, y), 0) == 5 assert integration_reduction(facets, 0, a, b, 0, (x, y), 0) == 0
def _eval_subs(self, old, new): if old == self.parameter: return Point(*[f.subs(old, new) for f in self.functions])
def test_is_vertex(): assert is_vertex(2) is False assert is_vertex((2, 3)) is True assert is_vertex(Point(2, 3)) is True assert is_vertex((2, 3, 4)) is True assert is_vertex((2, 3, 4, 5)) is False