python计算三角函数_Python标准库——数学运算
三角函数三角函数将三角形中的角与其边长相关联。在有周期性质的公式中经常出现三角函数,如谐波或圆周运动;在处理角时也会经常用到三角函数。标准库中所有三角函数的角参数都被表示为孤度。给定一个直角三角形中的角,其正弦是对边长度与斜边长度之比(sinA=对边/斜边)。余弦是邻边长度与斜边长度之比(cosA=邻边/斜边)。正切是对边与邻边之比(tanA=对边/邻边)。新建math_trig.py文件。imp
三角函数
三角函数将三角形中的角与其边长相关联。在有周期性质的公式中经常出现三角函数,如谐波或圆周运动;在处理角时也会经常用到三角函数。标准库中所有三角函数的角参数都被表示为孤度。
给定一个直角三角形中的角,其正弦是对边长度与斜边长度之比(sinA=对边/斜边)。余弦是邻边长度与斜边长度之比(cosA=邻边/斜边)。正切是对边与邻边之比(tanA=对边/邻边)。
新建math_trig.py文件。
import math
print('{:^7} {:^7} {:^7} {:^7} {:^7}'.format(
'Degrees', 'Radians', 'Sine', 'Cosine', 'Tangent'))
print('{:-^7} {:-^7} {:-^7} {:-^7} {:-^7}'.format(
'-', '-', '-', '-', '-'))
fmt = '{:7.2f} {:7.2f} {:7.2f} {:7.2f} {:7.2f}'
for deg in range(0, 361, 30):
rad = math.radians(deg)
if deg in (90, 270):
t = float('inf')
else:
t = math.tan(rad)
print(fmt.format(deg, rad, math.sin(rad), math.cos(rad), t))
以上代码输出结果为:
Degrees Radians Sine Cosine Tangent
------- ------- ------- ------- -------
0.00 0.00 0.00 1.00 0.00
30.00 0.52 0.50 0.87 0.58
60.00 1.05 0.87 0.50 1.73
90.00 1.57 1.00 0.00 inf
120.00 2.09 0.87 -0.50 -1.73
150.00 2.62 0.50 -0.87 -0.58
180.00 3.14 0.00 -1.00 -0.00
210.00 3.67 -0.50 -0.87 0.58
240.00 4.19 -0.87 -0.50 1.73
270.00 4.71 -1.00 -0.00 inf
300.00 5.24 -0.87 0.50 -1.73
330.00 5.76 -0.50 0.87 -0.58
360.00 6.28 -0.00 1.00 -0.00
以上代码,正切也可以被已定义为角的正弦值与其余弦值之比,因为弧度π/2和π/3的余弦是0,所以相应的正切值为无穷大。
给定一个点(x, y),点[(0, 0), (x, 0), (x, y)]构成的三角形中斜边的长度为(x 2 + y 2) ** 1/2,可以用hypot()来计算。
新建math_hypot.py文件。
import math
print('{:^7} {:^7} {:^10}'.format('X', 'Y', 'Hypotenuse'))
print('{:-^7} {:-^7} {:-^10}'.format('', '', ''))
POINTS = [
(1, 1),
(-1, -1),
(math.sqrt(2), math.sqrt(2)),
(3, 4),
(math.sqrt(2) / 2, math.sqrt(2) / 2),
(0.5, math.sqrt(3) / 2),
]
for x, y in POINTS:
h = math.hypot(x, y)
print('{:7.2f} {:7.2f} {:7.2f}'.format(x, y, h))
以上代码输出结果为:
X Y Hypotenuse
------- ------- ----------
1.00 1.00 1.41
-1.00 -1.00 1.41
1.41 1.41 2.00
3.00 4.00 5.00
0.71 0.71 1.00
0.50 0.87 1.00
以上代码,对于圆上的点,其斜边总是等于1 。
还可以用这个函数查看两点之间的距离。
新建math_distance_2_points.py文件。
import math
print('{:^8} {:^8} {:^8} {:^8} {:^8}'.format(
'X1', 'Y1', 'X2', 'Y2', 'Distance',
))
print('{:-^8} {:-^8} {:-^8} {:-^8} {:-^8}'.format(
'', '', '', '', '',
))
POINTS = [
((5, 5), (6, 6)),
((-6, -6), (-5, -5)),
((0, 0), (3, 4)),
((-1, -1), (2, 3)),
]
for (x1, y1), (x2, y2) in POINTS:
x = x1 - x2
y = y1 - y2
h = math.hypot(x, y)
print('{:8.2f} {:8.2f} {:8.2f} {:8.2f} {:8.2f}'.format(
x1, y1, x2, y2, h,
))
以上代码输出结果为:
X1 Y1 X2 Y2 Distance
-------- -------- -------- -------- --------
5.00 5.00 6.00 6.00 1.41
-6.00 -6.00 -5.00 -5.00 1.41
0.00 0.00 3.00 4.00 5.00
-1.00 -1.00 2.00 3.00 5.00
以上代码,使用x值之差和y值之差将一个端点移动至原点,然后将结果传入hypot()。
math还定义了反三角函数。
新建math_inverse_trig.py文件。
import math
for r in [0, 0.5, 1]:
print('arcsine({:.1f}) = {:5.2f}'.format(r, math.asin(r)))
print('arccosine({:.1f}) = {:5.2f}'.format(r, math.acos(r)))
print('arctangent({:.1f}) = {:5.2f}'.format(r, math.atan(r)))
print()
以上代码输出结果为:
arcsine(0.0) = 0.00
arccosine(0.0) = 1.57
arctangent(0.0) = 0.00
arcsine(0.5) = 0.52
arccosine(0.5) = 1.05
arctangent(0.5) = 0.46
arcsine(1.0) = 1.57
arccosine(1.0) = 0.00
arctangent(1.0) = 0.79
以上代码,1.57大约等于 π/2,或90度,这个角的正弦为1,余弦为0 。

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐
所有评论(0)