这是一篇求三角函数的文章
我们将运用旋转角度迭代的方式来计算任意角度的三角函数。
(初中生及以上可食用)
预备知识
正文开始
证明过程
众所周知,和角公式可以通过两个角分别的 cos,sin 值求出两角之和(差)的三角函数值,公式如下:
cosα+β=cosαcosβ−sinαsinβ
sinα+β=sinαcosβ+cosαsinβ
和角公式证明
如下图,可得
x=x′cosΔα−y′sinΔα
y=y′cosΔα+x′sinΔα

提取一个 cosΔα,可得
x=cosΔα(x′−y′tanΔα)=secΔαx′−y′tanΔα=1+tan2Δαx′−y′tanΔα
y=cosΔα(y′+x′tanΔα)=secΔαy′+x′tanΔα=1+tan2Δαy′+x′tanΔα
记 n 为迭代次数,x(n)(R) 为真旋转的 cos 值,y(n)(R) 为真旋转的 sin 值,Δαn 为当前旋转角,则
x(n)(R)=1+tan2Δαnxn−1−yn−1tanΔαn
y(n)(R)=1+tan2Δαnyn−1+xn−1tanΔαn
为提高运算效率,记 x(n)(P) 为伪旋转的 cos 值,y(n)(P) 为伪旋转的 sin 值,且 x(n)(P)=xn−1−yn−1tanΔαn,y(n)(P)=yn−1+xn−1tanΔαn。
伪旋转后的情况,角度不变(即 xy 不变),向量变长,如何还原呢?
记 kn=1+tan2Δαn,则
⎩⎨⎧x(1)(P)=x(1)(R)k1=(x0cosΔα1−y0sinΔα1)k1y(1)(P)=y(1)(R)k1=(y0cosΔα1+x0sinΔα1)k1
⎩⎨⎧x(2)(P)=x(2)(R)k2y(2)(P)=y(2)(R)k2=(x(1)(P)cosΔα2−y(1)(P)sinΔα2)k2∗=[(x0cosΔα1−y0sinΔα1)k1cosΔα2−(y0cosΔα1+x0sinΔα1)k1sinΔα2]k2=[x0cosΔα1cosΔα2−y0sinΔα1cosΔα2−y0cosΔα1sinΔα2−x0sinΔα1sinΔα2]k1k2=[x0(cosΔα1cosΔα2−sinΔα1sinΔα2)−y0(sinΔα1cosΔα2−cosΔα1sinΔα2)]k1k2=[x0cos(Δα1+Δα2)−y0sin(Δα1+Δα2)]k1k2=(y(1)(P)cosΔα2+x(1)(P)sinΔα2)k2=[(y0cosΔα1+x0sinΔα1)k1cosΔα2−(x0cosΔα1−y0sinΔα1)k1sinΔα2]k2=[y0cosΔα1cosΔα2+x0sinΔα1cosΔα2+x0cosΔα1sinΔα2−y0sinΔα1sinΔα2]k1k2=[y0(cosΔα1cosΔα2−sinΔα1sinΔα2)+x0(sinΔα1cosΔα2−cosΔα1sinΔα2)]k1k2=[y0cos(Δα1+Δα2)+x0sin(Δα1+Δα2)]k1k2
∗ 此处可以将 R 改为 P 是因为旋转角不变,而和角公式只跟角度有关,跟具体的 cos 值、sin 值无关。
……
⎩⎨⎧x(n)(P)=[x0cos(i=1∑nΔαi)−y0sin(i=1∑nΔαi)]i=1∏nkiy(n)(P)=[y0cos(i=1∑nΔαi)+x0sin(i=1∑nΔαi)]i=1∏nki
记 i=1∏nki 为 Kn,则当 x0=Kn1 且 y0=0时,x(n)(P)=x(n)(R),y(n)(P)=y(n)(R),成功还原。
另一个问题,Δα 的值怎么取?
计算机中乘或除以 2 的幂次数,只需一个移位寄存器。
所以 Δαi 应为 arctan2−i。经计算,i=1∑n→∞arctan2−i≈99.8829°,是收敛的,能覆盖 0∼2π 之内的所有角度,最后通过诱导公式扩展到所有角度即可。
代码示例
Pythonfrom typing import Union
from math import atan, pi, sqrt
gen_num = 100 # 迭代次数,可改动(越大越精确,时间也越久)
atan2 = [atan(2 ** -i) for i in range(gen_num)] # Delta_alpha数组
atan2 = [0] + atan2 # 索引变成从1开始
K = 1
for i in range(gen_num):
K *= sqrt(1 + 2 ** (-2 * i)) # K[n]
def cordic(angle: Union[int, float], decimal_digits: int = 6) -> (float, float):
# angle:以弧度为单位,decimal_digits:精确的小数位数,返回值为(cos, sin)
angle %= 2 * pi # 转换到[0, 360)°之间
status = 0
# 转换到[0, 90)°间,status保留原有状态
if angle < pi / 2:
pass
elif pi / 2 <= angle < pi:
angle = pi - angle
status = 1
elif angle < 3 * pi / 2:
angle = angle - pi
status = 2
else:
angle = 2 * pi - angle
status = 3
# Python只有16位小数,如果大于10位可能会有精确度问题
assert 2 <= decimal_digits <= 10, 'decimal_digits should be between 2 and 10'
x = [0.0] * (gen_num + 1) # -> cos angle
y = [0.0] * (gen_num + 1) # -> sin angle
z = [0.0] * (gen_num + 1) # angle与当前迭代的角度之差 -> 0
d = [1] * (gen_num + 1) # z为正(非负)是1,z为负是-1
x[0] = 1 / K
y[0] = 0.0
z[0] = angle
for i in range(1, gen_num + 1):
d[i - 1] = 1 if z[i - 1] >= 0 else -1
x[i] = x[i - 1] - y[i - 1] * d[i - 1] * 2 ** -(i - 1)
# x(P)[n] = x(P)[n - 1] - y(P)[n - 1] * tan(Delta_alpha[n])
y[i] = y[i - 1] + x[i - 1] * d[i - 1] * 2 ** -(i - 1)
# y(P)[n] = y(P)[n - 1] + x(P)[n - 1] * tan(Delta_alpha[n])
z[i] = z[i - 1] - d[i - 1] * atan2[i]
if status == 1:
x[-1] = -x[-1]
elif status == 3:
y[-1] = -y[-1]
elif status == 2:
x[-1] = -x[-1]
y[-1] = -y[-1]
if x[-1] == -0.0:
x[-1] = 0.0
if y[-1] == -0.0:
y[-1] = 0.0
# 特判angle == pi / 2或angle == 3 * pi / 2
tan_ans = round(y[-1] / x[-1], decimal_digits) if angle != pi / 2 else float('inf')
cos_ans = round(x[-1], decimal_digits)
sin_ans = round(y[-1], decimal_digits)
# 修正小数错误
if cos_ans == -0.0:
cos_ans = 0.0
if sin_ans == -0.0:
sin_ans = 0.0
if tan_ans == -0.0:
tan_ans = 0.0
return cos_ans, sin_ans, tan_ans
angle = float(input()) * pi / 180 # 输入(单位为度)并转为弧度
x, y, tangent = cordic(angle)
print(x, y, tangent) # cos, sin, tan
CORDIC 的优点
相比于泰勒展开来说,CORDIC 算法预处理了 gen_num 个 arctan 值,做了一份 gen_num 个数据的表,而可以计算精度很高的三角函数值。当计算(调用函数)的次数很大时,CORDIC 的效率将远远超过泰勒展开。
有条件的话,可以将 atan2 这份表存下来,这样便无需再预处理了。
完结撒花!!!