ANN: mxNumber -- Experimental Number Types, Version 0.2.0

Brian Kelley kelley at bioreason.com
Fri Apr 27 17:34:46 EDT 2001


Tim Peters wrote:

> [Brian Kelley]
> > I have taken the opportunity to expose a couple of Integer methods
> >
> > Integer.popcount() - number of bits set
> > Integer.setbit(bit) - set the bit indicated by bit
> > Integer.clrbit(bit) - set the bit indicated by bit
> >
> > These are very useful for Aritificial Intelligence/feature vector
> > algorithms.  Is there any other desire to see these types of functions
> > exposed?
>
> Yes, provided they're carefully defined.  For example, what's the popcount
> of -1?  If you're viewing Integers as using 2's-comp notation (as Python
> does), there's no obvious answer to that (-1 is an "infinite string" of 1
> bits), which makes it *especially* important to define what it does.  All
> previous attempts to define these kinds of things floundered to death in an
> ocean of competing silly arguments.
>

This is actually defined from the original api although I am probably opening
up the sea of silly arguments.

Function: unsigned long int mpz_popcount (mpz_t op)
    For non-negative numbers, return the population count of op. For negative
numbers, return the largest possible value (MAX_ULONG).

more good stuff at
http://www.swox.com/gmp/manual/gmp_6.html#SEC30

> Pickles are supposed to be platform- and release-independent, so you can't
> change what the existing long format code does.  Note that it's "a feature"
> of text-mode pickles that they're composed of printable ASCII, too.  It's
> possible that you may be able to add a new format code to binary pickles.

This is more what I meant:

>>i = mx.Number.Integer("100101011101010")
>>pickle.dump(i,0)
"cmx.Number\n_I\np0\n(S'10101010101010'\np1\ntp2\nRp3\n."

The string S'10101010101010' is a fairly wasteful encoding for a bit vector.
There could be an encoded string form that generates the same bits but with
fewer characters.  Since mx.Number.Integer already takes a string as an
initializer there could perhaps be an encoded string form that is also
accepted.

Note that 10101010101010 needs roughly 44 bits to store.  The string is 112
bits in the pickle.  I wasn't intending to modify pickle at this stage, just
make (perhaps) a more efficient pickle representation.

Brian Kelley





More information about the Python-list mailing list