[Matrix-SIG] Re: NumPy addendum (Error in Documentation)

Travis Oliphant Oliphant.Travis@mayo.edu
Fri, 26 Mar 1999 13:40:47 -0600 (EST)


> Travis,
> 
> I got:
> 
> histogram ((ravel(b)).astype('d'), arange (0.0,255.0,1.0))
> 
> to work. I see that the input matrix to search sorted needed
> to have rank 1 (my fault), but why does it have to be type 'd'?
>

I've found the problem finally.  It's a mistake in the documentation:

searchsorted should be called with the bin_boundaries as the first
argument and the data to be sorted as the second argument.

This is done in the example but when histogram is defined the naming
convention used gets confusing.  So, as it stands you should really say

searchsorted( arange(0.0,255.0,1.0),ravel(b))  or in fact you could use
searchsorted( arange(0,255,1,'b'),ravel(b))  to have comparisons between
      unsigned integers.

and the documentation should be changed.



I still think there is an implementation bug, though, and you should not
get the error you got (even if you did call the function incorrectly ---
being led astray by the documentation).

The error you say is due to the fact that the underlying function has
to make comparisons between numbers.  These numbers must be the same type
for the comparsion to work.  Therefore, the two input arrays have to have
the same type.  As you had it before your two arrays were of different
types.  b was an unsigned char and arange(0.0,255.0,1.0) is an array of
double type.  So, one has to be typecast to the other.  The way it's
implemented now, the first array determines the type of the second.  And
so the code tries to convert your arange into unsigned chars which it
complains about because it would lose information.  

As is done in other routines, this should really convert both arrays to
the "highest" type, so you wouldn't have to convert it yourself.

Best,

Travis