本文整理汇总了Python中mpmath.mpf方法的典型用法代码示例。如果您正苦于以下问题:Python mpmath.mpf方法的具体用法?Python mpmath.mpf怎么用?Python mpmath.mpf使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在模块mpmath的用法示例。
在下文中一共展示了mpmath.mpf方法的24个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: compute_d
点赞 6
# 需要导入模块: import mpmath [as 别名]
# 或者: from mpmath import mpf [as 别名]
def compute_d(K, N):
"""d_{k, n} from DLMF 8.12.12"""
M = N + 2*K
d0 = [-mp.mpf(1)/3]
alpha = compute_alpha(M + 2)
for n in range(1, M):
d0.append((n + 2)*alpha[n+2])
d = [d0]
g = compute_g(K)
for k in range(1, K):
dk = []
for n in range(M - 2*k):
dk.append((-1)**k*g[k]*d[0][n] + (n + 2)*d[k-1][n+2])
d.append(dk)
for k in range(K):
d[k] = d[k][:N]
return d
开发者ID:ryfeus,项目名称:lambda-packs,代码行数:19,
示例2: gammainc
点赞 6
# 需要导入模块: import mpmath [as 别名]
# 或者: from mpmath import mpf [as 别名]
def gammainc(a, x, dps=50, maxterms=10**8):
"""Compute gammainc exactly like mpmath does but allow for more
summands in hypercomb. See
mpmath/functions/expintegrals.py#L134
in the mpmath github repository.
"""
with mp.workdps(dps):
z, a, b = mp.mpf(a), mp.mpf(x), mp.mpf(x)
G = [z]
negb = mp.fneg(b, exact=True)
def h(z):
T1 = [mp.exp(negb), b, z], [1, z, -1], [], G, [1], [1+z], b
return (T1,)
res = mp.hypercomb(h, [z], maxterms=maxterms)
return mpf2float(res)
开发者ID:ryfeus,项目名称:lambda-packs,代码行数:22,
示例3: zpkfreqz
点赞 6
# 需要导入模块: import mpmath [as 别名]
# 或者: from mpmath import mpf [as 别名]
def zpkfreqz(z, p, k, worN=None):
"""
Frequency response of a filter in zpk format, using mpmath.
This is the same calculation as scipy.signal.freqz, but the input is in
zpk format, the calculation is performed using mpath, and the results are
returned in lists instead of numpy arrays.
"""
if worN is None or isinstance(worN, int):
N = worN or 512
ws = [mpmath.pi * mpmath.mpf(j) / N for j in range(N)]
else:
ws = worN
h = []
for wk in ws:
zm1 = mpmath.exp(1j * wk)
numer = _prod([zm1 - t for t in z])
denom = _prod([zm1 - t for t in p])
hk = k * numer / denom
h.append(hk)
return ws, h
开发者ID:Relph1119,项目名称:GraphicDesignPatternByPython,代码行数:24,
示例4: test_rf
点赞 6
# 需要导入模块: import mpmath [as 别名]
# 或者: from mpmath import mpf [as 别名]
def test_rf(self):
if LooseVersion(mpmath.__version__) >= LooseVersion("1.0.0"):
# no workarounds needed
mppoch = mpmath.rf
else:
def mppoch(a, m):
# deal with cases where the result in double precision
# hits exactly a non-positive integer, but the
# corresponding extended-precision mpf floats don't
if float(a + m) == int(a + m) and float(a + m) <= 0:
a = mpmath.mpf(a)
m = int(a + m) - a
return mpmath.rf(a, m)
assert_mpmath_equal(sc.poch,
mppoch,
[Arg(), Arg()],
dps=400)
开发者ID:Relph1119,项目名称:GraphicDesignPatternByPython,代码行数:20,
示例5: test_boxcox
点赞 6
# 需要导入模块: import mpmath [as 别名]
# 或者: from mpmath import mpf [as 别名]
def test_boxcox(self):
def mp_boxcox(x, lmbda):
x = mpmath.mp.mpf(x)
lmbda = mpmath.mp.mpf(lmbda)
if lmbda == 0:
return mpmath.mp.log(x)
else:
return mpmath.mp.powm1(x, lmbda) / lmbda
assert_mpmath_equal(sc.boxcox,
exception_to_nan(mp_boxcox),
[Arg(a=0, inclusive_a=False), Arg()],
n=200,
dps=60,
rtol=1e-13)
开发者ID:Relph1119,项目名称:GraphicDesignPatternByPython,代码行数:18,
示例6: test_boxcox1p
点赞 6
# 需要导入模块: import mpmath [as 别名]
# 或者: from mpmath import mpf [as 别名]
def test_boxcox1p(self):
def mp_boxcox1p(x, lmbda):
x = mpmath.mp.mpf(x)
lmbda = mpmath.mp.mpf(lmbda)
one = mpmath.mp.mpf(1)
if lmbda == 0:
return mpmath.mp.log(one + x)
else:
return mpmath.mp.powm1(one + x, lmbda) / lmbda
assert_mpmath_equal(sc.boxcox1p,
exception_to_nan(mp_boxcox1p),
[Arg(a=-1, inclusive_a=False), Arg()],
n=200,
dps=60,
rtol=1e-13)
开发者ID:Relph1119,项目名称:GraphicDesignPatternByPython,代码行数:19,
示例7: mpf2float
点赞 5
# 需要导入模块: import mpmath [as 别名]
# 或者: from mpmath import mpf [as 别名]
def mpf2float(x):
"""
Convert an mpf to the nearest floating point number. Just using
float directly doesn't work because of results like this:
with mp.workdps(50):
float(mpf("0.99999999999999999")) = 0.9999999999999999
"""
return float(mpmath.nstr(x, 17, min_fixed=0, max_fixed=0))
开发者ID:ryfeus,项目名称:lambda-packs,代码行数:12,
示例8: mp_assert_allclose
点赞 5
# 需要导入模块: import mpmath [as 别名]
# 或者: from mpmath import mpf [as 别名]
def mp_assert_allclose(res, std, atol=0, rtol=1e-17):
"""
Compare lists of mpmath.mpf's or mpmath.mpc's directly so that it
can be done to higher precision than double.
"""
try:
len(res)
except TypeError:
res = list(res)
n = len(std)
if len(res) != n:
raise AssertionError("Lengths of inputs not equal.")
failures = []
for k in range(n):
try:
assert_(mpmath.fabs(res[k] - std[k]) <= atol + rtol*mpmath.fabs(std[k]))
except AssertionError:
failures.append(k)
ndigits = int(abs(np.log10(rtol)))
msg = [""]
msg.append("Bad results ({} out of {}) for the following points:"
.format(len(failures), n))
for k in failures:
resrep = mpmath.nstr(res[k], ndigits, min_fixed=0, max_fixed=0)
stdrep = mpmath.nstr(std[k], ndigits, min_fixed=0, max_fixed=0)
if std[k] == 0:
rdiff = "inf"
else:
rdiff = mpmath.fabs((res[k] - std[k])/std[k])
rdiff = mpmath.nstr(rdiff, 3)
msg.append("{}: {} != {} (rdiff {})".format(k, resrep, stdrep, rdiff))
if failures:
assert_(False, "\n".join(msg))
开发者ID:ryfeus,项目名称:lambda-packs,代码行数:39,
示例9: compute_a
点赞 5
# 需要导入模块: import mpmath [as 别名]
# 或者: from mpmath import mpf [as 别名]
def compute_a(n):
"""a_k from DLMF 5.11.6"""
a = [mp.sqrt(2)/2]
for k in range(1, n):
ak = a[-1]/k
for j in range(1, len(a)):
ak -= a[j]*a[-j]/(j + 1)
ak /= a[0]*(1 + mp.mpf(1)/(k + 1))
a.append(ak)
return a
开发者ID:ryfeus,项目名称:lambda-packs,代码行数:12,
示例10: lagrange_inversion
点赞 5
# 需要导入模块: import mpmath [as 别名]
# 或者: from mpmath import mpf [as 别名]
def lagrange_inversion(a):
"""Given a series
f(x) = a[1]*x + a[2]*x**2 + ... + a[n-1]*x**(n - 1),
use the Lagrange inversion formula to compute a series
g(x) = b[1]*x + b[2]*x**2 + ... + b[n-1]*x**(n - 1)
so that f(g(x)) = g(f(x)) = x mod x**n. We must have a[0] = 0, so
necessarily b[0] = 0 too.
The algorithm is naive and could be improved, but speed isn't an
issue here and it's easy to read.
"""
n = len(a)
f = sum(a[i]*x**i for i in range(len(a)))
h = (x/f).series(x, 0, n).removeO()
hpower = [h**0]
for k in range(n):
hpower.append((hpower[-1]*h).expand())
b = [mp.mpf(0)]
for k in range(1, n):
b.append(hpower[k].coeff(x, k - 1)/k)
b = map(lambda x: mp.mpf(x), b)
return b
开发者ID:Relph1119,项目名称:GraphicDesignPatternByPython,代码行数:29,
示例11: _binomial_cdf
点赞 5
# 需要导入模块: import mpmath [as 别名]
# 或者: from mpmath import mpf [as 别名]
def _binomial_cdf(k, n, p):
k, n, p = mpmath.mpf(k), mpmath.mpf(n), mpmath.mpf(p)
if k <= 0:
return mpmath.mpf(0)
elif k >= n:
return mpmath.mpf(1)
onemp = mpmath.fsub(1, p, exact=True)
return mpmath.betainc(n - k, k + 1, x2=onemp, regularized=True)
开发者ID:Relph1119,项目名称:GraphicDesignPatternByPython,代码行数:11,
示例12: _f_cdf
点赞 5
# 需要导入模块: import mpmath [as 别名]
# 或者: from mpmath import mpf [as 别名]
def _f_cdf(dfn, dfd, x):
if x < 0:
return mpmath.mpf(0)
dfn, dfd, x = mpmath.mpf(dfn), mpmath.mpf(dfd), mpmath.mpf(x)
ub = dfn*x/(dfn*x + dfd)
res = mpmath.betainc(dfn/2, dfd/2, x2=ub, regularized=True)
return res
开发者ID:Relph1119,项目名称:GraphicDesignPatternByPython,代码行数:9,
示例13: _noncentral_chi_cdf
点赞 5
# 需要导入模块: import mpmath [as 别名]
# 或者: from mpmath import mpf [as 别名]
def _noncentral_chi_cdf(x, df, nc, dps=None):
if dps is None:
dps = mpmath.mp.dps
x, df, nc = mpmath.mpf(x), mpmath.mpf(df), mpmath.mpf(nc)
with mpmath.workdps(dps):
res = mpmath.quad(lambda t: _noncentral_chi_pdf(t, df, nc), [0, x])
return res
开发者ID:Relph1119,项目名称:GraphicDesignPatternByPython,代码行数:9,
示例14: test_tklmbda_zero_shape
点赞 5
# 需要导入模块: import mpmath [as 别名]
# 或者: from mpmath import mpf [as 别名]
def test_tklmbda_zero_shape(self):
# When lmbda = 0 the CDF has a simple closed form
one = mpmath.mpf(1)
assert_mpmath_equal(
lambda x: sp.tklmbda(x, 0),
lambda x: one/(mpmath.exp(-x) + one),
[Arg()], rtol=1e-7)
开发者ID:Relph1119,项目名称:GraphicDesignPatternByPython,代码行数:9,
示例15: test_g
点赞 5
# 需要导入模块: import mpmath [as 别名]
# 或者: from mpmath import mpf [as 别名]
def test_g():
# Test data for the g_k. See DLMF 5.11.4.
with mp.workdps(30):
g = [mp.mpf(1), mp.mpf(1)/12, mp.mpf(1)/288,
-mp.mpf(139)/51840, -mp.mpf(571)/2488320,
mp.mpf(163879)/209018880, mp.mpf(5246819)/75246796800]
mp_assert_allclose(compute_g(7), g)
开发者ID:Relph1119,项目名称:GraphicDesignPatternByPython,代码行数:9,
示例16: test_alpha
点赞 5
# 需要导入模块: import mpmath [as 别名]
# 或者: from mpmath import mpf [as 别名]
def test_alpha():
# Test data for the alpha_k. See DLMF 8.12.14.
with mp.workdps(30):
alpha = [mp.mpf(0), mp.mpf(1), mp.mpf(1)/3, mp.mpf(1)/36,
-mp.mpf(1)/270, mp.mpf(1)/4320, mp.mpf(1)/17010,
-mp.mpf(139)/5443200, mp.mpf(1)/204120]
mp_assert_allclose(compute_alpha(9), alpha)
开发者ID:Relph1119,项目名称:GraphicDesignPatternByPython,代码行数:9,
示例17: test_d
点赞 5
# 需要导入模块: import mpmath [as 别名]
# 或者: from mpmath import mpf [as 别名]
def test_d():
# Compare the d_{k, n} to the results in appendix F of [1].
#
# Sources
# -------
# [1] DiDonato and Morris, Computation of the Incomplete Gamma
# Function Ratios and their Inverse, ACM Transactions on
# Mathematical Software, 1986.
with mp.workdps(50):
dataset = [(0, 0, -mp.mpf('0.333333333333333333333333333333')),
(0, 12, mp.mpf('0.102618097842403080425739573227e-7')),
(1, 0, -mp.mpf('0.185185185185185185185185185185e-2')),
(1, 12, mp.mpf('0.119516285997781473243076536700e-7')),
(2, 0, mp.mpf('0.413359788359788359788359788360e-2')),
(2, 12, -mp.mpf('0.140925299108675210532930244154e-7')),
(3, 0, mp.mpf('0.649434156378600823045267489712e-3')),
(3, 12, -mp.mpf('0.191111684859736540606728140873e-7')),
(4, 0, -mp.mpf('0.861888290916711698604702719929e-3')),
(4, 12, mp.mpf('0.288658297427087836297341274604e-7')),
(5, 0, -mp.mpf('0.336798553366358150308767592718e-3')),
(5, 12, mp.mpf('0.482409670378941807563762631739e-7')),
(6, 0, mp.mpf('0.531307936463992223165748542978e-3')),
(6, 12, -mp.mpf('0.882860074633048352505085243179e-7')),
(7, 0, mp.mpf('0.344367606892377671254279625109e-3')),
(7, 12, -mp.mpf('0.175629733590604619378669693914e-6')),
(8, 0, -mp.mpf('0.652623918595309418922034919727e-3')),
(8, 12, mp.mpf('0.377358774161109793380344937299e-6')),
(9, 0, -mp.mpf('0.596761290192746250124390067179e-3')),
(9, 12, mp.mpf('0.870823417786464116761231237189e-6'))]
d = compute_d(10, 13)
res = []
for k, n, std in dataset:
res.append(d[k][n])
std = map(lambda x: x[2], dataset)
mp_assert_allclose(res, std)
开发者ID:Relph1119,项目名称:GraphicDesignPatternByPython,代码行数:38,
示例18: test_spherical_jn
点赞 5
# 需要导入模块: import mpmath [as 别名]
# 或者: from mpmath import mpf [as 别名]
def test_spherical_jn(self):
def mp_spherical_jn(n, z):
arg = mpmath.mpmathify(z)
out = (mpmath.besselj(n + mpmath.mpf(1)/2, arg) /
mpmath.sqrt(2*arg/mpmath.pi))
if arg.imag == 0:
return out.real
else:
return out
assert_mpmath_equal(lambda n, z: sc.spherical_jn(int(n), z),
exception_to_nan(mp_spherical_jn),
[IntArg(0, 200), Arg(-1e8, 1e8)],
dps=300)
开发者ID:Relph1119,项目名称:GraphicDesignPatternByPython,代码行数:16,
示例19: test_spherical_yn
点赞 5
# 需要导入模块: import mpmath [as 别名]
# 或者: from mpmath import mpf [as 别名]
def test_spherical_yn(self):
def mp_spherical_yn(n, z):
arg = mpmath.mpmathify(z)
out = (mpmath.bessely(n + mpmath.mpf(1)/2, arg) /
mpmath.sqrt(2*arg/mpmath.pi))
if arg.imag == 0:
return out.real
else:
return out
assert_mpmath_equal(lambda n, z: sc.spherical_yn(int(n), z),
exception_to_nan(mp_spherical_yn),
[IntArg(0, 200), Arg(-1e10, 1e10)],
dps=100)
开发者ID:Relph1119,项目名称:GraphicDesignPatternByPython,代码行数:16,
示例20: test_spherical_yn_complex
点赞 5
# 需要导入模块: import mpmath [as 别名]
# 或者: from mpmath import mpf [as 别名]
def test_spherical_yn_complex(self):
def mp_spherical_yn(n, z):
arg = mpmath.mpmathify(z)
out = (mpmath.bessely(n + mpmath.mpf(1)/2, arg) /
mpmath.sqrt(2*arg/mpmath.pi))
if arg.imag == 0:
return out.real
else:
return out
assert_mpmath_equal(lambda n, z: sc.spherical_yn(int(n.real), z),
exception_to_nan(mp_spherical_yn),
[IntArg(0, 200), ComplexArg()])
开发者ID:Relph1119,项目名称:GraphicDesignPatternByPython,代码行数:15,
示例21: test_spherical_in
点赞 5
# 需要导入模块: import mpmath [as 别名]
# 或者: from mpmath import mpf [as 别名]
def test_spherical_in(self):
def mp_spherical_in(n, z):
arg = mpmath.mpmathify(z)
out = (mpmath.besseli(n + mpmath.mpf(1)/2, arg) /
mpmath.sqrt(2*arg/mpmath.pi))
if arg.imag == 0:
return out.real
else:
return out
assert_mpmath_equal(lambda n, z: sc.spherical_in(int(n), z),
exception_to_nan(mp_spherical_in),
[IntArg(0, 200), Arg()],
dps=200, atol=10**(-278))
开发者ID:Relph1119,项目名称:GraphicDesignPatternByPython,代码行数:16,
示例22: test_spherical_in_complex
点赞 5
# 需要导入模块: import mpmath [as 别名]
# 或者: from mpmath import mpf [as 别名]
def test_spherical_in_complex(self):
def mp_spherical_in(n, z):
arg = mpmath.mpmathify(z)
out = (mpmath.besseli(n + mpmath.mpf(1)/2, arg) /
mpmath.sqrt(2*arg/mpmath.pi))
if arg.imag == 0:
return out.real
else:
return out
assert_mpmath_equal(lambda n, z: sc.spherical_in(int(n.real), z),
exception_to_nan(mp_spherical_in),
[IntArg(0, 200), ComplexArg()])
开发者ID:Relph1119,项目名称:GraphicDesignPatternByPython,代码行数:15,
示例23: test_spherical_kn
点赞 5
# 需要导入模块: import mpmath [as 别名]
# 或者: from mpmath import mpf [as 别名]
def test_spherical_kn(self):
def mp_spherical_kn(n, z):
out = (mpmath.besselk(n + mpmath.mpf(1)/2, z) *
mpmath.sqrt(mpmath.pi/(2*mpmath.mpmathify(z))))
if mpmath.mpmathify(z).imag == 0:
return out.real
else:
return out
assert_mpmath_equal(lambda n, z: sc.spherical_kn(int(n), z),
exception_to_nan(mp_spherical_kn),
[IntArg(0, 150), Arg()],
dps=100)
开发者ID:Relph1119,项目名称:GraphicDesignPatternByPython,代码行数:15,
示例24: pdf_gauss_mp
点赞 5
# 需要导入模块: import mpmath [as 别名]
# 或者: from mpmath import mpf [as 别名]
def pdf_gauss_mp(x, sigma, mean):
return mp.mpf(1.) / mp.sqrt(mp.mpf("2.") * sigma ** 2 * mp.pi) * mp.exp(
- (x - mean) ** 2 / (mp.mpf("2.") * sigma ** 2))
开发者ID:SAP-samples,项目名称:machine-learning-diff-private-federated-learning,代码行数:5,
注:本文中的mpmath.mpf方法示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。