Sympy 简明教程

SymPy - Entities

SymPy 中的几何模块允许创建二维实体,如线、圆等。然后我们可以获取有关它们的信息,例如检查共线或查找交点。

The geometry module in SymPy allows creation of two dimensional entities such as line, circle, etc. We can then obtain information about them such as checking colinearity or finding intersection.

Point

Point 类表示欧氏空间中的一个点。以下示例检查点的共线性:

Point class represents a point in Euclidean space. Following example checks for collinearity of points −

>>> from sympy.geometry import Point
>>> from sympy import *
>>> x=Point(0,0)
>>> y=Point(2,2)
>>> z=Point(4,4)
>>> Point.is_collinear(x,y,z)

Output

True

True

>>> a=Point(2,3)
>>> Point.is_collinear(x,y,a)

Output

False

False

Point 类的 distance() 方法计算两个点之间的距离

The distance() method of Point class calculates distance between two points

>>> x.distance(y)

Output

$2\sqrt2$

$2\sqrt2$

距离也可以用符号来表示。

The distance may also be represented in terms of symbols.

Line

线实体是由两个点对象获得的。intersection() 方法返回两条相交线段的交点。

Line entity is obtained from two Point objects. The intersection() method returns point of intersection if two lines intersect each other.

>>> from sympy.geometry import Point, Line
>>> p1, p2=Point(0,5), Point(5,0)
>>> l1=Line(p1,p2)
>>> l2=Line(Point(0,0), Point(5,5))
>>> l1.intersection(l2)

Output

[Point2D(5/2, 5/2)]

[Point2D(5/2, 5/2)]

>>> l1.intersection(Line(Point(0,0), Point(2,2)))

Output

[Point2D(5/2, 5/2)]

[Point2D(5/2, 5/2)]

>>> x,y=symbols('x y')
>>> p=Point(x,y)
>>> p.distance(Point(0,0))

Output

$\sqrt{x^2 + y^2}$

$\sqrt{x^2 + y^2}$

Triangle

这个函数用三个点对象构建一个三角形实体。

This function builds a triangle entity from three point objects.

Triangle(a,b,c)

Triangle(a,b,c)

>>> t=Triangle(Point(0,0),Point(0,5), Point(5,0))
>>> t.area

Output

$-\frac{25}{2}$

$-\frac{25}{2}$

Ellipse

一个椭圆几何实体是由一个对应于中心点的点对象和两个数字(分别代表水平和垂直半径)构成的。

An elliptical geometry entity is constructed by passing a Point object corresponding to center and two numbers each for horizontal and vertical radius.

ellipse(center, hradius, vradius)

ellipse(center, hradius, vradius)

>>> from sympy.geometry import Ellipse, Line
>>> e=Ellipse(Point(0,0),8,3)
>>> e.area

Output

$24\pi$

$24\pi$

可以使用偏心参数间接给出垂直半径。

The vradius can be indirectly provided by using eccentricity parameter.

>>> e1=Ellipse(Point(2,2), hradius=5, eccentricity=Rational(3,4))
>>> e1.vradius

Output

$\frac{5\sqrt7}{4}$

$\frac{5\sqrt7}{4}$

椭圆的 apoapsis 是焦点与轮廓线之间的最长距离。

The apoapsis of the ellipse is the greatest distance between the focus and the contour.

>>> e1.apoapsis

Output

$\frac{35}{4}$

$\frac{35}{4}$

下述语句计算椭圆的圆周率 -

Following statement calculates circumference of ellipse −

>>> e1.circumference

Output

$20E(\frac{9}{16})$

$20E(\frac{9}{16})$

椭圆的 equation 方法返回椭圆的方程。

The equation method of ellipse returns equation of ellipse.

>>> e1.equation(x,y)

Output

$(\frac{x}{5}-\frac{2}{5})^2 + \frac{16(y-2)2}{175} - 1$

$(\frac{x}{5}-\frac{2}{5})^2 + \frac{16(y-2)2}{175} - 1$