diff options
author | Travis Oliphant <oliphant@enthought.com> | 2005-10-10 21:55:29 +0000 |
---|---|---|
committer | Travis Oliphant <oliphant@enthought.com> | 2005-10-10 21:55:29 +0000 |
commit | 47d169b1c339c6096cbb2c1a64a88818dff63eeb (patch) | |
tree | b333f4e3626842564c2c62d7623f03365b9af820 /scipy/base/index_tricks.py | |
parent | 652a657a1faebe63035da6c65b88170a929a8877 (diff) | |
download | numpy-47d169b1c339c6096cbb2c1a64a88818dff63eeb.tar.gz |
Added ndenumerate. Some print statements for debugging on 64-bit
Diffstat (limited to 'scipy/base/index_tricks.py')
-rw-r--r-- | scipy/base/index_tricks.py | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/scipy/base/index_tricks.py b/scipy/base/index_tricks.py index 101472777..8a0b9df51 100644 --- a/scipy/base/index_tricks.py +++ b/scipy/base/index_tricks.py @@ -3,7 +3,7 @@ import types import numeric as _nx from numeric import asarray -__all__ = ['mgrid','ogrid','r_', 'c_', 'index_exp', 'ix_'] +__all__ = ['mgrid','ogrid','r_', 'c_', 'index_exp', 'ix_','ndenumerate'] from type_check import ScalarType import function_base @@ -216,6 +216,33 @@ c_=concatenator(-1) #row = concatenator(0,1) #col = concatenator(-1,1) + +# A simple nd index iterator over an array: + +class ndenumerate(enumerate): + def __init__(self, arr): + arr = asarray(arr) + self.iter = enumerate(arr.flat) + self.ashape = arr.shape + self.nd = arr.ndim + self.factors = [None]*(self.nd-1) + val = self.ashape[-1] + for i in range(self.nd-1,0,-1): + self.factors[i-1] = val + val *= self.ashape[i-1] + + def next(self): + res = self.iter.next() + indxs = [None]*self.nd + val = res[0] + for i in range(self.nd-1): + indxs[i] = val / self.factors[i] + val = val % self.factors[i] + indxs[self.nd-1] = val + return tuple(indxs), res[1] + + + # A nicer way to build up index tuples for arrays. # # You can do all this with slice() plus a few special objects, |