Python 2.7 在numpy中实现二维旋转有什么不准确的地方吗?

Python 2.7 在numpy中实现二维旋转有什么不准确的地方吗?,python-2.7,numpy,rotational-matrices,Python 2.7,Numpy,Rotational Matrices,我一直在为numpy中二维旋转的准确性挠头。实现是教科书式的,我的应用程序要求使用左手系统,因此: from numpy import sin, cos def rotate(pathx, pathy, r): """ pathx and pathy are lists of np.float64's recording x and y coordinates of points to be rotated """ c = cos(r) s =

我一直在为numpy中二维旋转的准确性挠头。实现是教科书式的,我的应用程序要求使用左手系统,因此:

from numpy import sin, cos

def rotate(pathx, pathy, r):
    """
    pathx and pathy are lists of np.float64's recording x and y 
    coordinates of points to be rotated
    """
    c = cos(r)
    s = sin(r)
    pathx = c*pathx + s*pathy
    pathy = -s*pathx + c*pathy  
为了测试这一点,我输入了
pathx=[1]
path=[1]
r=arctan2(1,1)~=pi/4~=0.78539816339744828
,预期结果是向量(1,1)在旋转
pi/4后将与x轴对齐

我得到
[1.4142135623730949,-0.29289321881345221]
。这看起来很荒谬的原因是因为我希望y坐标上的某个值更接近
0.0
。我还尝试通过使输入和sin、cos和arctan2输出都
dtype=float64
来提高性能,但没有任何区别

我犯了什么愚蠢的错误吗?还是我本该预料到的数字不稳定?我简直不敢相信错误的大小…

当你分配

pathy = -s*pathx + c*pathy  
现在使用刚刚分配的
pathx
变量,该变量已经旋转。您希望在旋转之前使用最初作为
pathx
传递给函数的数组

相应地,维护当前方法的简单方法是

def rotate(pathx, pathy, r):
    c = np.cos(r)
    s = np.sin(r)
    pathx_new = c * pathx + s * pathy
    pathy_new =-s * pathx + c * pathy
    return pathx_new, pathy_new
对于您的示例案例,返回哪一个

(array([ 1.41421356]), array([  1.11022302e-16]))

也许这会解释错误:

pathx_new =  c*pathx      + s*pathy
pathy     = -s*pathx_new  + c*pathy 
基本上,您正在更新
pathx
并根据其新值定义
path

您希望这样做,以便就地重新分配(实际比我预期的要难):

然后对于
pathx=path=1

rotate(pathxy, 0.78539816339744828)
array([ 1.41421356,  0.        ])
对于
pathxy=np.ones((3,2))


啊!就这样!看着所有错误的地方。谢谢你的帮助,让我的愚蠢的诺金酒转过来。谢谢你有趣的原地裸体成语。事实上,我可能想这样做。一般来说,pathx和pathy可能很长。
rotate(pathxy, 0.78539816339744828)
array([ 1.41421356,  0.        ])
rotate(pathxy, 0.78539816339744828)

pathxy

array([[ 1.41421356,  0.        ],
       [ 1.41421356,  0.        ],
       [ 1.41421356,  0.        ]])