[Numpy-discussion] short circuit != ?

Lluís xscript@gmx....
Wed Oct 27 12:06:36 CDT 2010

Alan G Isaac writes:

> Maybe:
> any((ai != bi) for ai,bi in izip(a.flat,b.flat))
> ?

I think this would indeed work, but it's not really friendly, while it
should work out-of-the-box.

Of course, we do not always want operations to perform lazily, so a
generic approach might be on the lines of:

  any(a.lazy() != b)

Invoking 'lazy' on an ndarray would return an object that instead of
computing the given operations will just record them, and try to defer
their evaluation as much as possible.


  class LazyNdarray:

        def __init__ (self, left, op = None, right = None):
            self._left = left
            self._op = op
            self._right = right

        # transitivize deferral on these operations

        def __neq__ (self, other):
            return LazyNdarray(self, "__neq__", other)

        # evaluate on these operations

        def __getitem__ (self, index):
            return self._evaluate(index)

        def __iter__ (self):
            for idx in range(len(self._left)):
                yield self._evaluate(idx)

        # do the evaluation

        def _evaluate (self, index)
            # still, this will not work unles no broadcast is necessary
            l = self._left[index]
            op = getattr(l, self._op)
            return op(right[index])

  class ndarray:
        def lazy (self):
            return LazyNdarray(self)

Or something like that.


 "And it's much the same thing with knowledge, for whenever you learn
 something new, the whole world becomes that much richer."
 -- The Princess of Pure Reason, as told by Norton Juster in The Phantom

More information about the NumPy-Discussion mailing list