[Numpy-discussion] Fast decrementation of indices

Mads Ipsen mads.ipsen at gmail.com
Tue Feb 4 02:58:24 EST 2014


Hi,

Thanks to everybody for all you valuable responses. This approach by
Rick White seems to nail it all down:

>> b = np.array([
>>  [0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3,  4, 4, 4, 5,  5, 5, 6, 7, 8, 9, 10, 11],
>>  [5, 6, 1, 0, 2, 7, 3, 8, 1, 4, 9, 2, 10, 5, 3, 4, 11, 0, 0, 1, 2, 3,  4,  5]
>> ])
>> 
>> a = [1,2,3,7,8]
>> 
>> keepdata = np.ones(12, dtype=np.bool)
>> keepdata[a] = False
>> w = np.where(keepdata[b[0]] & keepdata[b[1]])
>> newindex = keepdata.cumsum()-1
>> c = newindex[b[:,w[0]]]

Also, I'd like to mention that I did think about using the graph module
from SciPy. But the index bookkeeping done by numpy is in fact index
pointers to memory location in a C++ driver - and not just labels.

An when atoms are deleted, there memory chunks are also cleared, and
therefore all pointers to these must be decremented. So using numpy for
the bookkeeping seems a natural choice.

Best regards,

Mads

On 02/03/2014 02:36 PM, Rick White wrote:
> b = np.array([
>  [0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3,  4, 4, 4, 5,  5, 5, 6, 7, 8, 9, 10, 11],
>  [5, 6, 1, 0, 2, 7, 3, 8, 1, 4, 9, 2, 10, 5, 3, 4, 11, 0, 0, 1, 2, 3,  4,  5]
> ])
> 
> a = [1,2,3,7,8]
> 
> keepdata = np.ones(12, dtype=np.bool)
> keepdata[a] = False
> w = np.where(keepdata[b[0]] & keepdata[b[1]])
> newindex = keepdata.cumsum()-1
> c = newindex[b[:,w[0]]]

-- 
+---------------------------------------------------------+
| Mads Ipsen                                              |
+----------------------+----------------------------------+
| Gåsebæksvej 7, 4. tv | phone:              +45-29716388 |
| DK-2500 Valby        | email:      mads.ipsen at gmail.com |
| Denmark              | map  :   www.tinyurl.com/ns52fpa |
+----------------------+----------------------------------+



More information about the NumPy-Discussion mailing list