转换后恢复角度
Recover angles after transformation
这似乎是一项很简单的任务,但我未能找到解决方案,而且我 运行 没有想法。
我有两个角度用来定义一些变换系数。现在,我的真实数据中实际上没有这些角度的值,我有系数,我需要 恢复 角度。
我认为 arctan2
函数会解决这个问题,但有些情况下它无法恢复正确的 a1
角度,而是 returns 它的 180
补充,后来影响 a2
角度的恢复。
我哪里做错了,我怎样才能正确恢复 a1, a2
角度?
import numpy as np
# Repeat 100 times
for _ in range(100):
# Define two random angles in the range [-pi, pi]. I do not have these
# angles in my actual data, I have the A,B,C coefficients shown below.
a1, a2 = np.random.uniform(-180., 180., (2,))
# Transformation coefficients using the above angles.
# This is the data I actually have.
a1_rad, a2_rad = np.deg2rad(a1), np.deg2rad(a2) # to radians
A = - np.sin(a1_rad) * np.sin(a2_rad)
B = np.cos(a1_rad) * np.sin(a2_rad)
C = np.cos(a2_rad)
# Recover a1 using 'arctan2' (returns angle in the range [-pi, pi])
a1_recover = np.arctan2(-A / B, 1.)
# Now obtain sin(a2), used below to obtain 'a2'
sin_a2 = -A / np.sin(a1_recover)
# Recover a2 using 'arctan2', where: C = cos(a2)
a2_recover = np.arctan2(sin_a2, C)
# Print differences.
a1_recover = np.rad2deg(a1_recover)
print("a1: {:.2f} = {} - {}".format(a1 - a1_recover, a1, a1_recover))
a2_recover = np.rad2deg(a2_recover)
print("a2: {:.2f} = {} - {}\n".format(a2 - a2_recover, a2, a2_recover))
您无法恢复角标信息,因为它在A,B
计算(编队)中丢失了。
sin/cos
符号的 8 种可能组合仅给出 A/B
符号的 4 个结果(cos(a2)
符号在这里无济于事)。
请注意,球坐标的倾角范围仅为0..Pi
当a2_rad
等于0时,无论a1_rad
等于什么,(A, B, C)
都等于(0, 0, 1)
。所以转换不是一对一的。因此没有明确定义的逆。
def ABC(a1, a2):
a1_rad, a2_rad = np.deg2rad(a1), np.deg2rad(a2) # to radians
A = - np.sin(a1_rad) * np.sin(a2_rad)
B = np.cos(a1_rad) * np.sin(a2_rad)
C = np.cos(a2_rad)
return A, B, C
print(ABC(0, 0))
# (-0.0, 0.0, 1.0)
print(90, 0)
# (-0.0, 0.0, 1.0)
print(-90, 0)
# (-0.0, 0.0, 1.0)
对面(南)极也有类似的问题。在浮点精度的限制内,所有这些值(形式为 ABC(a1, 180)
)也基本相等:
ABC(1, 180)
# (-2.1373033680837913e-18, 1.2244602795081332e-16, -1.0)
ABC(0, 180)
# (-0.0, 1.2246467991473532e-16, -1.0)
ABC(90, 180)
# (-1.2246467991473532e-16, 7.498798913309288e-33, -1.0)
您可以将 a1
、a2
视为单位球体上的坐标,其中 a1
表示远离 x 轴的角度(通常称为 theta
)和 a2
表示远离z轴的角度(通常称为phi
)。
A
,B
,C
表示笛卡尔坐标系中单位球面上的同一点
通常 spherical coordinates 将 a1
限制在 [0, 2*pi)
范围内,将 a2
限制在 [0, pi]
范围内。
即使有这个限制,北极和南极也有不止一个(实际上是无限多个)有效表示。
您应该使用 np.arctan2(-A , B) 而不是 np.arctan2(-A / B, 1.)。对于后者,您正在丢失信息:A = -1 和 B = 1 将给出与 A - 1 和 B = -1 相同的结果,因此有时会出现 180 不匹配。
如果将 a2 限制在 (0,180) 内,则可以恢复角度。请注意,通过此限制,a2 可以恢复为 acos(C)。 (我已经试过了,但因为我的程序是用 C 语言编写的,所以它可能没有帮助)
这似乎是一项很简单的任务,但我未能找到解决方案,而且我 运行 没有想法。
我有两个角度用来定义一些变换系数。现在,我的真实数据中实际上没有这些角度的值,我有系数,我需要 恢复 角度。
我认为 arctan2
函数会解决这个问题,但有些情况下它无法恢复正确的 a1
角度,而是 returns 它的 180
补充,后来影响 a2
角度的恢复。
我哪里做错了,我怎样才能正确恢复 a1, a2
角度?
import numpy as np
# Repeat 100 times
for _ in range(100):
# Define two random angles in the range [-pi, pi]. I do not have these
# angles in my actual data, I have the A,B,C coefficients shown below.
a1, a2 = np.random.uniform(-180., 180., (2,))
# Transformation coefficients using the above angles.
# This is the data I actually have.
a1_rad, a2_rad = np.deg2rad(a1), np.deg2rad(a2) # to radians
A = - np.sin(a1_rad) * np.sin(a2_rad)
B = np.cos(a1_rad) * np.sin(a2_rad)
C = np.cos(a2_rad)
# Recover a1 using 'arctan2' (returns angle in the range [-pi, pi])
a1_recover = np.arctan2(-A / B, 1.)
# Now obtain sin(a2), used below to obtain 'a2'
sin_a2 = -A / np.sin(a1_recover)
# Recover a2 using 'arctan2', where: C = cos(a2)
a2_recover = np.arctan2(sin_a2, C)
# Print differences.
a1_recover = np.rad2deg(a1_recover)
print("a1: {:.2f} = {} - {}".format(a1 - a1_recover, a1, a1_recover))
a2_recover = np.rad2deg(a2_recover)
print("a2: {:.2f} = {} - {}\n".format(a2 - a2_recover, a2, a2_recover))
您无法恢复角标信息,因为它在A,B
计算(编队)中丢失了。
sin/cos
符号的 8 种可能组合仅给出 A/B
符号的 4 个结果(cos(a2)
符号在这里无济于事)。
请注意,球坐标的倾角范围仅为0..Pi
当a2_rad
等于0时,无论a1_rad
等于什么,(A, B, C)
都等于(0, 0, 1)
。所以转换不是一对一的。因此没有明确定义的逆。
def ABC(a1, a2):
a1_rad, a2_rad = np.deg2rad(a1), np.deg2rad(a2) # to radians
A = - np.sin(a1_rad) * np.sin(a2_rad)
B = np.cos(a1_rad) * np.sin(a2_rad)
C = np.cos(a2_rad)
return A, B, C
print(ABC(0, 0))
# (-0.0, 0.0, 1.0)
print(90, 0)
# (-0.0, 0.0, 1.0)
print(-90, 0)
# (-0.0, 0.0, 1.0)
对面(南)极也有类似的问题。在浮点精度的限制内,所有这些值(形式为 ABC(a1, 180)
)也基本相等:
ABC(1, 180)
# (-2.1373033680837913e-18, 1.2244602795081332e-16, -1.0)
ABC(0, 180)
# (-0.0, 1.2246467991473532e-16, -1.0)
ABC(90, 180)
# (-1.2246467991473532e-16, 7.498798913309288e-33, -1.0)
您可以将 a1
、a2
视为单位球体上的坐标,其中 a1
表示远离 x 轴的角度(通常称为 theta
)和 a2
表示远离z轴的角度(通常称为phi
)。
A
,B
,C
表示笛卡尔坐标系中单位球面上的同一点
通常 spherical coordinates 将 a1
限制在 [0, 2*pi)
范围内,将 a2
限制在 [0, pi]
范围内。
即使有这个限制,北极和南极也有不止一个(实际上是无限多个)有效表示。
您应该使用 np.arctan2(-A , B) 而不是 np.arctan2(-A / B, 1.)。对于后者,您正在丢失信息:A = -1 和 B = 1 将给出与 A - 1 和 B = -1 相同的结果,因此有时会出现 180 不匹配。 如果将 a2 限制在 (0,180) 内,则可以恢复角度。请注意,通过此限制,a2 可以恢复为 acos(C)。 (我已经试过了,但因为我的程序是用 C 语言编写的,所以它可能没有帮助)