Questions tagged [mpmath]

Mpmath is a Python library for arbitrary-precision floating-point arithmetic.

Mpmath is a pure-Python library for multiprecision floating-point arithmetic. It provides an extensive set of transcendental functions, unlimited exponent sizes, complex numbers, interval arithmetic, numerical integration and differentiation, root-finding, linear algebra, and much more. Almost any calculation can be performed just as well at 10-digit or 1000-digit precision, and in many cases mpmath implements asymptotically fast algorithms that scale well for extremely high precision work. 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 or if mpmath is imported from within Sage.

Mpmath is free (BSD license) and easy to install or include in other software due to being written entirely in Python with no additional required dependencies. It runs on Python 2.5 or higher, including Python 3.x. It can be used as a library, interactively via the Python interpreter, or via SymPy which uses it for numerical evaluation of symbolic expressions. Mpmath is also a standard component of Sage which uses it for special function evaluation.

If matplotlib is available, mpmath also provides a convenient plotting interface.

169 questions
2
votes
0 answers

Can we make this faster ? Moschopoulos algorithm

I implemented the algortihm of Moschopoulos that gives the density and c.d.f of a sum of gamma random variables. A C++ implementation exists in the dcoga R package, but i need mine to handle arbitrary precision numbers through the mpmath…
lrnv
  • 1,038
  • 8
  • 19
2
votes
2 answers

Passing arguments to mpmath quad integration

I'm integrating some pretty nasty functions, and scipy.integrate.quad is not handling the situation very well. I was planning to use mpmath.quad with tanh-sinh method, but I need to pass some arguments to the function that is being calculated, like…
Ivan
  • 19,560
  • 31
  • 97
  • 141
2
votes
1 answer

Does mpmath support very small numbers? (Python)

I'm trying to solve a numerical maths problem, and for that I need python to handle very small numbers. For that, I installed mpmath. It doesn't quite work as desired though. Mpmath is able to handle 1e-300 but not 1e-400 (10^-300 but not 10^-400…
2
votes
2 answers

TypeError: Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe'

I need to make an integral of the type g(u)jn(u) where g(u) is a smooth function without zeros and jn(u) in the Bessel function with infinity zeros, but I got the following error: TypeError: Cannot cast array data from dtype('O') to dtype('float64')…
Diogo
  • 64
  • 2
  • 7
2
votes
1 answer

Problem with mpmath under Python 3.8, but OK under 2.7

The following lttle program fails using Python 3.8, but is OK under 2.7: from mpmath import mpf, nsum def z(n): x = [mpf(1) for k in range(1,n)] return 99 print (nsum(z, [2,2])) The code looks odd, as it is pared down from a rather larger…
user2307360
  • 41
  • 1
  • 2
2
votes
1 answer

Finding the Roots of Chebyshev's polynomials in python

I want to find the roots of the Chebysev polynomial of any order using Python. I have seen similar threads for Legendre polynomials. However, I have constructed my polynomials using the method defined here as import numpy as np import sympy as sp…
slow_learner
  • 337
  • 1
  • 2
  • 15
2
votes
0 answers

Why does mpmath-function "diff" produce error AttributeError: 'MPContext' object has no attribute 'difference'?

I wanted to differentiate a function by using mpmaths function diff. In order to just try if everything works on a simple example, I used an example being described in the documentary. But it threw the error "AttributeError: 'MPContext' object has…
Johnny A
  • 21
  • 3
2
votes
0 answers

Special function definition issue in Python vs Mathematica

I have a Mathematica code that calculates the 95% confidence intervals of a Cumulative Distribution Function (CDF) obtained from a specific Probability Distribution Function (PDF). The PDF is ugly, as it contains an Hypergeometric 2F1 function, and…
user11242437
2
votes
1 answer

Python's scipy.integrate.quad with complex integration bounds

I'm calculating the upper incomplete gamma function using Python's mpmath.gammainc: import numpy as np from mpmath import gammainc z = 0 # define power of t as t^(-1) a = 0.5+0.4j # integral lower limit b = np.inf # integral upper limit myfun…
Medulla Oblongata
  • 3,771
  • 8
  • 36
  • 75
2
votes
1 answer

Speedups for arbitrary decimal precision singular value decomposition and matrix inversion

I am using mpmath for arbitrary decimal precision. I am creating large square matrices (30 x 30 and 100 x 100). For my code, I am executing singular value decomposition and matrix inversion using mpmath's built-in packages. My problem is that…
Paul Terwilliger
  • 1,596
  • 1
  • 20
  • 45
2
votes
1 answer

Using mpmath and scipy.stat together

I'm trying to use the mpmath library, which provides arbitrary precision arithmetic, and the scipy.stats library together: from mpmath import mpf from scipy.stats import norm x = mpf(3) # arbitrary precision float y = norm.cdf(x) However,…
Umer
  • 23
  • 2
2
votes
1 answer

How can I install mpmath as an external library for Blender?

I'm interested in trying out sympy with Blender (v2.76, Python 3.4.2 Console, Windows 8.1). I followed this answer from Blender SE, downloaded sympy as a ZIP from Githib, and moved the sympy folder to C:\Program Files\Blender…
DragonautX
  • 860
  • 1
  • 12
  • 22
2
votes
1 answer

Cannot create mpf from a complex number when calling qr_solve

I make the following imports: from sympy.matrices import Matrix as sy_matrix import sympy.mpmath as sy_mp Create my matrices like this: sysMat = sy_matrix([[0.0]*sz1]*sz2) resVec = sy_matrix([[0.0]]*sz2) .Populate then with python complex…
Jibbity jobby
  • 1,255
  • 2
  • 12
  • 26
2
votes
1 answer

using python's mpmath module how do I initialize mpf with a hexadecimal number?

I would like to load a very large hexadecimal number into one of mpmath's mpf objects. I tried from mpmath import mp, mpf a = mpf('0x100') # starting small here but it malfunctioned: ValueError: could not convert string to float: '0x100' Simply…
Mutant Bob
  • 3,121
  • 2
  • 27
  • 52
2
votes
2 answers

Create mpf from array

I'm trying to use fsolve in combination with the mpmath package. However, I get the error cannot create mpf from array([mpf('1.0')], dtype=object). Here is a minimal example reproducing the error. For this example, I technically do not need the…
Nigu
  • 2,025
  • 2
  • 22
  • 31
1 2
3
11 12