On Nov 18, 2007 2:18 PM, Ondrej Certik <[EMAIL PROTECTED]> wrote:

> Both MPFR and SymPy (mpmath) is in SAGE, so you can play with both of
> them from Python in SAGE. So it might be interesting for you Fredrik
> to compare mpmath to mpfr.

I'm well aware of MPFR :-) The goals of mpmath are very similar:
arbitrary precision, rigorous rounding so you can implement interval
arithmetic strong enough for testing inequalities in a computer
algebra system.

Unfortunately, the rigorous rounding only works for the +, -, *, /
operations so far in mpmath; getting it right for more advanced
functions will require (1) careful error analysis of the truncation
and rounding errors in all algorithms, (2) using Ziv's method (as
described in Paul Zimmerman's presentation) to handle rounding in
special cases.

(2) should be fairly easy to implement generically. (1) is much
harder. The MPFR documentation is very helpful here, but I can't use
many of its error bounds directly since MPFR often uses a different
algorithm. In mpmath, I prefer (with a few exceptions) to use the
simplest possible algorithm that is reasonably fast, not only because
I want to keep the code simple, but also because simpler algorithms
are often faster when implemented in Python. I also don't mind using
looser error bounds when they are simpler, and that will simplify
things.

Advantages of mpmath:
* It runs in Python, without compilation (or chance of miscompilation).
* Shorter and easier-to-read code.
* Supports arbitrary-sized exponents.
* Has some support for complex numbers.

Advantages of MPFR:
* Much faster.
* Overall has more features.
* Is mature, well-tested, and should be nearly bug-free (this can
definitely not be said of mpmath yet).
* Has a bigger and more skilled development team.

Perhaps you'll be interested in a speed comparison as well. I haven't
yet had the opportunity to test both MPFR and mpmath on the same
computer, but the numbers from the presentation could be used as
reference. Those were computed using a 1.8 GHz Athlon, and my computer
is a 2.2 GHz Athlon. Here are my results for mpmath (I hopefully
didn't make any stupid mistakes):

op, digits, time in ms / time with psyco (times slower than MPFR / with psyco)

x*y, 100, 0.0093 / 0.0048 (19x / 10x)
x*y, 10000, 5.25 (11x)

x/y, 100, 0.017 / 0.010 (17x / 10x)
x/y, 10000, 40 (33x)

sqrt(x), 100, 0.033 / 0.015 (24x / 11x)
sqrt(x), 10000, 26 (32x)

exp(x), 100, 0.095 / 0.070 (6x / 4x)
exp(x), 10000, 1204 (22x)

log(x), 100, 0.21 / 0.14 (7x / 5x)
log(x), 10000, 1319 (39x)

sin(x), 100, 0.20 / 0.12 (9x / 5x)
sin(x), 10000, 5983 (77x)

Note that due to the different processor speeds, mpmath is actually
probably a bit worse than these numbers indicate. Adding correct
rounding to exp, log and sin will also make them a bit slower in
mpmath (probably not much). But on average, in the area of 100 digits
which is typically most relevant for CAS operations, mpmath is fairly
consistently only about 10x slower than MPFR, and that is not too bad
for pure Python. (Interestingly, mpmath even seems to be faster than
NTL for exp and log.) From a practical point of view, with 1K-100K
ops/s at 100 digits (including elementary functions), mpmath is fast
enough to evaluate any reasonably complicated single expression at
interactive speed, which covers most use cases in SymPy.

The relatively good speed in mpmath is due to the fact that you can
implement arbitrary-precision arithmetic using Python ints instead of
digit arrays, and most of the work is done in the Python C core. For
comparison, I once tried to write an FFT in Python and never even got
it within 100x of the speed of a C FFT. Matrix multiplication is
similar. Although I generally think SymPy should be self-contained,
and I think mpmath will be good enough for arbitrary-precision
numerical calculations that we won't need an interface to MPFR (that
is of course my biased opinion), we should make sure that it is easy
to interact with SciPy for low-precision array computations.

Fredrik

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sympy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to