<br><br><div class="gmail_quote">On Nov 8, 2007 3:28 AM, Sebastian Haase <<a href="mailto:haase@msg.ucsf.edu">haase@msg.ucsf.edu</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><div></div><div class="Wj3C7c">On Nov 7, 2007 6:46 PM, Timothy Hochberg <<a href="mailto:tim.hochberg@ieee.org">tim.hochberg@ieee.org</a>> wrote:<br>><br>><br>><br>><br>> On Nov 7, 2007 10:35 AM, Sebastian Haase <
<a href="mailto:haase@msg.ucsf.edu">haase@msg.ucsf.edu</a>> wrote:<br>> ><br>> > On Nov 7, 2007 5:23 PM, Matthieu Brucher <<a href="mailto:matthieu.brucher@gmail.com">matthieu.brucher@gmail.com</a>><br>
> wrote:<br>> > ><br>> > > > I don't understand.  I'm thinking of most math functions in the<br>> > > > C-library. In C a boolean is just an integer of 0 or 1 (quasi, by<br>> > > > definition).
<br>> > > ><br>> > > > Could you explain what you mean ?<br>> > > ><br>> > ><br>> > > In C++, bool is a new type that has two values, true and false. If you<br>> add
<br>> > > true and true, it is still true, and not 2. In C, everything that is not<br>> 0<br>> > > is true, not in C++.<br>> ><br>> > Yes, I know this. But my situation is "the other way around". Lets say
<br>> > I want to count "foreground pixels" in an image: I would want to "sum"<br>> > all the true values, i.e. a true *is* a 1 and a false *is* a 0.<br>> ><br>> > In other words, I'm really thinking of (older kind of) C, where there
<br>> > *was* no bool.<br>> > I assume this thinking still applies to the internal arithmetic of CPUs<br>> today.<br>> > Also the "bit-values" of a boolean array (in memory) are set this way
<br>> > already anyway !<br>> ><br>> > How can I simply call my functions looking at these bit values ?<br>> > (essentially interpreting a boolean true as 1 and false as 0)<br>><br>> I'm not sure how well this would work, but could you change the dtype before
<br>> passing the array to your function? If you wanted a copy, you could just to<br>> the equivalent of a.astype(unit8). However, if you didn't want a copy, you<br>> could set the dtype to unit8, operate on the array and then reset it to
<br>> bool:<br>> >>> a = np.array([True, True, False, True])<br>> >>> a<br>> array([ True,  True, False,  True], dtype=bool)<br>> >>> a.dtype = np.uint8<br>> >>> a<br>
> array([1, 1, 0, 1], dtype=uint8)<br>> >>> # do something with 'a' here<br>> >>> a.dtype = bool<br>> >>> a<br>> array([ True,  True, False,  True], dtype=bool)<br>> This assumes everything is single threaded. If you have multiple threads
<br>> accessing 'a', this could be a problem... And, you probably want to do this<br>> in C, so translate as appropriate.<br>><br>> -tim<br>><br></div></div>Thanks Tim, this sound like a good idea.  How about creating an
<br>a = a.view()<br>before changing dtype. </blockquote><div><br>Yeah. That's a better idea. You should be able to just use "a.view(np.uint8)".<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
This should make the proposed solution thread safe again.<br>How "expensive" is the creation of a view (performance wise, e.g.<br>compared to calling a trivial C-function) ?</blockquote><div><br>It should be pretty cheap in theory, but I have no idea really.
<br></div></div><br>-- <br>.  __<br>.   |-\<br>.<br>.  <a href="mailto:tim.hochberg@ieee.org">tim.hochberg@ieee.org</a>