Questions tagged [mpmath]
Mpmath is a Python library for arbitrary-precision floating-point arithmetic.
169
questions
0
votes
0
answers
38
views
Is sympy evalf() using binary or decimal arithmetic?
I'm working on a numerical evaluation using the sympy evalf() package. Consider the mpmath library's introduction to precision and accuracy https://mpmath.org/doc/current/technical.html, where on
...
0
votes
0
answers
27
views
numpy allclose vs mpmath almosteq
abs(a - b) <= (atol + rtol * abs(b))
vs
(abs(a - b) <= atol
or
abs(a - b) / max(abs(a), abs(b)) < rtol)
Docs: numpy.allclose, mpmath.almosteq -- Source: numpy, mpmath
How do these schemes ...
1
vote
1
answer
29
views
mpmath stopped working with the high precisions
I'm using jupyter notebook with anaconda on windows (reinstalled to the current version after several attempts). Somehow the mpmath library stopped working. Consider the following code
import mpmath ...
0
votes
0
answers
30
views
How to replace eval in python [duplicate]
I have the following code, where I'm using mpmath library for calculating different irrational numbers:
for i in range(1, len(self.layer_sizes) - 1):
z = np.dot(a, self.weights[i])
...
0
votes
0
answers
12
views
Has automatic differenation been implemented in mpmath library?
I'm learning about the numerical differentiation and is trying out some examples with mpmath library.
On it's documentation page, it suggested that the mpmath.diff used either
method
Supported ...
1
vote
1
answer
28
views
Python Sympy and mpmath give different results
I did an evaluation of mpmath and sympy and found inconsistencies, which I cannot resolve. I believe mpmath gives correct results. Please, can somebody help in resolving this?
import sympy as sp
...
0
votes
0
answers
35
views
How to calculate the Automatic differentiation of an arbitrary function in python
I'm using the mpmath library to compute a self defined function f(x) and needed to compute its higher order derivatives.
I found the Automatic differentiation on Wikipedia and found it useful.
# ...
0
votes
1
answer
61
views
comparing Accurrate Dot Product with mpmath
This post is not a repost, which is related solely to mpmath. But this is mainly related to the comparison between different dot product algorithms.
I have coded an accurate dot product in python in ...
0
votes
2
answers
48
views
Performing Dot Product using mpmath
I am looking for a correct implement of dot product using mpmath, but they give me different results, which I don't understand why.
sum = mp.mpf(0.) ...
0
votes
1
answer
21
views
mpmath library magnitude of number bug?
In the documentation of mpmath library page 8 it mentioned that
There is no restriction on the magnitude of numbers
>>> print(mpf(2)**32582657 - 1)
1.24575026015369e+9808357
and I checked ...
0
votes
0
answers
66
views
Fitting of mpmath.zeta using scipy.optimize.curve_fit with bounds encounters NaNs
This is a follow-up of my previous question. More information, including actual working example, can be found there. After doing some research I decided to post a new question rather than edit the ...
0
votes
1
answer
32
views
How to serialise an mpmath lambdified SymPy function containing Fresnel integrals?
I am using SymPy to generate some functions requiring the evaluation of Fresnel integrals and would like to save them to disk. When I sm.lambdify the expressions using the default ["scipy", &...
0
votes
1
answer
89
views
TypeError: cannot create mpf from x
I've checked multiple other answers for this error, but can't figure out why this is occuring.
from sympy.solvers import nsolve
from sympy import Symbol, log
x = Symbol('x')
u = nsolve(log(x/0.06) / (...
-1
votes
1
answer
101
views
How to find roots of a complex function with python?
I'm working with a complex function whose coordinate is omega. I formed it as follows:
from sympy import *
def alpha(n, rho, l):
return n**2 + (2*rho + 2)*n + 2*rho + 1
def beta(n, rho, l):
...
0
votes
0
answers
31
views
Bessel function squence integration
Newbie in python, wondering what would be the best strategy for the below
I have plotted the below function. z-n is an array (the n terms of a sequence in fact).
Theta and r are mesh grids
others are ...
4
votes
0
answers
75
views
Why is there a discrepancy in Chebyshev coefficients?
I am trying to calculate Chebyshev nodes to approximate the cos function in the interval [-1,1] with a polynomial of degree 4. In Python3, I used chebyfit from mpmath and chebyshev from numpy.
While I ...
0
votes
1
answer
59
views
Casting numpy longdouble to mpmath.mpf
How can I cast np.longdouble to mpmath.mpf?
import numpy as np
import mpmath
b = np.longdouble(1.0)
a = mpmath.mpf(b)
print (a)
0
votes
1
answer
184
views
How to handle very small numbers (<1e-15) in Python, passed to Scipy/Numpy functions?
I would like to compute some scipy functions, such as: scipy.special.gammaincinv (https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.gammaincinv.html#scipy.special.gammaincinv).
The ...
1
vote
1
answer
55
views
How to combine scipy interp1d with mpmath quadosc
I have a density function (from quantum mechanics calculations) to be multiplied with the spherical Bessel function with a momentum grid (momentum q 1d array, real space distance r 1d array, so need ...
1
vote
1
answer
76
views
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 ...
0
votes
0
answers
66
views
Why is vectorize so much slower with mpmath than just math
I'm trying to lower the error of my floating point calculation, to this end I wanted to use python mpmath to utilize the arbitrary precision of the library. But I notice that it is much slower than ...
1
vote
2
answers
178
views
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 ...
0
votes
0
answers
17
views
is there a way to retrieve the nodes automatically computed by mpmath.quad integration routine?
I am trying to calculate an integral with mpmath.quad. I basically have to calculate three moments of a distribution, call it f (pseudocode):
integrate(f(x)/x, 0, infinity)
integrate(f(x)*x, 0, ...
1
vote
1
answer
120
views
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 ...
1
vote
2
answers
224
views
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
...
0
votes
1
answer
66
views
cannot create mpf from array error for Elliptic Integrals
I'm working on this code that requires the use of elliptic integrals to solve an equation as a function of time. Most of the code is pretty straight forward but it runs into an error on the final ...
-1
votes
1
answer
36
views
why does mpmath throw an error when i trying to plug in the Chudnovsky algorithm?
im trying to use the Chudnovsky algorithm in mpmath for python and so i wrote this code:
from mpmath import *
import csv
mp.dps = 100; mp.pretty = True
x = mpf(426880)*sqrt(10005)*(nsum(lambda q: ((...
1
vote
1
answer
271
views
Efficient way to compute the confluent Hypergeometric function for large arrays (~ 10^8 points) with complex parameters
I am working on a project related to gravitational lensing, for which I need to evaluate the confluent hypergeometric function 1F1(a,b,z) for an array z of length ~ 10^8 complex points, a = 1+0.48j ...
0
votes
3
answers
343
views
How do I make mpmath generating more then 15 digits?
I cannot get mpmath to work as expected.
Here is my code:
import mpmath as mp
from mpmath import *
mp.dps=31
pi_30=mpf('3.141592653589793238462643383279')
for i in ...
0
votes
0
answers
140
views
RuntimeWarning: invalid value encountered in double_scalars and RuntimeWarning: overflow encountered in double_scalars
I am coding a newmark beta numerical dynamic equation solver but I
got this error:
d:\university\payan nameh\cssr python codes\newmarkbeta_method.py:46: RuntimeWarning: overflow encountered in ...
1
vote
0
answers
120
views
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. ...
0
votes
0
answers
115
views
Differing results of integrals from scipy.integrate and mpmath.quad
I am integrating a function to implement the functions in this paper and I observe different outputs from scipy.integrate.quad and mpmath.quad as per the following:
import mpmath as mp
from scipy ...
0
votes
1
answer
174
views
Erorr:can't create mpf from an array while 3D plotting a function with incomplete gamma function
I would like to display my complex function in the complex domain, but I have a problem. I get the following error message: cannot create mpf from array The code is simple and I have tried to show ...
0
votes
1
answer
176
views
Hexadecimal notation of square roots in Python - Sha-512
I am going over the description of Sha-512. It is mentioned that the initial hash value consists of the sequence of 64-bit words that are obtained by taking the fractional part of the first eight ...
1
vote
1
answer
238
views
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 ...
0
votes
1
answer
423
views
Why does numexpr.evaluate return an error when using functions from mpmath?
I've just started using numexpr's evaluate function and I've come across an annoying error.
I want it to print, per se, sin(10), and it does that just perfectly, but if I do sec(10), I get "...
2
votes
2
answers
196
views
Finding continued fractions of pi with Stern-Brocot tree
Im trying to use python to determine the continued fractions of pi by following the stern brocot tree. Its simple, if my estimation of pi is too high, take a left, if my estimation of pi is too low, ...
1
vote
1
answer
121
views
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 ...
0
votes
1
answer
134
views
Matplotlib fails with `TypeError` when plotting with mpmath
I have 2D data which is very small (of order e-500) so I cannot use numpy which I would like to draw as a pcolormesh. For instance,
import mpmath as mp
import matplotlib.pyplot as plt
import numpy as ...
2
votes
1
answer
339
views
How do you use the **kwargs argument in mpmath.findroot?
I am trying to find the roots of a function that has a lot of arguments using findroot in mpmath, but in this question, I am going to use a simple function.
from mpmath import mp
def func(x, **...
1
vote
1
answer
82
views
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 ...
2
votes
2
answers
534
views
How to generate seeded random floats of at least 20 decimal places in python?
I would like to generate seeded random floats of at least 20 decimal places in the range of -1 to 1.
import numpy as np
np.random.seed(2000)
np.random.uniform(low=-1, high=1, size=2)
This is what I ...
1
vote
1
answer
283
views
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, ...
1
vote
1
answer
499
views
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 ...
0
votes
0
answers
95
views
numpy.linalg.det of square array of mpmath.mpfs
I am having the following issue:
>>> import numpy as np
>>> import mpmath as mp
>>> v1 = [mp.mpf(1), mp.mpf(2)]
>>> v2 = [mp.mpf(3), mp.mpf(4)]
>>> np.dot(...
0
votes
1
answer
125
views
How to store the error/residue of mpmath.findroot for a particular input guess?
We have a specific set of equations whose solutions are highly dependent on the input guesses. If we use verbose given in the docs it displays a whole lot of extra information. We only want the error, ...
2
votes
1
answer
312
views
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),...
1
vote
1
answer
596
views
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....
1
vote
1
answer
115
views
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....
1
vote
0
answers
70
views
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!! ...