# Utils¶

diofant.geometry.util.idiff(eq, y, x, n=1)[source]

Return dy/dx assuming that eq == 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 diofant.abc import a

>>> 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 of x:

>>> idiff(x + a + y, y, x)
-1


Now the x-dependence of a is made explicit by listing a after y in a list.

>>> idiff(x + a + y, [y, a], x)
-Derivative(a, x) - 1


See also

diofant.core.function.Derivative()

represents unevaluated derivatives

diofant.core.function.diff()

explicitly differentiates wrt symbols

diofant.geometry.util.intersection(*entities)[source]

The intersection of a collection of GeometryEntity instances.

Parameters

entities (sequence of GeometryEntity)

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.

Examples

>>> p1, p2, p3 = Point(0, 0), Point(1, 1), Point(-1, 5)
>>> l1, l2 = Line(p1, p2), Line(p3, p2)
>>> c = Circle(p2, 1)
>>> intersection(l1, p2)
[Point2D(1, 1)]
>>> intersection(l1, l2)
[Point2D(1, 1)]
>>> intersection(c, p2)
[]
>>> intersection(c, Point(1, 0))
[Point2D(1, 0)]
>>> intersection(c, l2)
[Point2D(-sqrt(5)/5 + 1, 2*sqrt(5)/5 + 1),
Point2D(sqrt(5)/5 + 1, -2*sqrt(5)/5 + 1)]

diofant.geometry.util.convex_hull(*args)[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)

Notes

This can only be performed on a set of non-symbolic points.

References

 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

Examples

>>> 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))

diofant.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

diofant.geometry.exceptions.GeometryError – When $$e1$$ and $$e2$$ cannot be compared.

Notes

If the two objects are equal then they are similar.

Examples

>>> 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

diofant.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

>>> 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, -sqrt(2) + 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)


Geometry Errors.

exception diofant.geometry.exceptions.GeometryError[source]

An exception raised by classes in the geometry module.