出于我的问题的目的,我必须使用module 而不是scipy中的gamma函数。但是,当变量从数组中获取并且 y 值应该以相同的格式生成时,我使用它来绘制我的函数。但在这样做时,我收到一条错误消息,抱怨从array到mpf的转换。mpmath
matplotlib.pyplot
numpy
TypeError
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import quad, dblquad
import mpmath as mp
low, up = 5.630e5, 1.167e12
alpha, threshold = 1.05 , 2.15e10
beta = 274
def g(x, low, up, beta):
return mp.gamma(-2/3) * (mp.gammainc(-2/3, beta*(x/low)**3) - mp.gammainc(-2/3, beta*(x/up)**3))
def Integrand1(x, low, up, threshold, alpha, beta):
return pow(x/threshold, alpha) * g(x, low, up, beta)
def Integrand2(x, low, up, threshold, alpha, beta):
return g(x, low, up, beta)
def PDF(x, low, up, threshold, alpha, beta):
A = quad(Integrand1, low, threshold, args=(low, up, threshold, alpha, beta))
B = quad(Integrand2, threshold, up, args=(low, up, threshold, alpha, beta))
C=(A[0]+B[0])**(-1)
y = np.piecewise(x,
[x < threshold], [lambda x: C * pow(x/threshold, alpha) * g(x, low, up, beta),
lambda x: C * g(x, low, up, beta)
]
)
return y
x_array = np.array(np.logspace(8.2, 11.8, 10))
y_array = PDF(x_array, low, up, threshold, alpha, beta)
plt.plot(x_array, y_array, color='green', linestyle='-')
plt.gca().autoscale(False)
plt.vlines([threshold],
plt.gca().get_ylim()[0], plt.gca().get_ylim()[1],
linestyles='dashed', color='k', label='')
plt.xscale("log", nonposx='clip')
plt.yscale("log", nonposy='clip')
plt.show()
回溯(最后一次调用):文件“test.py”,第 35 行,在 y_array = PDF(x_array, low, up, threshold, alpha, beta) 文件“test.py”,第 28 行,在 PDF lambda x 中: C * g(x, low, up, beta) 文件“/home/username/anaconda3/lib/python3.6/site-packages/numpy/lib/function_base.py”,第 1344 行,分段 y[condlist[k ]] = item(vals, *args, **kw) 文件“test.py”,第 27 行,在 [x < 阈值],[lambda x: C * pow(x/threshold, alpha) * g(x, low, up, beta),文件“test.py”,第 13 行,以 g 为单位返回 mp.gamma(-2/3) * (mp.gammainc(-2/3, beta*(x/low)**3 ) - mp.gammainc(-2/3, beta*(x/up)**3)) 文件“/home/username/anaconda3/lib/python3.6/site-packages/mpmath/functions/expintegrals.py” ,第 141 行,在 gammainc a = ctx.转换(a)文件“/home/username/anaconda3/lib/python3.6/site-packages/mpmath/ctx_mp_python.py”,第 662 行,在转换中返回 ctx._convert_fallback(x,字符串)文件“/home/username /anaconda3/lib/python3.6/site-packages/mpmath/ctx_mp.py",第 614 行,在 _convert_fallback 中引发 TypeError("cannot create mpf from " + repr(x)) TypeError: cannot create mpf from array([ 6.11259157 e+09, 9.68780477e+10,9.68780477e+10,9.68780477e+10,
1.53541358e+12, 2.43346654e+13, 3.85678455e+14, 6.11259157e+15])
我需要知道如何 mpf 一个数组,我找到了这些页面(如何 mpf 一个数组)和(创建一个 mpf),但我不知道如何将它们应用到我的绘图例程中。