after looking at it for a while, I don't see a way to easily speed it up using pure numpy. <br><br>As a matter of fact, the behavior shown below is a little confusing. Using fancy indexing, multiples of the same index are interpreted as a single call to that index, probably this a for a reason that I dont currently understand. I would think multiple calls to the same index would cause multiple increments in the example below. <br>
<br>For the life of me, I can't think of how to do this 3d histogram in numpy without a for loop. <br><br>Chris<br><br>########## example code #############<br><br><span style="font-family: 'Consolas'; font-size: 10pt; color: rgb(255, 0, 0);">>>> </span><span style="font-family: 'Consolas'; font-size: 10pt; color: rgb(0, 0, 0);">a = np.arange(0,8,1).reshape((2,2,2))</span>
<p style="margin: 0px; text-indent: 0px; font-family: 'Consolas'; font-size: 10pt; color: rgb(0, 0, 0);">>>> a</p>
<p style="margin: 0px; text-indent: 0px; font-family: 'Consolas'; font-size: 10pt; color: rgb(0, 0, 0);">array([[[0, 1],</p>
<p style="margin: 0px; text-indent: 0px; font-family: 'Consolas'; font-size: 10pt; color: rgb(0, 0, 0);"> [2, 3]],</p>
<p style="margin: 0px; text-indent: 0px; font-family: 'Consolas'; font-size: 10pt; color: rgb(0, 0, 0);"> [[4, 5],</p>
<p style="margin: 0px; text-indent: 0px; font-family: 'Consolas'; font-size: 10pt; color: rgb(0, 0, 0);"> [6, 7]]])</p><p style="margin: 0px; text-indent: 0px; font-family: 'Consolas'; font-size: 10pt; color: rgb(0, 0, 0);">
<br></p>
<p style="margin: 0px; text-indent: 0px; font-family: 'Consolas'; font-size: 10pt; color: rgb(0, 0, 0);">>>> indx = np.array([[0,0,0],[0,0,1],[0,1,0],[0,1,1],[1,0,0],[1,0,1],[1,1,0],[1,1,1]])<br></p><span style="font-family: 'Consolas'; font-size: 10pt; color: rgb(0, 0, 0);">>>> indx = indx.reshape(4,2,3)<br>
<br></span>
<p style="margin: 0px; text-indent: 0px; font-family: 'Consolas'; font-size: 10pt; color: rgb(0, 0, 0);">>>> a[indx[:,:,0], indx[:,:,1], indx[:,:,2]]+=1</p>
<p style="margin: 0px; text-indent: 0px; font-family: 'Consolas'; font-size: 10pt; color: rgb(0, 0, 0);">>>> a</p>
<p style="margin: 0px; text-indent: 0px; font-family: 'Consolas'; font-size: 10pt; color: rgb(0, 0, 0);">array([[[2, 3],</p>
<p style="margin: 0px; text-indent: 0px; font-family: 'Consolas'; font-size: 10pt; color: rgb(0, 0, 0);"> [4, 5]],</p>
<p style="margin: 0px; text-indent: 0px; font-family: 'Consolas'; font-size: 10pt; color: rgb(0, 0, 0);"> [[6, 7],</p>
<p style="margin: 0px; text-indent: 0px; font-family: 'Consolas'; font-size: 10pt; color: rgb(0, 0, 0);"> [8, 9]]])</p><p style="margin: 0px; text-indent: 0px; font-family: 'Consolas'; font-size: 10pt; color: rgb(0, 0, 0);">
<br></p>
<p style="margin: 0px; text-indent: 0px; font-family: 'Consolas'; font-size: 10pt; color: rgb(0, 0, 0);">>>> indx2 = np.zeros((4,2,3)).astype(np.uint8)</p><p style="margin: 0px; text-indent: 0px; font-family: 'Consolas'; font-size: 10pt; color: rgb(0, 0, 0);">
<br></p>
<p style="margin: 0px; text-indent: 0px; font-family: 'Consolas'; font-size: 10pt; color: rgb(0, 0, 0);">>>> a[indx2[:,:,0], indx2[:,:,1], indx2[:,:,2]]+=1</p>
<p style="margin: 0px; text-indent: 0px; font-family: 'Consolas'; font-size: 10pt; color: rgb(0, 0, 0);">>>> a</p>
<p style="margin: 0px; text-indent: 0px; font-family: 'Consolas'; font-size: 10pt; color: rgb(0, 0, 0);">array([[[3, 3],</p>
<p style="margin: 0px; text-indent: 0px; font-family: 'Consolas'; font-size: 10pt; color: rgb(0, 0, 0);"> [4, 5]],</p>
<p style="margin: 0px; text-indent: 0px; font-family: 'Consolas'; font-size: 10pt; color: rgb(0, 0, 0);"> [[6, 7],</p>
<p style="margin: 0px; text-indent: 0px; font-family: 'Consolas'; font-size: 10pt; color: rgb(0, 0, 0);"> [8, 9]]])</p>
<p style="margin: 0px; text-indent: 0px; font-family: 'Consolas'; font-size: 10pt; color: rgb(0, 0, 0);">>>> </p><br><br><div class="gmail_quote">On Thu, May 7, 2009 at 5:35 PM, Charles R Harris <span dir="ltr"><<a href="mailto:charlesr.harris@gmail.com">charlesr.harris@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br><br><div class="gmail_quote">2009/5/7 Dag Sverre Seljebotn <span dir="ltr"><<a href="mailto:dagss@student.matnat.uio.no" target="_blank">dagss@student.matnat.uio.no</a>></span><div class="im">
<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div>Stéfan van der Walt wrote:<br>
> 2009/5/7 Chris Colbert <<a href="mailto:sccolbert@gmail.com" target="_blank">sccolbert@gmail.com</a>>:<br>
>> This was really my first attempt at doing anything constructive with Cython.<br>
>> It was actually unbelievably easy to work with. I think i spent less time<br>
>> working on this, than I did trying to find an optimized solution using pure<br>
>> numpy and python.<br>
><br>
> One aspect we often overlook is how easy it is to write a for-loop in<br>
> comparison to vectorisation. Besides, for-loops are sometimes easier<br>
> to read as well!<br>
><br>
> I think the Cython guys are planning some sort of templating, but I'll<br>
> CC Dag so that he can tell us more.<br>
<br>
</div>We were discussing how it would/should look like, but noone's committed<br>
to implementing it so it's pretty much up in the blue I think -- someone<br>
might jump in and do it next week, or it might go another year, I can't<br>
tell.<br>
<br>
While I'm here, also note in that code Chris wrote that you want to pay<br>
attention to the change of default division semantics on Cython 0.12<br>
(especially for speed).<br>
<br>
<a href="http://wiki.cython.org/enhancements/division" target="_blank">http://wiki.cython.org/enhancements/division</a><br>
</blockquote></div><div><br>Hi Dag,<br><br>Numpy can now do separate compilations with controlled export of symbols when the object files are linked together to make a module. Does Cython have anyway of controlling the visibility of symbols or should we just include the right files in Numpy to get the needed macros?<br>
<br>Chuck </div></div><br>
<br>_______________________________________________<br>
Numpy-discussion mailing list<br>
<a href="mailto:Numpy-discussion@scipy.org">Numpy-discussion@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
<br></blockquote></div><br>