I am working on a project and I would like to make sure I use Legendre function correctly. I've made a simple comparision between mpmath and Mathematica and the results are different:
for mpmath in python:
import mpmath print(mpmath.legenp(-0.5, 1, 1.04018069))
the outcome is:
(1.323429779732253753588079e-36 + 0.03499697660646475313011583j)
And for Mathematica:
LegendreP[-1/2, 1, 1.04018069]
and the result is:
-2.14295*10^-18 + 0.034997 I
It seems they have the same imaginary part but different real part.
I want to know where is the issue and how should I use the Legendre function of first and second kind, of degree one and half integer order.
I am working on a project and I would like to make sure I use Legendre function correctly. I've made a simple comparision between mpmath and Mathematica and the results are different:
for mpmath in python:
import mpmath print(mpmath.legenp(-0.5, 1, 1.04018069))
the outcome is:
(1.323429779732253753588079e-36 + 0.03499697660646475313011583j)
And for Mathematica:
LegendreP[-1/2, 1, 1.04018069]
and the result is:
-2.14295*10^-18 + 0.034997 I
It seems they have the same imaginary part but different real part.
I want to know where is the issue and how should I use the Legendre function of first and second kind, of degree one and half integer order.
1 Answer
Reset to default 0Firstly, I would welcome you to the issue of computer precision in doing calculations. As commented by Martin Brown below your question, 1e-36 is fairly close to 0 as far as double precision computing goes.
Secondly, I would suggest you consider implementing a common practice in many languages where some "tolerance" is used to decide if something is sufficiently close to zero that we consider it as such. mpmath.chop(x, tol=None) is a function used to do just this.
Furthermore, mpmath has documentation which discusses setting or changing the precision which is worth a read.
Lastly, to address your question of "how to use Legendre function correctly", this really depends on your use case, to which you would need to provide more detail. You may only need 3 or 4 decimal places, in which case the mpmath.chop(x, tol=1e-4) route would work well enough.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744305384a4567704.html
0+iy
and that mpmath has by sheer good luck got a considerably better cancellation than Mathematica. Both are actually rather good approximations to zero in double precision arithmetic but 1e-36 is much better. Throw it at a symbolic algebra system to get confirmation of this hypothesis (guess). – Martin Brown Commented Mar 22 at 20:13