8000 1.7.x fix: revert patches by certik · Pull Request #463 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

1.7.x fix: revert patches #463

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 30, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions numpy/lib/index_tricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import numpy.matrixlib as matrix
from function_base import diff
from numpy.lib._compiled_base import ravel_multi_index, unravel_index
from numpy.lib.stride_tricks import as_strided
makemat = matrix.matrix

def ix_(*args):
Expand Down Expand Up @@ -532,20 +531,37 @@ class ndindex(object):
(2, 1, 0)

"""
def __init__(self, *shape):
x = as_strided(_nx.zeros(1), shape=shape, strides=_nx.zeros_like(shape))
self._it = _nx.nditer(x, flags=['multi_index'], order='C')

def __iter__(self):
return self
def __init__(self, *args):
if len(args) == 1 and isinstance(args[0], tuple):
args = args[0]
self.nd = len(args)
self.ind = [0]*self.nd
self.index = 0
self.maxvals = args
tot = 1
for k in range(self.nd):
tot *= args[k]
self.total = tot

def _incrementone(self, axis):
if (axis < 0): # base case
return
if (self.ind[axis] < self.maxvals[axis]-1):
self.ind[axis] += 1
else:
self.ind[axis] = 0
self._incrementone(axis-1)

def ndincr(self):
"""
Increment the multi-dimensional index by one.

This method is for backward compatibility only: do not use.
`ndincr` takes care of the "wrapping around" of the axes.
It is called by `ndindex.next` and not normally used directly.

"""
self.next()
self._incrementone(self.nd-1)

def next(self):
"""
Expand All @@ -557,8 +573,17 @@ def next(self):
Returns a tuple containing the indices of the current iteration.

"""
self._it.next()
return self._it.multi_index
if (self.index >= self.total):
raise StopIteration
val = tuple(self.ind)
self.index += 1
self.ndincr()
return val

def __iter__(self):
return self




# You can do all this with slice() plus a few special objects,
Expand Down
9F69 8 changes: 1 addition & 7 deletions numpy/lib/tests/test_index_tricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import numpy as np
from numpy import ( array, ones, r_, mgrid, unravel_index, zeros, where,
ndenumerate, fill_diagonal, diag_indices,
diag_indices_from, s_, index_exp, ndindex )
diag_indices_from, s_, index_exp )

class TestRavelUnravelIndex(TestCase):
def test_basic(self):
Expand Down Expand Up @@ -237,11 +237,5 @@ def test_diag_indices_from():
assert_array_equal(c, np.arange(4))


def test_ndindex():
x = list(np.ndindex(1, 2, 3))
expected = [ix for ix, e in np.ndenumerate(np.zeros((1, 2, 3)))]
assert_array_equal(x, expected)


if __name__ == "__main__":
run_module_suite()
0