【发布时间】:2020-07-11 14:39:13
【问题描述】:
我正在尝试在 Python 中实现 arcsin,而不使用任何外部库。
这是我的代码:
from time import process_time as pt
class TrigoCalc(metaclass=__readonly):
# This class evaluates various Trigonometric functions
# including Inverse Trigonometric functions
def __setattr__(self, name, value):
raise Exception("Value can't be changed")
@staticmethod
def asin(x):
'''Implementation from Taylor series
asin(x) => summation[(2k)! * x^(2k + 1) / (2^(2k) * (k!)^2 * (2k + 1))]
k = [0, inf)
x should be real
'''
# a0 = 1
# a1 = 1/(2*3)
# a2 = 1/2 * 3/(4*5)
# a3 = 1/2 * 3/4 * 5/(6*7)
# a4 = 1/2 * 3/4 * 5/6 * 7/(8*9)
# a5 = 1/2 * 3/4 * 5/6 * 7/8 * 9/(10*11)
# a6 = 1/2 * 3/4 * 5/6 * 7/8 * 9/10 * 11/(12*13)
# a7 = 1/2 * 3/4 * 5/6 * 7/8 * 9/10 * 11/12 * 13/(14*15)
# a8 = 1/2 * 3/4 * 5/6 * 7/8 * 9/10 * 11/12 * 13/14 * 15/(16*17)
# a9 = 1/2 * 3/4 * 5/6 * 7/8 * 9/10 * 11/12 * 13/14 * 15/16 * 17/(18*19)
# a10 = 1/2 * 3/4 * 5/6 * 7/8 * 9/10 * 11/12 * 13/14 * 15/16 * 17/18 * 19/(20*21)
# taking 10 coefficients for arriving at a common sequence
# N = n, D = n + 1; (N/D) --> Multiplication, number of times the coefficient number, n >= 1
start_time = pt()
coeff_list = []
NUM_ITER = 10000
for k in range(NUM_ITER):
if k == 0:
coeff_list.append(1)
else:
N = 1
D = N + 1
C = N/D
if k >= 2:
for i in range(k-1):
N += 2; D += 2
C = C * N/D
coeff_list.append(C)
__sum = 0
for k in range(NUM_ITER):
n = coeff_list[k] * math_utils.power(x, 2*k + 1) / (2*k + 1)
__sum += n
# Radian conversion to degrees
__sum = __sum/TrigoCalc.pi * 180
end_time = pt()
print(f'Execution time: {end_time - start_time} seconds')
return __sum
结果
当NUM_ITER 为60(无限级数迭代60 次)时,x = 1 极点处的计算存在明显的不准确性,而x = 1/2 给出了 14 点的精度。
In [2]: TrigoCalc.asin(0.5)
Execution time: 0.0 seconds
Out[2]: 30.000000000000007
In [3]: TrigoCalc.asin(1)
Execution time: 0.0 seconds
Out[3]: 85.823908877692
两次运行的执行时间都不明显。
当NUM_ITER 为10000 时,在x = 1 极点处,结果比上次运行更准确,但在x = 1/2 处,精度完全相同。
In [4]: TrigoCalc.asin(0.5)
Execution time: 19.109375 seconds
Out[4]: 30.000000000000007
In [5]: TrigoCalc.asin(1)
Execution time: 19.109375 seconds
Out[5]: 89.67674183336727
对于这种类型的计算,这 2 次运行的执行时间非常长。
问题
如何平衡代码,使其在较小的NUM_ITER 极点x = 1 处至少提供 1 点精度?
请随时对代码提出建议或更新。
Python 版本: 3.7.7
编辑:在@Joni 的回答的帮助下更改代码以获得精确的结果
-
将无限级数计算封装到
asin()内的另一个函数中:def asin(x): def __arcsin_calc(x): # .... # Computations # .... # Removing the radian to degree conversion from this function return __sum -
使用
asin()中的新函数将限制添加到x以避免收敛缓慢:if -1.0 <= x < -0.5: return -(TrigoCalc.pi/2 - __arcsin_calc(math_utils.power((1 - x*x), 0.5))) / TrigoCalc.pi * 180 # Radian to Degree conversion elif -0.5 <= x <= 0.5: return __arcsin_calc(x)/TrigoCalc.pi * 180 elif 0.5 < x <= 1.0: return (TrigoCalc.pi/2 - __arcsin_calc(math_utils.power((1 - x*x), 0.5))) / TrigoCalc.pi * 180 else: raise ValueError("x should be in range of [-1, 1]") -
结果:
In [2]: TrigoCalc.asin(0.99) Execution time: 0.0 seconds Out[2]: 81.89022502527023 In [3]: math.asin(0.99)/TrigoCalc.pi*180 Out[3]: 81.89038554400582 In [4]: TrigoCalc.asin(1) Execution time: 0.0 seconds Out[4]: 90.0 In [5]: math.asin(1)/TrigoCalc.pi*180 Out[5]: 90.0
【问题讨论】:
标签: python python-3.x math trigonometry