mpmath and files

35 views
Skip to first unread message

denis joubert

unread,
Sep 7, 2009, 10:41:37 AM9/7/09
to mpmath
Hello,
In mpmath, is there a way to process binary files as number ? and to
write numbers to files in binary format ?
thanks.

Fredrik Johansson

unread,
Sep 7, 2009, 10:56:22 AM9/7/09
to mpm...@googlegroups.com

Not directly, although you can use Python's pickle module to dump and
load mpmath numbers to files losslessly.

Direct binary input/output functions would certainly be possible to
implement. What are your precise requirements? Do you need to read
numbers in some existing format?

Fredrik

denis joubert

unread,
Sep 7, 2009, 1:32:50 PM9/7/09
to mpm...@googlegroups.com
I need to binary files not in real format but like the gmp c function mpz_import convert any files into big integers.

2009/9/7 Fredrik Johansson <fredrik....@gmail.com>

Fredrik Johansson

unread,
Sep 7, 2009, 7:50:27 PM9/7/09
to mpm...@googlegroups.com
On Mon, Sep 7, 2009 at 7:32 PM, denis joubert<denis....@gmail.com> wrote:
> I need to binary files not in real format but like the gmp c function
> mpz_import convert any files into big integers.

It sounds like what you need is a function to convert a byte array
into a Python long?

Fredrik

William Stein

unread,
Sep 7, 2009, 8:00:45 PM9/7/09
to mpm...@googlegroups.com

I think there is very complicated highly optimized code somewhere in Sage
(http://sagemath.org) for conversion between Python longs and GMP
mpz_t's. If this is what you need, it could be dug up.

William

denis joubert

unread,
Sep 8, 2009, 12:05:09 PM9/8/09
to mpm...@googlegroups.com
Could Python long store 6400bits and upper ?

2009/9/8 William Stein <wst...@gmail.com>

William Stein

unread,
Sep 8, 2009, 12:12:13 PM9/8/09
to mpm...@googlegroups.com
On Tue, Sep 8, 2009 at 9:05 AM, denis joubert<denis....@gmail.com> wrote:
> Could Python long store 6400bits and upper ?

Yes, with no trouble. They are arbitrary precision.

William
--
William Stein
Associate Professor of Mathematics
University of Washington
http://wstein.org

William Stein

unread,
Sep 8, 2009, 12:13:15 PM9/8/09
to mpm...@googlegroups.com
On Tue, Sep 8, 2009 at 9:12 AM, William Stein<wst...@gmail.com> wrote:
> On Tue, Sep 8, 2009 at 9:05 AM, denis joubert<denis....@gmail.com> wrote:
>> Could Python long store 6400bits and upper ?
>
> Yes, with no trouble.  They are arbitrary precision.

Here is how long conversion from GMP to Python long takes:

sage: n = 2**6400-1 # gmp by default in sage
sage: timeit('long(n)')
625 loops, best of 3: 2.71 µs per loop

sage: m = long(n)
sage: timeit('ZZ(m)')
625 loops, best of 3: 3.46 µs per loop

William

William Stein

unread,
Sep 8, 2009, 7:54:06 PM9/8/09
to casevh, mpm...@googlegroups.com, Gonzalo Tornaria
On Tue, Sep 8, 2009 at 4:38 PM, casevh<cas...@gmail.com> wrote:

>
>
> On Sep 7, 5:00 pm, William Stein <wst...@gmail.com> wrote:
>> On Mon, Sep 7, 2009 at 4:50 PM, Fredrik
>>
>> Johansson<fredrik.johans...@gmail.com> wrote:

>>
>> > On Mon, Sep 7, 2009 at 7:32 PM, denis joubert<denis.joub...@gmail.com> wrote:
>> >> I need to binary files not in real format but like the gmp c function
>> >> mpz_import convert any files into big integers.
>>
>> > It sounds like what you need is a function to convert a byte array
>> > into a Python long?
>>
>> I think there is very complicated highly optimized code somewhere in Sage
>> (http://sagemath.org) for conversion between Python longs and GMP
>> mpz_t's.  If this is what you need, it could be dug up.
>
> I maintain gmpy, the Python wrapper for GMP or MPIR, and I would like
> to look at including Sage's conversion code. I'm currently using
> mpz_import and mpz_export and am always looking for faster code.
>
> Case Van Horsen
>

Hi,

Look in the src and include directories here:

http://sage.math.washington.edu/home/wstein/build/sage-4.1.1/devel/sage/c_lib/

> Would there be any licensing issues since gmpy is LGPL2+?

Yes, there is a problem since the code is licensed GPL2+. However,
I've cc'd the author -- Gonzalo Tornaria -- and I bet he would be
willing to contribute a LGPL2+ version of the code to gmpy. I
certainly endorse that!

-- William

Case Vanhorsen

unread,
Sep 8, 2009, 10:33:29 PM9/8/09
to Gonzalo Tornaria, William Stein, mpm...@googlegroups.com
On Tue, Sep 8, 2009 at 6:58 PM, Gonzalo
Tornaria<torn...@math.utexas.edu> wrote:
> I can definitely contribute the mpz<-->pylong conversion code to gmpy
> under LGPL2+.
>
> You want to look at the files mpn_pylong.c and mpz_pylong.c. The high
> level interface (mpz <-> pylong) is given by functions
> mpz_get_pylong() and mpz_set_pylong(). There's also a function
> mpz_pythonhash() which computes a python-compatible hash of mpz
> integers.
>
> The function mpz_get_pyintlong() is a variation of mpz_get_pylong()
> written by David Harvey --- you should ask him about that one.
>
> Note also that the base conversion code used in mpn_get_pylong() is
> based on GMP code --- I think GMP was LGPLv2+ at the time, so it
> should be fine.
>
> Let me know if you have any issues with using this code in gmpy.
>
> If you use the code, can you add a comment to the files along these lines:
>
> """
> Originally written for sage (http://sagemath.org) -- if you improve
> these functions, please contribute them back to sage by posting to
> sage-...@googlegroups.com or sending me an email.
> """
>
> Best, Gonzalo
>
Thanks for contributing the code. I'll will definitely let you know
what changes need to be made for compatibility with Python 3.x and the
30-bit limb size that is used for 64-bit Python 2.7 and 3.1 builds.

Case

Reply all
Reply to author
Forward
0 new messages