我正在尝试绘制各种数据,包括复杂的向量。感谢贡献者查看答案https://stackoverflow.com/a/64480659/13953414,我设法生成了数据框,但是当我想添加一些其他计算时,我陷入了困境。我收到错误消息:
df['T_depth'] = (math.sqrt(D / (4 * (math.pi) * frequency)) / 1e-6),
TypeError: only size-1 arrays can be converted to Python scalars
由于格式问题,未执行所有从T_depth开始的计算。该功能已矢量化,但我无法执行和保存df。
from mpmath import *
import numpy as np
import cmath
import math
import pandas as pd
mp.dps = 15; mp.pretty = True
a = mpf(0.25)
b = mpf(0.25)
z = mpf(0.75)
frequency = np.arange(1, 50, 10) # frequency range
bh = np.arange(10e-6, 35e-6, 10e-6) #10e-6 # width
print(bh)
D = 1e-6 #7.8e-4 # diffusivity
gamma = 0.5772 # Euler constant
# Cartesian product of input variables
idx = pd.MultiIndex.from_product([bh, frequency], names=["bh", "frequency"])
df = pd.DataFrame(index=idx).reset_index()
# Omega is vectorized naturally.
omega = (df["bh"].values**2 * df["frequency"].values) * (2 * math.pi / D)
# vectorize meijerg() only, so other operations won't interrupt with this
def f_u(omega_elem):
# u = (-j/(math.pi * omega_elem)) * meijerg([[1, 3/2], []], [[1, 1], [0.5, 0]], j*omega_elem)
return (-j/(math.pi * omega_elem)) * meijerg([[1, 3/2], []], [[1, 1], [0.5, 0]], j*omega_elem)
f_u_vec = np.vectorize(f_u, otypes=[np.complex128]) # output complex
u = f_u_vec(omega)
def asympt(omega_elem):
return (4/(math.pi))*(-(1/2)*np.log(omega_elem) + 3/2 - gamma - j*((math.pi)/4))
asympt_vec = np.vectorize(asympt, otypes=[np.complex128]) # output complex
v = asympt_vec(omega)
#is it possible to merge these 2 aforementioned operation in one method?
df["Re"] = np.real(u)
df["Im"] = np.imag(u)
df["asympt_R"] = np.real(v)
df["asympt_Im"] = np.imag(v)
# the following operations cannot be executed
df['T_depth'] = (math.sqrt(D / (4 * (math.pi) * frequency)) / 1e-6)
df['amplitude'] = math.sqrt(np.real(u)**2 + np.imag(u)**2)
df['phase'] = math.degrees(math.atan(np.imag(u) / np.real(u)))
df['thermal_freq'] = 2 * (math.pi) * frequency
print(df)
答案 0 :(得分:1)
np.vectorize
可以返回多个“列”作为数组的元组。在这里,我展示了如何向矢量化函数添加“列”以及如何重新排列它们。
def f_u(omega_elem):
val1 = (-j / (math.pi * omega_elem)) * meijerg([[1, 3 / 2], []], [[1, 1], [0.5, 0]], j * omega_elem)
asympt = (4 / (math.pi)) * (-(1 / 2) * np.log(omega_elem) + 3 / 2 - gamma - j * ((math.pi) / 4))
return val1, asympt
# return a tuple of array. Remember to assign two otypes.
f_u_vec = np.vectorize(f_u, otypes=[np.complex128, np.complex128])
tup = f_u_vec(omega) # tuple of arrays: (val1, asympt)
df["Re"] = np.real(tup[0]) # val1
df["Im"] = np.imag(tup[0])
df["asympt_R"] = np.real(tup[1]) # asympt
df["asympt_Im"] = np.imag(tup[1])
# result
df
Out[94]:
bh frequency Re Im asympt_R asympt_Im
0 0.00001 1 5.868486 -0.999374 5.868401 -1.0
1 0.00001 11 4.342982 -0.994876 4.341854 -1.0
2 0.00001 21 3.932365 -0.991121 3.930198 -1.0
3 0.00001 31 3.685457 -0.987696 3.682257 -1.0
4 0.00001 41 3.508498 -0.984488 3.504268 -1.0
5 0.00002 1 4.986257 -0.997867 4.985859 -1.0
6 0.00002 11 3.463849 -0.983559 3.459311 -1.0
7 0.00002 21 3.056269 -0.972212 3.047656 -1.0
8 0.00002 31 2.812349 -0.962168 2.799715 -1.0
9 0.00002 41 2.638332 -0.952979 2.621726 -1.0