Home  |  About Us  |  Link To Us  |  FAQ  |  Contact

# Mpmath 0.17

Company: Fredrik Johansson
Date Added: August 19, 2013  |  Visits: 203

mpmath is a replacement for Python's float/complex types and math/cmath modules with unlimited precision and exponent sizes. The mpmath software is written entirely in Python without any external dependencies and hence runs almost anywhere, without the need for compilation.<br /><br />To install, unpack the mpmath archive and run<br /><br />python setup.py install<br /><br />Documentation and usage:<br /><br />Import mpmath with<br /><br />from mpmath import *<br /><br />This provides the classes mpf and mpc which work analogously to Python's float and complex types:<br /><br />>>> mpf(2) / mpf(3)<br />mpf('0.66666666666666663')<br /><br />>>> mpc(0, -1)<br />mpc(real='0', imag='-1')<br /><br />>>> mpf(-0.6) ** mpf(-0.2)<br />mpc(real='0.89603999408558288', imag='-0.65101116249684809')<br /><br />For prettier output (that also hides small rounding errors), use print or str():<br /><br />>>> print mpf(2) / mpf(3)<br />0.666666666666667<br /><br />>>> print mpc(1+2j)**0.5<br />(1.27201964951407 + 0.786151377757423j)<br /><br />The precision is controlled by the properties mpf.prec (number of bits) and mpf.dps (number of decimals). These properties are linked, so changing one automatically updates the other to match. Setting prec or dps changes the precision at which all operations are carried out and the number of digits to display when printing numbers. The default is<br />prec=53 and dps=15, the same as Python floats.<br /><br />>>> mpf.dps = 30<br />>>> mpf(2) / mpf(3)<br />mpf('0.66666666666666666666666666666663')<br />>>> print _<br />0.666666666666666666666666666667<br />>>> mpf.dps = 15 # restore to default<br /><br />You can create mpfs and mpcs from Python numbers, or combine mpfs and mpcs with Python numbers in arithmetic operations, but be aware that regular Python floats only have finite precision. To initialize an mpf with a full-precision value, use a string:<br /><br />>>> mpf(0.1)<br />mpf('0.10000000000000001') # same accuracy as float<br />>>> mpf.dps = 50<br />>>> mpf(0.1)<br />mpf('0.1000000000000000055511151231257827021181583404541016') # junk<br /><br />>>> mpf('0.1')<br />mpf('0.1000000000000000000000000000000000000000000000000001') # ok<br /><br />The following standard functions are available and support both real and complex arguments:<br /><br />sqrt, exp, log, power, cos, sin, tan, cosh, sinh, tanh,<br />acos, asin, atan, acosh, asinh, atanh<br /><br />Example:<br /><br />>>> mpf.dps = 15<br />>>> print cos(1)<br />0.540302305868140<br />>>> mpf.dps = 50<br />>>> print cos(1)<br />0.54030230586813971740093660744297660373231042061792<br /><br />Some less-common functions are also available: gamma (gamma function), factorial, erf (error function), lower_gamma/upper_gamma (incomplete gamma function) and zeta (Riemann zeta function).<br /><br />Finally, the convenience functions hypot and atan2 are available (defined for real numbers only).<br /><br />The constants pi, e, and cgamma (Euler's constant) are available as special objects that behave like mpfs but whose values automatically adjust to the precision.<br /><br />>>> mpf.dps = 15<br />>>> print pi<br />3.14159265358979<br />>>> mpf.dps = 50<br />>>> print pi<br />3.1415926535897932384626433832795028841971693993751<br /><br />>>> mpf.dps = 15<br />>>> e**(-pi*1j)<br />mpc(real='-1', imag='-1.2289836075083701E-16')<br />>>> mpf.dps = 50<br />>>> e**(-pi*1j)<br />mpc(real='-1', imag='1.0106 [...] E-51')<br /><br />Directed rounding is partially implemented. For example, this computes and verifies a 15-digit approximation interval for pi:<br /><br />>>> mpf.dps = 15<br />>>> mpf.round_down(); pi1 = +pi<br />>>> mpf.round_up(); pi2 = +pi<br />>>> pi1<br />mpf('3.1415926535897931')<br />>>> pi2<br />mpf('3.1415926535897936')<br />>>> mpf.dps = 30<br />>>> pi1 < pi < pi2<br />True

 Requirements: No special requirements Platforms: Mac, Mac OSX Keyword: Bernoulli,  Bessel,  Carlsen,  Chebyshev,  Clausen,  Computation,  Coulomb,  Euler,  Exact,  Forms,  Hankel,  Jacobi,  Kelvin,  Large,  Legendre,  Polylogarithms,  Polynomials,  Stieltjes,  Struve,  Whittaker Users rating: 0/10