填充时Numpy不正确的点积

时间:2016-07-16 07:43:41

标签: numpy

我遇到了numpy dot产品的问题 - 旋转矩阵产品一个向量。请参阅代码。这两行应该给出相同的结果!我存储的位置计算结果不应影响计算。 Y应与y1和y2相同。

import numpy
rotMat = numpy.array([[-0.27514947,  0.40168313,  0.87346633], [ 0.87346633, -0.27514947,  0.40168313], [ 0.40168313,  0.87346633, -0.27514947]])
print "Rotation matrix:"
print rotMat
x = [1, 0, 1, 1, 1, 0]
X = numpy.array(x)
X.shape = (2, 3)
Y = numpy.array(6*[0])
Y.shape = (2, 3)
print "X", X
print "Y initialised:", Y
Y[0, :] = numpy.dot(rotMat, X[0, :])
Y[1, :] = numpy.dot(rotMat, X[1, :])
print "Filling Y initialised, Y=", Y
print "not filling Y initialised:"
print "y1", numpy.dot(rotMat, X[0, :])
print "y2", numpy.dot(rotMat, X[1, :])

结果如下:

Rotation matrix: [[-0.27514947  0.40168313  0.87346633]
 [ 0.87346633 -0.27514947  0.40168313]
 [ 0.40168313  0.87346633 -0.27514947]]
X [[1 0 1]
 [1 1 0]]
Y initialised: [[0 0 0]
 [0 0 0]]
Filling Y initialised, Y= [[0 1 0]
 [0 0 1]]
not filling Y initialised:
y1 [ 0.59831686  1.27514946  0.12653366]
y2 [ 0.12653366  0.59831686  1.27514946]

我正在使用Python 2.7.11,使用Numpy 1.10.1

1 个答案:

答案 0 :(得分:1)

您正在填写类型为Y的{​​{1}},因此会将放入其中的所有值转换为int。初始化int时,初始化为Y

float

per @hpaulj的评论:

Y = numpy.array(6*[0], float)

也可以,因为默认Y = numpy.zeros((6,)) dtype