寻找纯python NxMxZ矩阵库

时间:2011-08-17 15:53:58

标签: python google-app-engine

我正在尝试用Python处理任意大小的(NxMxZ)3D矩阵,总共大约50MB的浮点数。我需要对轴和对角线进行简单的尽可能有效的求和和平均计算,但没有什么特别的,矩阵是密集的。

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

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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)
"""

答案 2 :(得分: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)