给定两个大的numpy数组,一个用于3D点列表,另一个用于转换矩阵列表。假设两个列表之间存在1对1的对应关系,我正在寻找最佳方法来计算由相应矩阵变换的每个点的结果数组。numpy中的大点矩阵数组乘法
我的解决方法做,这是(在下面的示例代码中看到“TEST4”),它工作得很好用小数组使用切片,但是失败,因为我的方法是如何内存浪费是:)
大型阵列import numpy as np
COUNT = 100
matrix = np.random.random_sample((3,3,)) # A single matrix
matrices = np.random.random_sample((COUNT,3,3,)) # Many matrices
point = np.random.random_sample((3,)) # A single point
points = np.random.random_sample((COUNT,3,)) # Many points
# Test 1, result of a single point multiplied by a single matrix
# This is as easy as it gets
test1 = np.dot(point,matrix)
print 'done'
# Test 2, result of a single point multiplied by many matrices
# This works well and returns a transformed point for each matrix
test2 = np.dot(point,matrices)
print 'done'
# Test 3, result of many points multiplied by a single matrix
# This works also just fine
test3 = np.dot(points,matrix)
print 'done'
# Test 4, this is the case i'm trying to solve. Assuming there's a 1-1
# correspondence between the point and matrix arrays, the result i want
# is an array of points, where each point has been transformed by it's
# corresponding matrix
test4 = np.zeros((COUNT,3))
for i in xrange(COUNT):
test4[i] = np.dot(points[i],matrices[i])
print 'done'
用一个小阵列,这工作正常。对于大数组,(COUNT = 1000000)测试#4有效,但速度相当慢。
有没有办法让Test#4更快?假定不使用循环?
@WarrenWeckesser正确的,我道歉这个明显的错误。我写了两个尖叫的孩子跑在我身边,必须有错误的副本粘贴:) – Fnord