寻找纯python NxMxZ矩阵库
我试图用Python处理任意大小的NxMxZ 3D矩阵,总共大约50MB的浮点数。我需要尽可能简单有效地计算轴和对角线上的和和和平均值,但没有什么特别的,矩阵是稠密的寻找纯python NxMxZ矩阵库,python,google-app-engine,Python,Google App Engine,我试图用Python处理任意大小的NxMxZ 3D矩阵,总共大约50MB的浮点数。我需要尽可能简单有效地计算轴和对角线上的和和和平均值,但没有什么特别的,矩阵是稠密的 有人知道有没有这样的图书馆吗?我已经找到了许多用于python的3D矩阵库,但它们都是用于3D图形的,并且仅限于4x4矩阵。通常我会使用Numpy,但我使用的是Google AppEngine,不能使用需要C扩展的库。我们只是一个支持Python 2.7的可信测试程序,其中包括Numpy。你可能想考虑报名参加。这没有道理。你不能使
有人知道有没有这样的图书馆吗?我已经找到了许多用于python的3D矩阵库,但它们都是用于3D图形的,并且仅限于4x4矩阵。通常我会使用Numpy,但我使用的是Google AppEngine,不能使用需要C扩展的库。我们只是一个支持Python 2.7的可信测试程序,其中包括Numpy。你可能想考虑报名参加。这没有道理。你不能使用C扩展吗?Python不是用C写的吗?可能的重复答案与上面的答案几乎相同,没有任何解释。请解释你的答案的要点,以及为什么它与其他答案不同。
class ndim: # from 3D array to flat array
def __init__(self,x,y,z,d):
self.dimensions=[x,y,z]
self.numdimensions=d
self.gridsize=x*y*z
def getcellindex(self, location):
cindex = 0
cdrop = self.gridsize
for index in xrange(self.numdimensions):
cdrop /= self.dimensions[index]
cindex += cdrop * location[index]
return cindex
def getlocation(self, cellindex):
res = []
for size in reversed(self.dimensions):
res.append(cellindex % size)
cellindex /= size
return res[::-1]
""" how to use ndim class
n=ndim(4,4,5,3)
print n.getcellindex((0,0,0))
print n.getcellindex((0,0,1))
print n.getcellindex((0,1,0))
print n.getcellindex((1,0,0))
print n.getlocation(20)
print n.getlocation(5)
print n.getlocation(1)
print n.getlocation(0)
"""
class ndim: # from nD array to flat array
def __init__(self,arr_dim):
self.dimensions=arr_dim
print "***dimensions***"
print self.dimensions
self.numdimensions=len(arr_dim)
print "***numdimension***"
print self.numdimensions
self.gridsize=reduce(lambda x, y: x*y, arr_dim)
print self.gridsize
def getcellindex(self, location):
cindex = 0
cdrop = self.gridsize
for index in xrange(self.numdimensions):
cdrop /= self.dimensions[index]
cindex += cdrop * location[index]
return cindex
def getlocation(self, cellindex):
res = []
for size in reversed(self.dimensions):
res.append(cellindex % size)
cellindex /= size
return res[::-1]
# how to use ndim class
arr_dim = [3,3,2,2]
n=ndim(arr_dim)
print "*****n.getcellindex((0,0,0,0))"
print n.getcellindex((0,0,0,0))
print "*****n.getcellindex((0,0,1,1))"
print n.getcellindex((0,0,1,1))
print "*****n.getcellindex((0,1,0,0))"
print n.getcellindex((0,1,0,0))
print "*****n.getcellindex((2,2,1,1))"
print n.getcellindex((2,2,1,1))
print
print "*****n.getlocation(0) "
print n.getlocation(0)
print "*****n.getlocation(3) "
print n.getlocation(3)
print "*****n.getlocation(4) "
print n.getlocation(4)
print "*****n.getlocation(35) "
print n.getlocation(35)