admin管理员组文章数量:1405311
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.
本文标签: pythonLegendre function comparision between mpmath and MathematicaStack Overflow
版权声明:本文标题:python - Legendre function comparision between mpmath and Mathematica - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744305383a2599785.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