Code:
|
shape = self.shape
lendiff = len(shape) - len(point)
if lendiff < 0:
raise ApiError("Point %s incompatible with %s-dimensional matrix"
% (point,len(shape)))
elif lendiff:
point = list(point) + lendiff*[0]
result = 0
factor = 1
for ii in range(len(shape)-1,0,-1): # loop over dimensions in reverse order
# skipping the first one
size = shape[ii]
num,indx = divmod(point[ii],size) # convert -n to width-n
if num not in (0,-1):
raise ApiError("Point %s out of range for dimension %s" % (point,ii))
result += factor * indx
factor *= size
# treat first dimension differently, because locations beyond the end of
# the data may be relevant and are certainly defined
indx = point[0]
if indx < 0:
indx += shape[0]
if indx < 0:
raise ApiError("Point %s out of range for dimension %s" % (point,0))
result += factor * indx
|