2011-08-17 48 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