Utils¶
- sympy.geometry.util.intersection(*entities, pairwise=False, **kwargs)[source]¶
The intersection of a collection of GeometryEntity instances.
- Parameters
entities : sequence of GeometryEntity
pairwise (keyword argument) : Can be either True or False
- Returns
intersection : list of GeometryEntity
- Raises
NotImplementedError
When unable to calculate intersection.
Notes
The intersection of any geometrical entity with itself should return a list with one item: the entity in question. An intersection requires two or more entities. If only a single entity is given then the function will return an empty list. It is possible for \(intersection\) to miss intersections that one knows exists because the required quantities were not fully simplified internally. Reals should be converted to Rationals, e.g. Rational(str(real_num)) or else failures due to floating point issues may result.
Case 1: When the keyword argument ‘pairwise’ is False (default value): In this case, the function returns a list of intersections common to all entities.
Case 2: When the keyword argument ‘pairwise’ is True: In this case, the functions returns a list intersections that occur between any pair of entities.
Examples
>>> from sympy import Ray, Circle, intersection >>> c = Circle((0, 1), 1) >>> intersection(c, c.center) [] >>> right = Ray((0, 0), (1, 0)) >>> up = Ray((0, 0), (0, 1)) >>> intersection(c, right, up) [Point2D(0, 0)] >>> intersection(c, right, up, pairwise=True) [Point2D(0, 0), Point2D(0, 2)] >>> left = Ray((1, 0), (0, 0)) >>> intersection(right, left) [Segment2D(Point2D(0, 0), Point2D(1, 0))]
- sympy.geometry.util.convex_hull(*args, polygon=True)[source]¶
The convex hull surrounding the Points contained in the list of entities.
- Parameters
args : a collection of Points, Segments and/or Polygons
- Returns
convex_hull : Polygon if
polygon
is True else as a tuple \((U, L)\) whereL
andU
are the lower and upper hulls, respectively.
Optional Parameters
- polygonBoolean. If True, returns a Polygon, if false a tuple, see below.
Default is True.
Notes
This can only be performed on a set of points whose coordinates can be ordered on the number line.
Examples
>>> from sympy import convex_hull >>> points = [(1, 1), (1, 2), (3, 1), (-5, 2), (15, 4)] >>> convex_hull(*points) Polygon(Point2D(-5, 2), Point2D(1, 1), Point2D(3, 1), Point2D(15, 4)) >>> convex_hull(*points, **dict(polygon=False)) ([Point2D(-5, 2), Point2D(15, 4)], [Point2D(-5, 2), Point2D(1, 1), Point2D(3, 1), Point2D(15, 4)])
References
- R458
- R459
Andrew’s Monotone Chain Algorithm (A.M. Andrew, “Another Efficient Algorithm for Convex Hulls in Two Dimensions”, 1979) http://geomalgorithms.com/a10-_hull-1.html
- sympy.geometry.util.are_similar(e1, e2)[source]¶
Are two geometrical entities similar.
Can one geometrical entity be uniformly scaled to the other?
- Parameters
e1 : GeometryEntity
e2 : GeometryEntity
- Returns
are_similar : boolean
- Raises
GeometryError
When \(e1\) and \(e2\) cannot be compared.
Notes
If the two objects are equal then they are similar.
Examples
>>> from sympy import Point, Circle, Triangle, are_similar >>> c1, c2 = Circle(Point(0, 0), 4), Circle(Point(1, 4), 3) >>> t1 = Triangle(Point(0, 0), Point(1, 0), Point(0, 1)) >>> t2 = Triangle(Point(0, 0), Point(2, 0), Point(0, 2)) >>> t3 = Triangle(Point(0, 0), Point(3, 0), Point(0, 1)) >>> are_similar(t1, t2) True >>> are_similar(t1, t3) False
- sympy.geometry.util.centroid(*args)[source]¶
Find the centroid (center of mass) of the collection containing only Points, Segments or Polygons. The centroid is the weighted average of the individual centroid where the weights are the lengths (of segments) or areas (of polygons). Overlapping regions will add to the weight of that region.
If there are no objects (or a mixture of objects) then None is returned.
Examples
>>> from sympy import Point, Segment, Polygon >>> from sympy.geometry.util import centroid >>> p = Polygon((0, 0), (10, 0), (10, 10)) >>> q = p.translate(0, 20) >>> p.centroid, q.centroid (Point2D(20/3, 10/3), Point2D(20/3, 70/3)) >>> centroid(p, q) Point2D(20/3, 40/3) >>> p, q = Segment((0, 0), (2, 0)), Segment((0, 0), (2, 2)) >>> centroid(p, q) Point2D(1, 2 - sqrt(2)) >>> centroid(Point(0, 0), Point(2, 0)) Point2D(1, 0)
Stacking 3 polygons on top of each other effectively triples the weight of that polygon:
>>> p = Polygon((0, 0), (1, 0), (1, 1), (0, 1)) >>> q = Polygon((1, 0), (3, 0), (3, 1), (1, 1)) >>> centroid(p, q) Point2D(3/2, 1/2) >>> centroid(p, p, p, q) # centroid x-coord shifts left Point2D(11/10, 1/2)
Stacking the squares vertically above and below p has the same effect:
>>> centroid(p, p.translate(0, 1), p.translate(0, -1), q) Point2D(11/10, 1/2)
- sympy.geometry.util.idiff(eq, y, x, n=1)[source]¶
Return
dy/dx
assuming thateq == 0
.- Parameters
y : the dependent variable or a list of dependent variables (with y first)
x : the variable that the derivative is being taken with respect to
n : the order of the derivative (default is 1)
Examples
>>> from sympy.abc import x, y, a >>> from sympy.geometry.util import idiff
>>> circ = x**2 + y**2 - 4 >>> idiff(circ, y, x) -x/y >>> idiff(circ, y, x, 2).simplify() (-x**2 - y**2)/y**3
Here,
a
is assumed to be independent ofx
:>>> idiff(x + a + y, y, x) -1
Now the x-dependence of
a
is made explicit by listinga
aftery
in a list.>>> idiff(x + a + y, [y, a], x) -Derivative(a, x) - 1
See also
sympy.core.function.Derivative
represents unevaluated derivatives
sympy.core.function.diff
explicitly differentiates wrt symbols