首页 > 解决方案 > 循环后小数消失

问题描述

我目前正在做一些学校练习,但总是遇到麻烦。我正在围绕中心点旋转和缩放矢量集。看起来我很接近,因为我的数字真的很接近答案,但它们是四舍五入的。我试过让所有东西都浮动(通过早期和晚期确定它们,但也通过循环),但它们仍然消失。我的代码如下所示:

import numpy as np
import math

def rotateScale(coordinates, center, theta, scale):
  
    new=np.copy(coordinates)
    new[1,:]=0
    new[0,:]=0
    
 
    for n in range(len(coordinates[0,:])):
    #for n in range(1):
        for i in range(len(new[:,n])):
            new[i,n]=float(new[i,n])
            
        matrix=np.reshape([math.cos(theta),-1*math.sin(theta),math.sin(theta),math.cos(theta)],[2,2])
        matrix2=coordinates[:,n]-center
        new[:,n]=np.dot(matrix,matrix2)*scale+center
    newCoordinates=new
        
    return newCoordinates

print(rotateScale(np.array([[1,3,3,4,7,6,1],[1,1,2,3,3,5,5]]), np.array([5,2]), -math.pi/3, 2))

我的输出是这样的

[[ 0  1  2  5  8 11  6]
 [ 7  4  5  4  0  3 11]]

但它应该是这样的:

[[ -0.73205081   1.26794919   3. 5.73205081   8.73205081  11.19615242   6.19615242]
[  7.92820323   4.46410162   5.46410162   4.73205081  -0.46410162   3.26794919  11.92820323]]

有谁知道发生了什么?:)

标签: pythonpython-3.xmatrixdecimal

解决方案


推荐阅读