如何评估 mpmath 函数内的 numpy 数组?
How to evaluate a numpy array inside an mpmath fuction?
当我尝试在 mpmath 函数中使用 numpy 数组时出现错误,此示例在到达以下行时失败:
C = (f*L/D) + 2*mp.log(P1/P2)
其中P1是一个数组。
错误:
cannot create mpf from array([**P1_array**])
我知道 and this 个踏板,它们是相关的。但我无法让我的代码工作。有人可以帮我改正这个错误吗?
import numpy as np
import mpmath as mp
mp.mp.dps = 20
# State equation --> pV = nZRT
P1 = np.linspace(101325,10*101325,100)
P2 = 101325
T = 300
D = 0.0095
A = mp.power(D,2)*mp.pi/4
L = 300
R = 8.31446
f = 0.05
Z1 = 0.9992
Z2 = 0.9999
Zm = 0.5*(Z1+Z2)
C = (f*L/D) + 2*mp.log(P1/P2)
w2 = (mp.power(P1,2)-mp.power(P2,2))*mp.power(A,2)/(Zm*R*T*C)
w = mp.power(w2,0.5)
您需要使用 np.frompyfunc:
在您的 numpy 数组上广播您想要的函数(此处为日志和电源)
import numpy as np
import mpmath as mp
mp.mp.dps = 20
# State equation --> pV = nZRT
P1 = np.linspace(101325,10*101325,100)
P2 = 101325
T = 300
D = 0.0095
A = mp.power(D,2)*mp.pi/4
L = 300
R = 8.31446
f = 0.05
Z1 = 0.9992
Z2 = 0.9999
Zm = 0.5*(Z1+Z2)
log_array = np.frompyfunc(mp.log, 1, 1) #to evaluate mpmath log function on a numpy array
pow_array = np.frompyfunc(mp.power, 2, 1) #to evaluate mpmath power function on a numpy array
C = (f*L/D) + 2*log_array(P1/P2)
w2 = (pow_array(P1,2)-pow_array(P2,2))*pow_array(A,2)/(Zm*R*T*C)
w = pow_array(w2,0.5)
当我尝试在 mpmath 函数中使用 numpy 数组时出现错误,此示例在到达以下行时失败:
C = (f*L/D) + 2*mp.log(P1/P2)
其中P1是一个数组。 错误:
cannot create mpf from array([**P1_array**])
我知道
import numpy as np
import mpmath as mp
mp.mp.dps = 20
# State equation --> pV = nZRT
P1 = np.linspace(101325,10*101325,100)
P2 = 101325
T = 300
D = 0.0095
A = mp.power(D,2)*mp.pi/4
L = 300
R = 8.31446
f = 0.05
Z1 = 0.9992
Z2 = 0.9999
Zm = 0.5*(Z1+Z2)
C = (f*L/D) + 2*mp.log(P1/P2)
w2 = (mp.power(P1,2)-mp.power(P2,2))*mp.power(A,2)/(Zm*R*T*C)
w = mp.power(w2,0.5)
您需要使用 np.frompyfunc:
在您的 numpy 数组上广播您想要的函数(此处为日志和电源)import numpy as np
import mpmath as mp
mp.mp.dps = 20
# State equation --> pV = nZRT
P1 = np.linspace(101325,10*101325,100)
P2 = 101325
T = 300
D = 0.0095
A = mp.power(D,2)*mp.pi/4
L = 300
R = 8.31446
f = 0.05
Z1 = 0.9992
Z2 = 0.9999
Zm = 0.5*(Z1+Z2)
log_array = np.frompyfunc(mp.log, 1, 1) #to evaluate mpmath log function on a numpy array
pow_array = np.frompyfunc(mp.power, 2, 1) #to evaluate mpmath power function on a numpy array
C = (f*L/D) + 2*log_array(P1/P2)
w2 = (pow_array(P1,2)-pow_array(P2,2))*pow_array(A,2)/(Zm*R*T*C)
w = pow_array(w2,0.5)