变换后恢复角度

2024-04-25 23:13:09 发布

您现在位置:Python中文网/ 问答频道 /正文

这似乎是一个很容易的任务,但我没有找到一个解决办法,我已经用尽的想法。你知道吗

我用两个角度来定义一些变换系数。现在,我没有实际数据中这些角度的值,我有系数,我需要恢复这些角度。你知道吗

我原以为^{}函数会处理这个问题,但有时它无法恢复正确的a1角度,而是返回其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))

Tags: theina2havea1nppirange
3条回答

你应该使用np.arctan2号(-A,B)代替np.arctan2号(-A/B,1.)。对于后者,您将丢失信息:A=-1和B=1将给出与A-1和B=-1相同的结果,因此有时180不匹配。 如果将a2限制在(0180)中,则可以恢复角度。注意,有了这个限制,a2可以恢复为acos(C)。(我试过这个,但由于我的程序是C语言的,所以可能没有帮助)

您无法还原角度符号信息,因为它在A,B计算(形成)中丢失。你知道吗

8种可能的sin/cos符号组合只给出了4种A/B符号的结果(而cos(a2)符号在这里没有帮助)。你知道吗

注意,对于球坐标,倾角范围仅为0..Pi

a2_rad等于0时,(A, B, C)等于(0, 0, 1),无论a1_rad等于什么。所以这个变换不是1比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)

你可以把a1a2看作单位球上的坐标,其中a1 表示远离x轴的角度(通常称为theta)和a2 表示远离z轴的角度(通常称为phi)。你知道吗

ABC表示笛卡尔坐标系中单位球面上的同一点。你知道吗

通常spherical coordinatesa1限制在[0, 2*pi)范围内,将a2限制在[0, pi]范围内。 即使有这个限制,北极和南极也有不止一个(实际上是无限多个)有效的表示。你知道吗

相关问题 更多 >