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
1
vote
1 answer

UDF in xlwings calculating with defined precision using Python Decimal or mpmath

I am trying to use UDF in xlwings and I want to achieve required precision as both libraries Python Decimal and mpmath allows for this. I am calculating 2 ^ 0.5 with following functions: @xw.func def fce_2(x, y): import decimal from decimal…
Skip
  • 21
  • 1
1
vote
2 answers

Polynomial multiplication with arbitrary precision

I can make a polynomial and square it with: from numpy.polynomial import Polynomial P=Polynomial([1,2,3]) P**2 I will also need to be able to access individual coefficients and be able to truncate polynomials. These are both supported by…
Simd
  • 19,447
  • 42
  • 136
  • 271
1
vote
1 answer

Using Python to find best square approximation of Piecewise Functions?

Given a parabola, $f(x)=10(x-1)^2-1,x\in \Omega =[1,2]$, find the best approximation in the space of all linear functions. The Python code is as follows: import sympy as sym import numpy as np import matplotlib.pyplot as plt from mpmath import…
1
vote
2 answers

How to use solver in mpmath.findroot

I want to find the root of a 2D nonlinear equation. I wrote: res = mpm.findroot(f=func1, x0=x01, tol=1.e-5, solver='MDNewton', J=JAKOB1) but I get this message: ValueError: could not recognize solver As per the documentation of findroot, MDNewton is…
1
vote
0 answers

Numerical instability in the inverse Laplace transform

I have a problem with Laplace inversion and my function is not numerically stable for the Laplac inverse, but I do not understand the cause of this problem. Here is my code and graph of this problem. Does anyone have any idea? I have also tried all…
Ali AlCapone
  • 121
  • 7
1
vote
1 answer

Problem using lambda expression to be integrated using quad from mpmath

I am trying to integrate an oscillatory expression with mpmath.quad. The integral only has one variable but I need to evaluate it in a 2D space. I need the resulting function from the integral along kx to be evaluated for a group of coordinated in x…
1
vote
1 answer

How would we represent the "Riesz function" in python (not using Sympy)?

I want to trace a tracking function known as the Riesz function in honor of the creator: after a search I found this mpmath library that can help me with the plot function, zeta function and euler's number but I don't know how to put it all…
larous25
  • 149
  • 7
1
vote
1 answer

ijv sparse matrix representation for mpmath matrix (coo_matrix equivalent)

Apparently coo_matrix does not work properly for mapmath matrix. Is there any equivalent implementation or simple algorithm to implement for mpmath? import mpmath import numpy as np from scipy.sparse import coo_matrix A = np.diag([1]*5) A_coo =…
Abolfazl
  • 1,047
  • 2
  • 12
  • 29
1
vote
1 answer

Why does installing sagemath improve the performance of mpmath in python?

I noticed that the performance of mpmath, as oddly as it sounds, depends on whether sagemath is installed or not, regardless of whether the sage module is loaded in the current session. In particular, I experienced this for operations with multiple…
MannyC
  • 288
  • 2
  • 11
1
vote
1 answer

mpmath slower than decimal in simple multiplication

I am wondering why mpmath is so much slower than decimal when doing the same operation for the same precision settings. from decimal import * from mpmath import * import timeit from decimal import Decimal as dc from mpmath import mpf import sys #…
1
vote
1 answer

Generalised hypergeometric function for numpy

I'm looking to fit some data to the hypergeometric function. I'm using the generalised hypergeometric function given in mpmath, hyper. I'm trying to convert it to work with curve_fit using np.frompyfunc. When I do np_hyp =…
Sam
  • 11
  • 1
1
vote
1 answer

Python: Multiprocessing running portion of code that it shouldn't

I was sort of playing around with multiprocessing and different math libraries to calculate pi and wanted to know how much faster was it with or without multiprocessing by implementing time.perf_counter(). mp.Pool maps 20 threads as the same with my…
1
vote
0 answers

Integrale value decreases when I increase range of integration using mpmath (positive function)

I am using mpmath library on python to compute this integral : The issue is that when I increase the range of integration, my integral value slowly decreases to 0.. while the integrand is positive!! I give you some values : integration range :…
Sarah
  • 37
  • 4
1
vote
1 answer

python mpmath nstr strip zeros not

How can I let the python mpmath.nstr function keep all the trailing zeros for mpmath.mpf('0')? It looks that the strip_zeros optional argument does not work for mpmath.mpf('0') as seen in the following example: Python 3.8.5 (default, Jul 28 2020,…
norio
  • 3,652
  • 3
  • 25
  • 33
1
vote
0 answers

Mpmath : quadprog or lsei equivalent?

I want to solve a quadratic program with mpmath precision. The problem can be stated as a least square with linear equality and inequality constraints, or as quadratic program with linear equality and inequality constraints. I am aware of the LSEI…
lrnv
  • 1,038
  • 8
  • 19