📜  SymPy 中的实体是什么?

📅  最后修改于: 2022-05-13 01:55:15.947000             🧑  作者: Mango

SymPy 中的实体是什么?

SymPy中的几何模块是所有几何实体Python的基础类,允许您创建二维对象,如线和圆、多边形等。然后我们可以通过寻找共线或检测交叉点来了解更多信息。任何具有特定几何特性的对象都称为 GeometryEntity。

所有几何实体都继承自这个基本类。此类不代表任何特定的几何实体;相反,它实现了几个由所有子类共享的方法。

积分

位置是几何中的一个点。它没有尺寸,即没有宽度、长度或深度。一个点代表一个点。共线性是几何中位于一条直线上的一组点的属性。共线性是指一组具有这种性质的点。 Point()函数用于在空间中创建一个点。 Point 类包含 distance() 方法来查找两点之间的距离。

Python3
# import packages
from sympy.geometry import Point
 
# create points
x = Point(1, 1)
y = Point(2, 2)
z = Point(3, 3)
w = Point(5, 2)
 
# checking if points are collinear.
print(Point.is_collinear(x, y, z))
print(Point.is_collinear(y, z, w))
 
# calculating distance between two points
print('Distance between x and y points is ' + str(x.distance(y)))


Python3
# importing packages
from sympy.geometry import Point
from sympy.abc import a, b
 
# defining a point
p = Point(a, b)
 
# distance of the point from the origin
print(p.distance(Point(0, 0)))


Python3
# importing packages
from sympy.geometry import Point, Line
 
# creating two points
p1, p2 = Point(1, 2), Point(2, 0)
line1 = Line(p1, p2)
print(line1)
 
# creating two points
line2 = Line(Point(2, 4), Point(6, 2))
print(line2)
 
# intersection point of two lines
print(line1.intersection(line2))
 
# Angle between the two lines
print('Angle between two lines is : \
' + str(line1.angle_between(line2)))


Python3
# importing packages
from sympy.geometry import Point, Triangle
 
# constructing a triangle with three points
triangle = Triangle(Point(0, 0), Point(3, 0), Point(3, 3))
 
# area of the triangle
print('area of the triangle is : '+str(triangle.area))


Python3
# importing packages
from sympy import RegularPolygon, Point
 
fig  = RegularPolygon(Point(0, 0), 1, 3)
 
# area of the regular polygon
print(fig.area)


Python3
# importing packages
from sympy import Circle, Point
 
fig = Circle(Point(0, 0), 3)
 
# area of the regular polygon
print(fig.area)


Python3
# importing packages
from sympy.geometry import Ellipse, Point
from sympy.abc import x, y
 
# ellipse
ellipse = Ellipse(Point(0, 0), 5, 8)
 
# area of ellipse
print('area of the ellipse is : '+str(ellipse.area))
 
# equation of ellipse
print('equation of the ellipse is : ')
print(ellipse.equation(x, y))
 
# circumference of ellipse
print('circumference of the ellipse is : \
'+str(ellipse.circumference))


输出:

True
False
Distance between x and y points is sqrt(2)

点到原点的距离公式:

Python3

# importing packages
from sympy.geometry import Point
from sympy.abc import a, b
 
# defining a point
p = Point(a, b)
 
# distance of the point from the origin
print(p.distance(Point(0, 0)))

输出:

sqrt(a**2 + b**2)

线

一条线被定义为在两个方向上无限延伸的一组点。它只有一个维度,即长度。 Line() 是在两点的帮助下创建的。 intersection() 方法用于查找两条线之间的交点。 angle_between()函数用于查找两条线之间的角度。

Python3

# importing packages
from sympy.geometry import Point, Line
 
# creating two points
p1, p2 = Point(1, 2), Point(2, 0)
line1 = Line(p1, p2)
print(line1)
 
# creating two points
line2 = Line(Point(2, 4), Point(6, 2))
print(line2)
 
# intersection point of two lines
print(line1.intersection(line2))
 
# Angle between the two lines
print('Angle between two lines is : \
' + str(line1.angle_between(line2)))

输出:

Line2D(Point2D(1, 2), Point2D(2, 0))
Line2D(Point2D(2, 4), Point2D(6, 2))
[Point2D(-2/3, 16/3)]
Angle between two lines is : acos(4/5)

三角形

三角形是具有三个顶点和三个边的三边多边形。它是最基本的几何形式之一。三角形是在三个点或顶点的帮助下形成的。 .area 属性用于查找三角形的面积。

Python3

# importing packages
from sympy.geometry import Point, Triangle
 
# constructing a triangle with three points
triangle = Triangle(Point(0, 0), Point(3, 0), Point(3, 3))
 
# area of the triangle
print('area of the triangle is : '+str(triangle.area))

输出:

area of the triangle is : 9/2

多边形

几何类中的RegularPolygon() 方法用于构造RegularPolygon。它采用以下参数:

Python3

# importing packages
from sympy import RegularPolygon, Point
 
fig  = RegularPolygon(Point(0, 0), 1, 3)
 
# area of the regular polygon
print(fig.area)

输出:

3*sqrt(3)/4

圆圈

圆是由一个点在平面上移动所画出的曲线,因此它与给定点的距离是恒定的;或者,它是由平面中与给定点(中心)相距一定距离的所有点形成的形状。

Circle() 方法以一个点为中心和其他参数如半径并构造一个圆。面积属性用于查找圆的面积。

Python3

# importing packages
from sympy import Circle, Point
 
fig = Circle(Point(0, 0), 3)
 
# area of the regular polygon
print(fig.area)

输出:

9*pi

椭圆

椭圆是由点组成的闭合曲线,这些点与两个固定点(焦点)的距离加起来都相同。中心是焦点在中间相遇的位置。椭圆的特性是从一个焦点反射到其边界的线将穿过另一个焦点。

椭圆方程可以用 equation() 方法构造,它以符号作为输入。 .area 属性用于显示圆的面积,.circumference 属性用于查找圆的周长。

Python3

# importing packages
from sympy.geometry import Ellipse, Point
from sympy.abc import x, y
 
# ellipse
ellipse = Ellipse(Point(0, 0), 5, 8)
 
# area of ellipse
print('area of the ellipse is : '+str(ellipse.area))
 
# equation of ellipse
print('equation of the ellipse is : ')
print(ellipse.equation(x, y))
 
# circumference of ellipse
print('circumference of the ellipse is : \
'+str(ellipse.circumference))

输出:

area of the ellipse is : 40*pi
equation of the ellipse is : 
x**2/25 + y**2/64 - 1
circumference of the ellipse is : 32*elliptic_e(39/64)