[SciPy-user] usings numpy arrays in sets
Robert Kern
robert.kern@gmail....
Thu Jun 25 13:04:11 CDT 2009
On Thu, Jun 25, 2009 at 12:53, Francesc Alted<faltet@pytables.org> wrote:
> A Thursday 25 June 2009 19:33:27 Robert Kern escrigué:
>> > Hmm, now that I think about this, I ask myself if it would not be useful
>> > to implement a functional `__hash__()` method for read-only arrays.
>> > Perhaps there is a show stopper for that, but I can't see it.
>>
>> Even if the memory is not writable from your array doesn't mean that
>> it isn't being modified from another.
>>
>> In [1]: a = arange(10)
>>
>> In [2]: b = a[:]
>>
>> In [3]: b.flags.writeable = False
>>
>> In [4]: b
>> Out[4]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>
>> In [5]: a[3:7] = 10
>>
>> In [6]: a
>> Out[6]: array([ 0, 1, 2, 10, 10, 10, 10, 7, 8, 9])
>>
>> In [7]: b
>> Out[7]: array([ 0, 1, 2, 10, 10, 10, 10, 7, 8, 9])
>
> Yep. However, one could also check for the array owning the data, and if
> true, then we can be pretty sure that the array is immutable. I like the idea
> of having hasheable arrays; they can be handy in some scenarios.
Is that a challenge? :-)
In [15]: a = np.arange(10)
In [16]: a.flags.writeable = False
In [17]: d = a.__array_interface__.copy()
In [18]: d['data'] = (d['data'][0], False)
In [19]: b = np.asarray(np.lib.stride_tricks.DummyArray(d))
In [20]: b.flags
Out[20]:
C_CONTIGUOUS : True
F_CONTIGUOUS : True
OWNDATA : False
WRITEABLE : True
ALIGNED : True
UPDATEIFCOPY : False
In [21]: b[3:7] = 10
In [22]: a
Out[22]: array([ 0, 1, 2, 10, 10, 10, 10, 7, 8, 9])
I agree that it would be handy, but hashability is not the only
problem. When hashes collide, the objects are then compared by
equality. This is a problem for numpy arrays because we do not return
bools.
The proper fix is to make a set() implementation that allows you to
provide your own hash and equality functions. This is a general
solution to a problem that affects more than just numpy arrays.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the SciPy-user
mailing list