On Jun 28, 2007, at 5:04 PM, Martin Albrecht wrote:

> Hi there,
>
> I often come across the situation where I have to construct an  
> integer from
> its binary representation and vice versa. So far you do it in SAGE  
> using
> strings. I have attached a preliminary patch which allows the  
> following code
> to work, i.e. I _replaced_ the binary() method (which returned a  
> string) with
> a method that returns a tuple of Python ints.
>
> sage: e = ZZ(10231252).binary(); e
> (1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1,  
> 0, 0)
> sage: ZZ(e,2)
> 10231252
>
> I know that this patch has some issues:
> * it breaks doctests which rely on strings (fix by replace ZZ.binary 
> () with
> ZZ.str(2))
> * it returns a tuple of Python ints should return tuple (?) of GF 
> (2) elements
> but I didn't want to mess with imports yet
> * it can be done faster
> * you loose the minus sign: ZZ(-1).binary() is (1,), shall it raise an
> exception then?
>
> I am not out for micromanagement but I hesitate to just submit a  
> polished
> patch as the Integers are such a central building block of SAGE.  
> So, does
> anybody actually like the current behavior of ZZ.binary() which  
> returns a
> (signed) string? Any other objections against making the above work?

Hmmm I don't know if I like this. Well, I don't have any objections  
to such a method being available, but I prefer the name "binary" to  
have the current behaviour. It's more pythonic, like the hex function.

BTW for the list format, I would prefer the least significant bit to  
be listed first. This is more like the list() function for  
polynomials, if you think of an integer as a polynomial in 2:

eleven = 1*2^0 + 1*2^1 + 0*2^2 + 1*2^3 = [1, 1, 0, 1]

david


--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://sage.scipy.org/sage/ and http://modular.math.washington.edu/sage/
-~----------~----~----~----~------~----~------~--~---

Reply via email to