Pages

Monday, June 27, 2011

Arbitrary Precision using Python Mpmath

Mpmath is great, powerful and easy to use to calculating equation that involve floating point.
I found several article that could be used to find minimal setting for arbitrary precision, enough the chat, let my code do the talk..

Software that used:
  • python ver 2.6.5 in ubuntu 10.04 64 bit
  • python EPD(Enthought Python Distribution) ver 7.0-2 32 bit in windows 7 64 bit, no default mpmath installation under python epd, but its available via sympy(included ver 0.14 mpmath)
  • python mpmath ver 0.17, http://code.google.com/p/mpmath/
  • gmpy -> python binding for GMP
  • sage version 4.7 64bit for ubuntu10.04  http://www.sagemath.org/

Below are 2 equations that could be used for searching significant setting for mpmath arbitrary precision.
1. Interval Arithmetic: Python Implementation and Applications by Stefano Taschini in SciPy2008_proceedings.pdf
GMPY Backend

SAGE Backend


Python Backend


2. Why and how to use arbitrary precision by Kaveh R. Ghazi, Vincent Lefèvre, Philippe Théveny, Paul Zimmermann in cise.pdf
#!/usr/bin/python
import sys
# using mpmath for arbitrary precision,
# sympy for showing the equation in pretty way
if sys.platform == 'linux2':
import mpmath as mp, sympy as sm
elif sys.platform == 'win32':
import sympy as sm
mp = sm.mpmath
# just for write shorter code
f = mp.mpf
ms = mp.nstr
pr = sm.pprint
print 'Equation for testing arbitrary precision'
# show the equation with pretty printing
pr(173746*sm.sin(1e22) + 94228*sm.log(17.1) - 78487*sm.exp(0.42))
# Expected value from the reference 'Why and how to use arbitrary precision'
# by Kaveh R. Ghazi, Vincent Lefevre, Philippe Theveny, Paul Zimmermann
expect = '-1.3418189578296195e-12'
# show default setting for mpmath
print '\n Default', mp.mp, '\n'
print 'Expected value are', expect, '\n'
print 'Searching prec value, to match the expected value', expect
# calc -> calculate ; cpr -> for comparing with previosly calculated
calc = cpr = 0
# loop for searching prec setting that matched
# expected value -1.341818958e-12'
# by calculating f_1 function
while calc != expect:
mp.mp.prec +=1
a, b, c = mp.sin(1e22), mp.log(f('17.1')), mp.exp(f('0.42'))
r = f(173746)*(a) + f(94228)*(b) - f(78487)*(c)
calc = mp.nstr(r, n=17)
if cpr != calc:
cpr = calc
# uncomment below to find out when the calculated is changed
# than previous when prec is changed
#print cpr, mp.mp
print 'Found Significand', mp.mp
print 'Result using above setting =', calc
print 'mpmath backend', mp.libmp.BACKEND #show mpmath backend
GMPY Backend




SAGE Backend


Python Backend



Conclusion
  • different BACKEND in mpmath did not effect in result and mpmath.mp context(setting for arbitrary precision), just gives an performance effect(sage faster, than gmpy, last python) 
  • mpmath prec value that could be gives most precision is not less than 128 bit (mpmath.mp.prec=128)
  • creating a mpf object is better to converted into string data type first( mp.mpf('0.1') or mp.mpf(str(0.1)))
  • better doing a arithmetic rational operation when possible see first code that solved using sympy

No comments:

Post a Comment