[Numpy-discussion] vectorizing histogram-like computation

Piotr Luszczek luszczek at cs.utk.edu
Mon Jun 20 13:01:12 CDT 2005


Todd Miller wrote:
> On Mon, 2005-06-20 at 15:36, Piotr Luszczek wrote:
> 
>>Perry Greenfield wrote:
>>
>>>On Jun 20, 2005, at 3:09 PM, Piotr Luszczek wrote:
>>>
>>>
>>>>All,
>>>>
>>>>I apologize if this is a dupe. I haven't seen it where I looked.
>>>>
>>>>There is numarray.numeric.histogram and numarray.mlab.histogram.
>>>>What they do is this (more or less):
>>>>
>>>>for idx in arr:
>>>>   histogram[idx] += 1
>>>>
>>>>What I want to do is:
>>>>
>>>>for idx in arr:
>>>>   result[idx] ^= value
>>>>
>>>>Doing:
>>>>
>>>>result[arr] ^= value
>>>>
>>>>doesn't work. Which is not surprising because:
>>>>
>>>>histogram[arr] += 1
>>>>
>>>>doesn't work either.
>>>>
>>>>My question is whether there is a way to do it (the XOR example)
>>>>without using a for loop.
> 
> 
> I may be misreading your intent,  but here's what I get:
> 
> 
>>>>a = na.zeros((10,))
>>>>a[[1,3,5]] ^= 100
>>>>a
> 
> array([  0, 100,   0, 100,   0, 100,   0,   0,   0,   0])
> 
> It seems to work to me.  What is it that you meant?

I meant exactly what you said. But:

>>> a=na.zeros((10,))
>>> a[[1,3,5,3]] ^= 100
>>> a
array([  0, 100,   0, 100,   0, 100,   0,   0,   0,   0])

So provided that indices are unique - it works.
If indices are not unique, then the last operation on the index
prevails (I think).

Functionality like this is the most useful only for commutative and
associative operators so that numarray has freedom to reorder the
computation at will for duplicates. 

> Regards,
> Todd
> 
> 
>>>>Thanks,
>>>>
>>>>Piotr
>>>
>>>
>>>Not that I'm aware of. It suggests that there may be a need to expand on 
>>>the histogram-like functionality to handle this sort of thing, though I 
>>>wonder how often it would be used.
>>
>>Sparse matrix computations use indirection all the time.
>>And being able to the calculations in C and in-place would be a good
>>thing. But then again I don't know if many numarray users use
>>sparse matrices rather than just make the matrix dense and
>>use LAPACK.




More information about the Numpy-discussion mailing list