Looking for a pure-python NxMxZ matrix library [closed]
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
开发者_开发知识库 Improve this questionI'm trying to deal with an arbitrary-sized (NxMxZ) 3D matrix in Python, about 50MB of floating point numbers in total. I need to do simple as-efficient-as-possible sum and average calculations across axes and diagonals, but nothing very fancy, and the matrix is dense.
Anyone know if such a library exists? I've found a number of "3D matrix" libraries for python, but they're all for 3D graphics, and are limited to, e.g. 4x4x4 matrices. Normally I'd use Numpy, but I'm on Google AppEngine, and can't use a library that requires C extensions.
We just announced a trusted tester program for Python 2.7 support, which includes NumPy. You might want to consider signing up for it.
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)
精彩评论