[Numpy-discussion] Should bool_ subclass int?
Gael Varoquaux
gael.varoquaux@normalesup....
Tue Jul 10 08:08:02 CDT 2007
On Tue, Jul 10, 2007 at 02:39:28PM +0200, Sebastian Haase wrote:
> When you talk about algebra - one might have to restrict one self to '|' and '&'
> -- not use '+' and '-'
> E.g.:
> True - True = False # right !?
> # but if:
> True+True = True.
> # then
> True+True -False = True -False # ????
> # here I'm already lost ... I don't think this can be done in a consistent way.
It can, its called the Bool algebra, and it is a consistent algebra, in a
mathematical sense of algebra
(http://en.wikipedia.org/wiki/Boolean_algebra), actually what we are
talking about is the two element bool algebra
(http://en.wikipedia.org/wiki/Two-element_Boolean_algebra), and the
mathematical structure we are taling about is a ring, the wikipedia
article is quite comprehensible
(http://en.wikipedia.org/wiki/Ring_(mathematics))
> In other words: a "+" operator would also need a corresponding "-"
Yes. In other words (the ensemble theory words) each element needs to
have an opposite concerning the '+' law. To understand this you need a
bit of algebra theory.
* An algebra has 2 laws, lets call them "+" and "*".
* Each law has a neutral element for this law, ie an element a for which
"a + b = b" for all b in the algebra, lets write these "n+", and "n*".
* Each element a is required to have an inverse for the "+", ie an element
b for wich b + a = n+, lets write the opposite of b "-b".
For integer, n+ = 0, n* = 1.
For Booleans, n+ = False, and n+ = True, therefore, as Matthieu points out,
-True = True, as True + True = n+ = True,
and -False = True, as True + False = n+ = True.
So you have a consistent algebra.
Now there is a law for which every element does not have an inverse, it
the "*" law. You can check the out for integers. It is also true for
booleans. In fact, you can proove that in an ring, n+ cannot have an
inverse for the * law (it the famous divide by zero error !).
In conclusion, I would like to stress that, yes, +, - and * are well
defined on booleans, the definition is universal, and please don't try to
change it.
Gaël
More information about the Numpy-discussion
mailing list