简体   繁体   English

Mpmath 超几何函数对高精度和低精度表现出不同的行为

[英]Mpmath hypergeometric function shows different behaviour for high & low precision

For one of my projects, I need to repeatedly evaluate an expression involving the general hypergeometric function.对于我的一个项目,我需要反复计算一个涉及一般超几何函数的表达式。 While SciPy does not support the general HypGeo function, MPMath does.虽然 SciPy 不支持一般的 HypGeo 功能,但 MPMath 支持。 However, using mp.hyper(..) is very time consuming.但是,使用mp.hyper(..)非常耗时。 So instead I decided to use their fast precision library function fp.hyper(..) .所以我决定使用他们的快速精度库函数fp.hyper(..) Unfortunately the behaviour seems to be completely different.不幸的是,行为似乎完全不同。 My example below:我的例子如下:

from mpmath import mp, fp
from math import sin, cos, pi

H = 0.2
k = 2

A = 4 * sqrt(H) / (1 + 2 * H)
B = 4 * pi / (3 + 2 * H)
C = H/2 + 3/4


f_high = lambda t: (B * k * t * sin(pi * k) *
                   mp.hyper([1], [C+1/2, C+1], -(k*pi*t)**2) +
                   cos(pi * k) * mp.hyper([1], [C, C + 1/2],
                   -(k*pi*t)**2)) * A * t**(H + 1/2)


f_low = lambda t: (B * k * t * sin(pi * k) *
                   fp.hyper([1], [C+1/2, C+1], -(k*pi*t)**2) +
                   cos(pi * k) * fp.hyper([1], [C, C + 1/2],
                   -(k*pi*t)**2)) * A * t**(H + 1/2)

使用 fp.plot(f_high,[0,5

通过 fp.plot(f_low,[0,5

The first plot shows fp.plot(f_high,[0,1]) , the second one fp.plot(f_low,[0,1]) .第一个图显示fp.plot(f_high,[0,1]) ,第二个图fp.plot(f_low,[0,1]) In case anyone is wondering: The functions look ugly but one is a copy of the other, just mp replaced by fp , so there is no chance they differ in any other way.如果有人想知道:这些函数看起来很丑,但一个是另一个的副本,只是mp替换为fp ,因此它们不可能有任何其他区别。

I also plotted it in Mathematica and the picture is more like the upper one (high precision).我也在 Mathematica 中绘制了它,图片更像上面的(高精度)。

Looks like there is a mistake with the implementation of the fp.hyper function, right?貌似fp.hyper函数的实现有问题吧?

The documentation for fp says (emphasis added): fp文档说(强调):

Due to intermediate rounding and cancellation errors, results computed with fp arithmetic may be much less accurate than those computed with mp using an equivalent precision (mp.prec = 53), since the latter often uses increased internal precision.由于中间舍入和抵消误差,使用 fp 算术计算的结果可能比使用等效精度 (mp.prec = 53) 使用 mp 计算的结果准确得多,因为后者通常使用更高的内部精度。 The accuracy is highly problem-dependent: for some functions, fp almost always gives 14-15 correct digits;准确性与问题高度相关:对于某些函数,fp 几乎总是给出 14-15 个正确的数字; for others, results can be accurate to only 2-3 digits or even completely wrong .对于其他人,结果可能准确到只有 2-3 位数,甚至完全错误 The recommended use for fp is therefore to speed up large-scale computations where accuracy can be verified in advance on a subset of the input set, or where results can be verified afterwards.因此,fp 的推荐用途是加速大规模计算,其中可以提前在输入集的子集上验证准确性,或者可以在之后验证结果。

If fp were just a drop-in replacement for mp with faster computation and no downside, there would be no reason for mp to exist.如果fp只是一个简易替换为mp具有更快的计算,没有缺点,就没有理由mp存在。 In this case, it appears that fp is not suited for your task, so you'll have to use mp .在这种情况下, fp似乎不适合您的任务,因此您必须使用mp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM