[Numpy-discussion] unexpected behavior of na.where (no short-circuiting)

Warren Focke focke at slac.stanford.edu
Wed Jul 20 10:37:17 EDT 2005


Well, that's the way Python works - function arguments are evaluated
before the function is called.

Even if there were some way to pass the arguments in as expressions, and
evaluate them on the fly, you'd need some sort of lazy evaluation, and
some pretty deep parsing of the expression - in this case, you could calc
1./a[x,y,z,whatever] for the appropriate values of [x,y,z,whatever], but
what if, instead of 1./a, you have divide.outer(b,c)?  Or
videoCamera.getNextFrame()?  I don't think the general problem of figuring
out how to iterate over the indices of subexpressions in the argument to
get the appropriate index into the evaluated argument is soluble, even
when there are indexable subexpressions.

In this case, you can pre-mask the array:
>>> a = na.arange(4)-2
a[a==0] = 1./999
>>> na.where(a != 0,1./a, 999)

Warren Focke

On Tue, 19 Jul 2005, Sebastian Haase wrote:

> Hi,
> any thoughts on this ? I really think it's counter intuitive ?
>
> Thanks,
> Sebastian Haase
>
> On Tuesday 05 July 2005 17:34, Sebastian Haase wrote:
> > Hi,
> >
> > I was very surprised when I got this warning:
> > >>> a = na.arange(4)-2
> > >>> na.where(a != 0,1./a, 999)
> >
> > Warning: Encountered divide by zero(s)  in divide
> > [  -0.5   -1.   999.     1. ]
> >
> > Then I realized that this is generally referred to as (not) "short
> > circuiting" (e.g. in the case of the '?:'-C-operator, the middle part never
> > gets evaluated at all if the first part evals to 0 )
> >
> > Especially annoying was this because (for debugging) I had set this
> >
> > error-mode:
> > >>> na.Error.setMode(dividebyzero="error")
> >
> > My questions are:
> >  a) Did other people encounter this problem ?
> >  b) What is the general feeling about this actually being a "problem" ?
> >  c) Could this (at all possible) be implemented differently ?
> >
> > Thanks,
> > Sebastian Haase
> >
> >
> >
> >
> >
> >
> >
> > -------------------------------------------------------
> > SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
> > from IBM. Find simple to follow Roadmaps, straightforward articles,
> > informative Webcasts and more! Get everything you need to get up to
> > speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
> > _______________________________________________
> > Numpy-discussion mailing list
> > Numpy-discussion at lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/numpy-discussion
>
>
> -------------------------------------------------------
> SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
> from IBM. Find simple to follow Roadmaps, straightforward articles,
> informative Webcasts and more! Get everything you need to get up to
> speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion
>




More information about the NumPy-Discussion mailing list