SWIGed function does not like my boolean array
![](https://secure.gravatar.com/avatar/6194b135cba546afa82516de1537de49.jpg?s=120&d=mm&r=g)
Hi, I have a templated function written in C++. My SWIG typemap instantiates this for many argument array types (such as unit8, int16, uint16, int32, float32,float64,...) All works well, except when feeding in a boolean array as in seb.mmms(a>6) I get this error: NotImplementedError: No matching function for overloaded 'mmms' What should I do ? Preferably I would avoid having to add another type-instantiation into the library (it looks already quite bloated having 6+ versions of every function). Isn't bool just a synonym for int32 ? Thanks Sebastian Haase
![](https://secure.gravatar.com/avatar/60e03bd1fd9f2dbc750e0899b9e7e71d.jpg?s=120&d=mm&r=g)
Hi, No, a bool is not an int32. Try just sizeof(bool) to be sure (on my box, it's one). Besides, if you use a std::vector of bool, be aware of the fact that it is not like the other vectors. Matthieu 2007/11/7, Sebastian Haase <haase@msg.ucsf.edu>:
Hi, I have a templated function written in C++. My SWIG typemap instantiates this for many argument array types (such as unit8, int16, uint16, int32, float32,float64,...) All works well, except when feeding in a boolean array as in seb.mmms(a>6) I get this error: NotImplementedError: No matching function for overloaded 'mmms'
What should I do ? Preferably I would avoid having to add another type-instantiation into the library (it looks already quite bloated having 6+ versions of every function). Isn't bool just a synonym for int32 ?
Thanks Sebastian Haase _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
-- French PhD student Website : http://miles.developpez.com/ Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92 LinkedIn : http://www.linkedin.com/in/matthieubrucher
![](https://secure.gravatar.com/avatar/6194b135cba546afa82516de1537de49.jpg?s=120&d=mm&r=g)
O.K., so sizeof(bool) is 1. But I already have a function instantiation for uint8. The problem is that without doing "some magic" the compiler (?, or numpy ?) would never allow to use anything but a "dedicated" bool-typed function for bool. Even though the CPU treats bool exactly like an integer of same byte-width. How can I have numpy (or is SWIG the problem ??) use my existing integer function for a bool-type array ? (Without making a differently-typed copy of course ...) Thanks, Sebastian PS: I'm not using the C++ std library. On Nov 7, 2007 2:54 PM, Matthieu Brucher <matthieu.brucher@gmail.com> wrote:
Hi,
No, a bool is not an int32. Try just sizeof(bool) to be sure (on my box, it's one). Besides, if you use a std::vector of bool, be aware of the fact that it is not like the other vectors.
Matthieu
2007/11/7, Sebastian Haase <haase@msg.ucsf.edu>:
Hi, I have a templated function written in C++. My SWIG typemap instantiates this for many argument array types (such as unit8, int16, uint16, int32, float32,float64,...) All works well, except when feeding in a boolean array as in seb.mmms(a>6) I get this error: NotImplementedError: No matching function for overloaded 'mmms'
What should I do ? Preferably I would avoid having to add another type-instantiation into the library (it looks already quite bloated having 6+ versions of every function). Isn't bool just a synonym for int32 ?
Thanks Sebastian Haase _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
-- French PhD student Website : http://miles.developpez.com/ Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92 LinkedIn : http://www.linkedin.com/in/matthieubrucher _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
![](https://secure.gravatar.com/avatar/60e03bd1fd9f2dbc750e0899b9e7e71d.jpg?s=120&d=mm&r=g)
Unfortunately, operations on boolean are not the same as operations on integers, so you can't replace one with another. Matthieu 2007/11/7, Sebastian Haase <haase@msg.ucsf.edu>:
O.K., so sizeof(bool) is 1. But I already have a function instantiation for uint8.
The problem is that without doing "some magic" the compiler (?, or numpy ?) would never allow to use anything but a "dedicated" bool-typed function for bool. Even though the CPU treats bool exactly like an integer of same byte-width.
How can I have numpy (or is SWIG the problem ??) use my existing integer function for a bool-type array ? (Without making a differently-typed copy of course ...)
Thanks, Sebastian
PS: I'm not using the C++ std library.
On Nov 7, 2007 2:54 PM, Matthieu Brucher <matthieu.brucher@gmail.com> wrote:
Hi,
No, a bool is not an int32. Try just sizeof(bool) to be sure (on my box, it's one). Besides, if you use a std::vector of bool, be aware of the fact that it is not like the other vectors.
Matthieu
2007/11/7, Sebastian Haase <haase@msg.ucsf.edu>:
Hi, I have a templated function written in C++. My SWIG typemap instantiates this for many argument array types (such as unit8, int16, uint16, int32, float32,float64,...) All works well, except when feeding in a boolean array as in seb.mmms(a>6) I get this error: NotImplementedError: No matching function for overloaded 'mmms'
What should I do ? Preferably I would avoid having to add another type-instantiation into the library (it looks already quite bloated having 6+ versions of every function). Isn't bool just a synonym for int32 ?
Thanks Sebastian Haase _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
-- French PhD student Website : http://miles.developpez.com/ Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92 LinkedIn : http://www.linkedin.com/in/matthieubrucher _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
-- French PhD student Website : http://miles.developpez.com/ Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92 LinkedIn : http://www.linkedin.com/in/matthieubrucher
![](https://secure.gravatar.com/avatar/6194b135cba546afa82516de1537de49.jpg?s=120&d=mm&r=g)
On Nov 7, 2007 4:45 PM, Matthieu Brucher <matthieu.brucher@gmail.com> wrote:
Unfortunately, operations on boolean are not the same as operations on integers, so you can't replace one with another.
I don't understand. I'm thinking of most math functions in the C-library. In C a boolean is just an integer of 0 or 1 (quasi, by definition). Could you explain what you mean ? Thanks, Sebastian
2007/11/7, Sebastian Haase < haase@msg.ucsf.edu>:
O.K., so sizeof(bool) is 1. But I already have a function instantiation for uint8.
The problem is that without doing "some magic" the compiler (?, or numpy ?) would never allow to use anything but a "dedicated" bool-typed function for bool. Even though the CPU treats bool exactly like an integer of same byte-width.
How can I have numpy (or is SWIG the problem ??) use my existing integer function for a bool-type array ? (Without making a differently-typed copy of course ...)
Thanks, Sebastian
PS: I'm not using the C++ std library.
On Nov 7, 2007 2:54 PM, Matthieu Brucher <matthieu.brucher@gmail.com> wrote:
Hi,
No, a bool is not an int32. Try just sizeof(bool) to be sure (on my box, it's one). Besides, if you use a std::vector of bool, be aware of the fact that it is not like the other vectors.
Matthieu
2007/11/7, Sebastian Haase < haase@msg.ucsf.edu>:
Hi, I have a templated function written in C++. My SWIG typemap instantiates this for many argument array types (such as unit8, int16, uint16, int32, float32,float64,...) All works well, except when feeding in a boolean array as in seb.mmms(a>6) I get this error: NotImplementedError: No matching function for overloaded 'mmms'
What should I do ? Preferably I would avoid having to add another type-instantiation into the library (it looks already quite bloated having 6+ versions of every function). Isn't bool just a synonym for int32 ?
Thanks Sebastian Haase _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
-- French PhD student Website : http://miles.developpez.com/ Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92 LinkedIn : http://www.linkedin.com/in/matthieubrucher _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
-- French PhD student Website : http://miles.developpez.com/ Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92 LinkedIn : http://www.linkedin.com/in/matthieubrucher _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
![](https://secure.gravatar.com/avatar/60e03bd1fd9f2dbc750e0899b9e7e71d.jpg?s=120&d=mm&r=g)
I don't understand. I'm thinking of most math functions in the C-library. In C a boolean is just an integer of 0 or 1 (quasi, by definition).
Could you explain what you mean ?
In C++, bool is a new type that has two values, true and false. If you add true and true, it is still true, and not 2. In C, everything that is not 0 is true, not in C++. Matthieu -- French PhD student Website : http://miles.developpez.com/ Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92 LinkedIn : http://www.linkedin.com/in/matthieubrucher
![](https://secure.gravatar.com/avatar/6194b135cba546afa82516de1537de49.jpg?s=120&d=mm&r=g)
On Nov 7, 2007 5:23 PM, Matthieu Brucher <matthieu.brucher@gmail.com> wrote:
I don't understand. I'm thinking of most math functions in the C-library. In C a boolean is just an integer of 0 or 1 (quasi, by definition).
Could you explain what you mean ?
In C++, bool is a new type that has two values, true and false. If you add true and true, it is still true, and not 2. In C, everything that is not 0 is true, not in C++.
Yes, I know this. But my situation is "the other way around". Lets say I want to count "foreground pixels" in an image: I would want to "sum" all the true values, i.e. a true *is* a 1 and a false *is* a 0. In other words, I'm really thinking of (older kind of) C, where there *was* no bool. I assume this thinking still applies to the internal arithmetic of CPUs today. Also the "bit-values" of a boolean array (in memory) are set this way already anyway ! How can I simply call my functions looking at these bit values ? (essentially interpreting a boolean true as 1 and false as 0) -Sebastian
![](https://secure.gravatar.com/avatar/5b2449484c19f8e037c5d9c71e429508.jpg?s=120&d=mm&r=g)
On Nov 7, 2007 10:35 AM, Sebastian Haase <haase@msg.ucsf.edu> wrote:
On Nov 7, 2007 5:23 PM, Matthieu Brucher <matthieu.brucher@gmail.com> wrote:
I don't understand. I'm thinking of most math functions in the C-library. In C a boolean is just an integer of 0 or 1 (quasi, by definition).
Could you explain what you mean ?
In C++, bool is a new type that has two values, true and false. If you
add
true and true, it is still true, and not 2. In C, everything that is not 0 is true, not in C++.
Yes, I know this. But my situation is "the other way around". Lets say I want to count "foreground pixels" in an image: I would want to "sum" all the true values, i.e. a true *is* a 1 and a false *is* a 0.
In other words, I'm really thinking of (older kind of) C, where there *was* no bool. I assume this thinking still applies to the internal arithmetic of CPUs today. Also the "bit-values" of a boolean array (in memory) are set this way already anyway !
How can I simply call my functions looking at these bit values ? (essentially interpreting a boolean true as 1 and false as 0)
I'm not sure how well this would work, but could you change the dtype before passing the array to your function? If you wanted a copy, you could just to the equivalent of a.astype(unit8). However, if you didn't want a copy, you could set the dtype to unit8, operate on the array and then reset it to bool:
a = np.array([True, True, False, True]) a array([ True, True, False, True], dtype=bool) a.dtype = np.uint8 a array([1, 1, 0, 1], dtype=uint8) # do something with 'a' here a.dtype = bool a array([ True, True, False, True], dtype=bool) This assumes everything is single threaded. If you have multiple threads accessing 'a', this could be a problem... And, you probably want to do this in C, so translate as appropriate.
-tim
-Sebastian _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
-- . __ . |-\ . . tim.hochberg@ieee.org
![](https://secure.gravatar.com/avatar/6194b135cba546afa82516de1537de49.jpg?s=120&d=mm&r=g)
On Nov 7, 2007 6:46 PM, Timothy Hochberg <tim.hochberg@ieee.org> wrote:
On Nov 7, 2007 10:35 AM, Sebastian Haase <haase@msg.ucsf.edu> wrote:
On Nov 7, 2007 5:23 PM, Matthieu Brucher <matthieu.brucher@gmail.com>
wrote:
I don't understand. I'm thinking of most math functions in the C-library. In C a boolean is just an integer of 0 or 1 (quasi, by definition).
Could you explain what you mean ?
In C++, bool is a new type that has two values, true and false. If you
add
true and true, it is still true, and not 2. In C, everything that is not 0 is true, not in C++.
Yes, I know this. But my situation is "the other way around". Lets say I want to count "foreground pixels" in an image: I would want to "sum" all the true values, i.e. a true *is* a 1 and a false *is* a 0.
In other words, I'm really thinking of (older kind of) C, where there *was* no bool. I assume this thinking still applies to the internal arithmetic of CPUs today. Also the "bit-values" of a boolean array (in memory) are set this way already anyway !
How can I simply call my functions looking at these bit values ? (essentially interpreting a boolean true as 1 and false as 0)
I'm not sure how well this would work, but could you change the dtype before passing the array to your function? If you wanted a copy, you could just to the equivalent of a.astype(unit8). However, if you didn't want a copy, you could set the dtype to unit8, operate on the array and then reset it to bool:
a = np.array([True, True, False, True]) a array([ True, True, False, True], dtype=bool) a.dtype = np.uint8 a array([1, 1, 0, 1], dtype=uint8) # do something with 'a' here a.dtype = bool a array([ True, True, False, True], dtype=bool) This assumes everything is single threaded. If you have multiple threads accessing 'a', this could be a problem... And, you probably want to do this in C, so translate as appropriate.
-tim
Thanks Tim, this sound like a good idea. How about creating an a = a.view() before changing dtype. This should make the proposed solution thread safe again. How "expensive" is the creation of a view (performance wise, e.g. compared to calling a trivial C-function) ? Thanks, Sebastian
![](https://secure.gravatar.com/avatar/5b2449484c19f8e037c5d9c71e429508.jpg?s=120&d=mm&r=g)
On Nov 8, 2007 3:28 AM, Sebastian Haase <haase@msg.ucsf.edu> wrote:
On Nov 7, 2007 6:46 PM, Timothy Hochberg <tim.hochberg@ieee.org> wrote:
On Nov 7, 2007 10:35 AM, Sebastian Haase <haase@msg.ucsf.edu> wrote:
On Nov 7, 2007 5:23 PM, Matthieu Brucher <matthieu.brucher@gmail.com>
wrote:
I don't understand. I'm thinking of most math functions in the C-library. In C a boolean is just an integer of 0 or 1 (quasi, by definition).
Could you explain what you mean ?
In C++, bool is a new type that has two values, true and false. If
add
true and true, it is still true, and not 2. In C, everything that is not 0 is true, not in C++.
Yes, I know this. But my situation is "the other way around". Lets say I want to count "foreground pixels" in an image: I would want to "sum" all the true values, i.e. a true *is* a 1 and a false *is* a 0.
In other words, I'm really thinking of (older kind of) C, where there *was* no bool. I assume this thinking still applies to the internal arithmetic of CPUs today. Also the "bit-values" of a boolean array (in memory) are set this way already anyway !
How can I simply call my functions looking at these bit values ? (essentially interpreting a boolean true as 1 and false as 0)
I'm not sure how well this would work, but could you change the dtype before passing the array to your function? If you wanted a copy, you could just to the equivalent of a.astype(unit8). However, if you didn't want a copy, you could set the dtype to unit8, operate on the array and then reset it to bool:
a = np.array([True, True, False, True]) a array([ True, True, False, True], dtype=bool) a.dtype = np.uint8 a array([1, 1, 0, 1], dtype=uint8) # do something with 'a' here a.dtype = bool a array([ True, True, False, True], dtype=bool) This assumes everything is single threaded. If you have multiple threads accessing 'a', this could be a problem... And, you probably want to do
you this
in C, so translate as appropriate.
-tim
Thanks Tim, this sound like a good idea. How about creating an a = a.view() before changing dtype.
Yeah. That's a better idea. You should be able to just use "a.view(np.uint8 )".
This should make the proposed solution thread safe again. How "expensive" is the creation of a view (performance wise, e.g. compared to calling a trivial C-function) ?
It should be pretty cheap in theory, but I have no idea really. -- . __ . |-\ . . tim.hochberg@ieee.org
participants (3)
-
Matthieu Brucher
-
Sebastian Haase
-
Timothy Hochberg