Is there a way to tell that mpmath is using gmpy?

286 views
Skip to first unread message

Christopher Small

unread,
Feb 8, 2013, 7:29:01 PM2/8/13
to mpm...@googlegroups.com
Greetings

I am using mpmath.hyp2f1 in a bioinformatics application which requires high precision arithmetic in order to avoid rounding issues with important input domains. The application requires quite a bit of repeated computation of this function, and we were hoping that setting up gmpy would significantly improve our performance. From the home page:

Mpmath internally uses Python's builtin long integers by default, but automatically switches to GMP/MPIR for much faster high-precision arithmetic if gmpy is installed

I first tried installing gmpy2, which lead to  the following error:

Traceback (most recent call last):
  File "beta_rat.py", line 7, in <module>
    import mpmath
  File "/home/matsengrp/python-local/lib/python2.7/site-packages/mpmath/__init__.py", line 5, in <module>
    from .ctx_fp import FPContext
  File "/home/matsengrp/python-local/lib/python2.7/site-packages/mpmath/ctx_fp.py", line 1, in <module>
    from .ctx_base import StandardBaseContext
  File "/home/matsengrp/python-local/lib/python2.7/site-packages/mpmath/ctx_base.py", line 3, in <module>
    from .libmp.backend import xrange
  File "/home/matsengrp/python-local/lib/python2.7/site-packages/mpmath/libmp/__init__.py", line 1, in <module>
    from .libmpf import (prec_to_dps, dps_to_prec, repr_dps,
  File "/home/matsengrp/python-local/lib/python2.7/site-packages/mpmath/libmp/libmpf.py", line 20, in <module>
    from .libintmath import (giant_steps,
  File "/home/matsengrp/python-local/lib/python2.7/site-packages/mpmath/libmp/libintmath.py", line 302, in <module>
    sqrtrem = gmpy.sqrtrem
AttributeError: 'module' object has no attribute 'sqrtrem'

Hunting around, I was able to find that only gmpy1 is supported with mpmath. While it's not the main point of my enquiry here, may I suggest clarifying this in the home page by doing s/gmpy/gmpy1/?

After removing gmpy2 and installing gmpy, I reran my program, and though the errors above went away, I didn't notice any performance boost. I would imagine that since it was (apparently) able to find gmpy2, mpmath probably is finding (and presumably using) gmpy1. Is there any way to tell whether this is the case? Is it possible that there are components of mpmath (such as hyp2f1) that aren't set up to use gmpy? Or is gmpy just not the silver bullet that I was hoping it would be?

I very much appreciate any feedback you might be able to provide in this matter.

Best Wishes

Christopher Small
Programmer and Systems Analysis
Computational Biology Program
Fred Hutchinson Cancer Research Center

Aaron Meurer

unread,
Feb 8, 2013, 8:41:23 PM2/8/13
to mpm...@googlegroups.com
Do "from mpmath.libmp import BACKEND". BACKEND will be 'python' if
it's using Python and 'gmpy' if it's using gmpy.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "mpmath" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mpmath+un...@googlegroups.com.
> To post to this group, send email to mpm...@googlegroups.com.
> Visit this group at http://groups.google.com/group/mpmath?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Fredrik Johansson

unread,
Feb 8, 2013, 8:44:36 PM2/8/13
to mpm...@googlegroups.com
Hi Christopher,
This problem should already be fixed in the git repository. I just
need to make a new release some time soon.

> After removing gmpy2 and installing gmpy, I reran my program, and though the
> errors above went away, I didn't notice any performance boost. I would
> imagine that since it was (apparently) able to find gmpy2, mpmath probably
> is finding (and presumably using) gmpy1. Is there any way to tell whether
> this is the case? Is it possible that there are components of mpmath (such
> as hyp2f1) that aren't set up to use gmpy? Or is gmpy just not the silver
> bullet that I was hoping it would be?

To see if gmpy is used, check mpmath.libmp.BACKEND.

gmpy does not speed up everything. I think it does not make much of a
difference for this function unless the precision is at least several
hundred digits. The sage backend should be somewhat faster, though.

Fredrik

Christopher Small

unread,
Feb 11, 2013, 1:27:31 PM2/11/13
to mpm...@googlegroups.com
Thank you both for your help. I can confirm that gmpy is being loaded. Guess we'll just have to live with the speed on that.

Best

Chris Small
Reply all
Reply to author
Forward
0 new messages