2011-08-17 65 views
-1

我想对付Python中的任意大小(NxMxZ)3D矩阵,约浮点数共50MB。我需要在轴和对角线上做简单的尽可能有效的总和和平均计算,但没有什么特别的,而且矩阵很密集。寻找一个纯Python NxMxZ矩阵库

任何人都知道这样的图书馆是否存在?我已经找到了许多用于python的“3D矩阵”库,但它们都是用于3D图形的,并且仅限于例如4x4x4矩阵。通常我会使用Numpy,但我使用的是Google AppEngine,无法使用需要C扩展名的库。

+0

这是没有意义的。你不能使用C扩展? Python不是用C编写的吗? –

+2

[Google App Engine上有哪些替代品可能会出现问题](http://stackoverflow.com/questions/5490723/what-alternatives-are-there-to-numpy-on-google-app-engine) –

回答

1

我们只是announced为Python 2.7的支持,其中包括与NumPy为信任的测试程序。您可能需要考虑注册。

1
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) 
""" 
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) 
+0

这与上面的答案几乎相同,没有任何解释。请解释您的答案的重点,以及与其他答案不同的原因。 – blackbuild