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.

133 questions
3
votes
1 answer

How to evaluate a numpy array inside an mpmath fuction?

I get an error when I try to use a numpy array inside mpmath function, this example fails when it reaches the line: C = (f*L/D) + 2*mp.log(P1/P2) Where P1 is an array. With the error: cannot create mpf from array([**P1_array**]) I'm aware of this…
user3408085
  • 110
  • 8
3
votes
4 answers

How can I build a polynomial lambda function from an list of coefficients?

I have an list of coefficients that correspond to a polynomial expression, ie: [1,2,0] corresponds to x^2 + 2x + 0. I would like to put an arbitrary length array of these coefficients into a lambda function. Specifically, I am using mpmath and I…
Chris
  • 31
  • 1
  • 2
3
votes
1 answer

Laplace inverse in Python with mpmath

I want to use "DE HOOG" algorithm for numerical Laplace inverse transform. I want to use the "mpmath" package and I installed it from the link: https://github.com/klkuhlm/mpmath Lets say I need to find the inverse Laplace transform of the below…
Jafar
  • 63
  • 5
3
votes
2 answers

Convert from mpf to Sympy Float without losing precision

Is there any way to do this? For example in the code below I lose precision: >>> from sympy import * >>> from sympy.mpmath import * >>> mp.dps = 50 >>> a = mpf('1.0')/mpf('3.0') >>>…
Peter Bingham
  • 1,105
  • 2
  • 8
  • 22
3
votes
2 answers

Access to mpmath module in sympy (python)

I am new to sympy and still naive about python.... I wanted to solve a trigonometric equation, to find its zeroes. (Once I have syntax, then I will use a more complex function.) I cannot find the right syntax yet. Here is what I tried at the iPython…
jal
  • 73
  • 6
3
votes
1 answer

How do arbitrary-precision libraries like mpmath evaluate simple trigonometric functions?

I ask for a brief explanation, pointing out the various acceleration methods involved. This is just for mere curiosity. For example the mpmath website tells that the exponential function formula is used for operations in the complex plane, but for…
user2464424
  • 1,456
  • 1
  • 12
  • 25
3
votes
3 answers

alternatives or speedups for mpmath matrix inversion

I'm writing some code in python that requires frequently inverting large square matrices (100-200 rows/colums). I'm hitting the limits of machine precision so have started trying to use mpmath to do arbitrary precision matrix inversion but it is…
user2153813
  • 93
  • 1
  • 6
2
votes
1 answer

How to optimise the numerical evaluation of a SymPy integral?

I'm somewhat of a newbie to SymPy and was hoping someone could point out ways to optimise my code. I need to numerically evaluate a somewhat involved expression with very high decimal places (150–300), and it is taking 30 seconds or longer per…
Thejasvi
  • 31
  • 5
2
votes
0 answers

Inverse Laplace Transformation of incomplete gamma function in julia

Recently, I have a problem in julia related with inverse laplace. Here is the code that I write in julia. using SpecialFunctions using InverseLaplace value = Talbot(u -> gamma_inc(1,u,2)[2], 80) value(2) This code make some error message…
이민호
  • 21
  • 1
2
votes
3 answers

Is there a multiple integrator in Python providing both variable integration limits (like scipy) and high precision (like mpmath)?

I can use scipy quad and nquad for a quadruple integration involving variable integration limits. The problem is that the default precision used raises an Error when the tolerance requested cannot be achieved. With mpmath integrator, I can define…
gerryD
  • 43
  • 7
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
  • 840
  • 4
  • 14
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
  • 49
  • 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
  • 93
  • 1
  • 1
  • 12
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
1
2
3
8 9