[Numpy-discussion] Should bool_ subclass int?
Timothy Hochberg
tim.hochberg@ieee....
Mon Jul 9 14:32:02 CDT 2007
On 7/7/07, Travis Oliphant <oliphant.travis@ieee.org> wrote:
>
>
> >
> >
> > On 7/6/07, *Travis Oliphant* <oliphant.travis@ieee.org
> > <mailto:oliphant.travis@ieee.org>> wrote:
> >
> > Timothy Hochberg wrote:
> > >
> > > I'm working on getting some old code working with numpy and I
> > noticed
> > > that bool_ is not a subclass of int. Given that python's bool
> > > subclasses into and that the other scalar types are subclasses of
> > > their respective counterparts it seems at first glance that
> > > numpy.bool_ should subclass python's bool, which in turn
> subclasses
> > > int. Or am I missing something here?
> > The reason it is not, is because it is not binary compatible with
> > Python's integer. The numpy bool_ is always only 8-bits while the
> > Python integer is 32-bits or 64-bits.
> >
> > This could be changed I suspect, but then it would break the
> > relationship between scalars and their array counterparts
> >
> >
> > Do you have and idea off the top of your head head how painful this
> > would be from an implementation standpoint. And is there a theoretical
> > reason that it is important that the scalar and array implementations
> > match? I would think that, conceptually, they are all 1-bit integers,
> > and it seems that the 8-bit, versus 32- or 64-bits is just an
> > implementation detail.
> It would probably take about 2-3 hours to make the change and about 3
> more hours to fix the problems that were not anticipated. Basically,
> we would have to special-case the bool like we do the unicode scalar
> (which also doesn't necessarily match the array-based representation but
> instead follows the Python implementation).
>
> I guess I don't really see a problem in switching just the numpy.bool_
> scalar to be a sub-class of the Python bool type and adjusting the code
> to make the switch when creating a scalar.
I gave this a try. Since so much code is auto-generated, it can be difficult
to figure out what's going on in the core matrix stuff. Still, it seems like
the solution is almost absurdly easy, consisting of changing only three
lines. First off, does this seem right? Code compiled against this patch
passes all tests and seems to run my application right, but that's not
conclusive.
Please let me know if I missed something obvious.
--
. __
. |-\
.
. tim.hochberg@ieee.org
===================================================================
Index: numpy/core/code_generators/generate_array_api.py
===================================================================
--- numpy/core/code_generators/generate_array_api.py (revision 3883)
+++ numpy/core/code_generators/generate_array_api.py (working copy)
@@ -17,7 +17,7 @@
typedef struct {
PyObject_HEAD
- npy_bool obval;
+ npy_long obval;
} PyBoolScalarObject;
Index: numpy/core/include/numpy/arrayscalars.h
===================================================================
--- numpy/core/include/numpy/arrayscalars.h (revision 3883)
+++ numpy/core/include/numpy/arrayscalars.h (working copy)
@@ -1,7 +1,7 @@
#ifndef _MULTIARRAYMODULE
typedef struct {
PyObject_HEAD
- npy_bool obval;
+ npy_long obval;
} PyBoolScalarObject;
#endif
Index: numpy/core/src/multiarraymodule.c
===================================================================
--- numpy/core/src/multiarraymodule.c (revision 3883)
+++ numpy/core/src/multiarraymodule.c (working copy)
@@ -7417,7 +7417,7 @@
return -1; \
}
- SINGLE_INHERIT(Bool, Generic);
+ DUAL_INHERIT(Bool, Bool, Generic);
SINGLE_INHERIT(Byte, SignedInteger);
SINGLE_INHERIT(Short, SignedInteger);
#if SIZEOF_INT == SIZEOF_LONG
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20070709/4496e096/attachment.html
More information about the Numpy-discussion
mailing list