Re: [Numpy-discussion] all elements equal

2012-03-05 Thread Keith Goodman
On Mon, Mar 5, 2012 at 12:12 PM, Keith Goodman wrote: > On Mon, Mar 5, 2012 at 12:06 PM, Neal Becker wrote: >> But doesn't this one fail on empty array? > > Yes. I'm optimizing for fun, not for corner cases. This should work > for size zero and NaNs: > > @cython.boundscheck(False) > @cython.wrapa

Re: [Numpy-discussion] all elements equal

2012-03-05 Thread Keith Goodman
On Mon, Mar 5, 2012 at 12:06 PM, Neal Becker wrote: > But doesn't this one fail on empty array? Yes. I'm optimizing for fun, not for corner cases. This should work for size zero and NaNs: @cython.boundscheck(False) @cython.wraparound(False) def allequal(np.ndarray[np.float64_t, ndim=1] a): c

Re: [Numpy-discussion] all elements equal

2012-03-05 Thread Neal Becker
Keith Goodman wrote: > On Mon, Mar 5, 2012 at 11:52 AM, Benjamin Root wrote: >> Another issue to watch out for is if the array is empty. Technically >> speaking, that should be True, but some of the solutions offered so far >> would fail in this case. > > Good point. > > For fun, here's the sp

Re: [Numpy-discussion] all elements equal

2012-03-05 Thread Brett Olsen
> Another issue to watch out for is if the array is empty.  Technically > speaking, that should be True, but some of the solutions offered so far > would fail in this case. Similarly, NaNs or Infs could cause problems: they should signal as False, but several of the solutions would return True.

Re: [Numpy-discussion] all elements equal

2012-03-05 Thread Keith Goodman
On Mon, Mar 5, 2012 at 11:52 AM, Benjamin Root wrote: > Another issue to watch out for is if the array is empty.  Technically > speaking, that should be True, but some of the solutions offered so far > would fail in this case. Good point. For fun, here's the speed of a simple cython allclose: I

Re: [Numpy-discussion] all elements equal

2012-03-05 Thread Benjamin Root
On Mon, Mar 5, 2012 at 1:44 PM, Keith Goodman wrote: > On Mon, Mar 5, 2012 at 11:36 AM, wrote: > > How about numpy.ptp, to follow this line? I would expect it's single > > pass, but wouldn't short circuit compared to cython of Keith > > I[1] a = np.ones(10) > I[2] timeit (a == a[0]).all() >

Re: [Numpy-discussion] all elements equal

2012-03-05 Thread Keith Goodman
On Mon, Mar 5, 2012 at 11:36 AM, wrote: > How about numpy.ptp, to follow this line? I would expect it's single > pass, but wouldn't short circuit compared to cython of Keith I[1] a = np.ones(10) I[2] timeit (a == a[0]).all() 1000 loops, best of 3: 203 us per loop I[3] timeit a.min() == a.max

Re: [Numpy-discussion] all elements equal

2012-03-05 Thread josef . pktd
On Mon, Mar 5, 2012 at 2:33 PM, Olivier Delalleau wrote: > Le 5 mars 2012 14:29, Keith Goodman a écrit : > >> On Mon, Mar 5, 2012 at 11:24 AM, Neal Becker wrote: >> > Keith Goodman wrote: >> > >> >> On Mon, Mar 5, 2012 at 11:14 AM, Neal Becker >> >> wrote: >> >>> What is a simple, efficient way

Re: [Numpy-discussion] all elements equal

2012-03-05 Thread Olivier Delalleau
Le 5 mars 2012 14:29, Keith Goodman a écrit : > On Mon, Mar 5, 2012 at 11:24 AM, Neal Becker wrote: > > Keith Goodman wrote: > > > >> On Mon, Mar 5, 2012 at 11:14 AM, Neal Becker > wrote: > >>> What is a simple, efficient way to determine if all elements in an > array (in > >>> my case, 1D) are

Re: [Numpy-discussion] all elements equal

2012-03-05 Thread John Hunter
On Mon, Mar 5, 2012 at 1:29 PM, Keith Goodman wrote: > > I[8] np.allclose(a, a[0]) > O[8] False > I[9] a = np.ones(10) > I[10] np.allclose(a, a[0]) > O[10] True > > One disadvantage of using a[0] as a proxy is that the result depends on the ordering of a (a.max() - a.min()) < epsilon is a

Re: [Numpy-discussion] all elements equal

2012-03-05 Thread Keith Goodman
On Mon, Mar 5, 2012 at 11:24 AM, Neal Becker wrote: > Keith Goodman wrote: > >> On Mon, Mar 5, 2012 at 11:14 AM, Neal Becker wrote: >>> What is a simple, efficient way to determine if all elements in an array (in >>> my case, 1D) are equal?  How about close? >> >> For the exactly equal case, how

Re: [Numpy-discussion] all elements equal

2012-03-05 Thread Zachary Pincus
How about the following? exact: numpy.all(a == a[0]) inexact: numpy.allclose(a, a[0]) On Mar 5, 2012, at 2:19 PM, Keith Goodman wrote: > On Mon, Mar 5, 2012 at 11:14 AM, Neal Becker wrote: >> What is a simple, efficient way to determine if all elements in an array (in >> my >> case, 1D) are equ

Re: [Numpy-discussion] all elements equal

2012-03-05 Thread Neal Becker
Keith Goodman wrote: > On Mon, Mar 5, 2012 at 11:14 AM, Neal Becker wrote: >> What is a simple, efficient way to determine if all elements in an array (in >> my case, 1D) are equal? How about close? > > For the exactly equal case, how about: > > I[1] a = np.array([1,1,1,1]) > I[2] np.unique(a)

Re: [Numpy-discussion] all elements equal

2012-03-05 Thread Keith Goodman
On Mon, Mar 5, 2012 at 11:14 AM, Neal Becker wrote: > What is a simple, efficient way to determine if all elements in an array (in > my > case, 1D) are equal?  How about close? For the exactly equal case, how about: I[1] a = np.array([1,1,1,1]) I[2] np.unique(a).size O[2] 1# All equal I[3]

[Numpy-discussion] all elements equal

2012-03-05 Thread Neal Becker
What is a simple, efficient way to determine if all elements in an array (in my case, 1D) are equal? How about close? ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion