数据帧中复杂函数多次返回的矢量化 [英] vectorization of multiple return of a complex function in a dataframe

查看:65
本文介绍了数据帧中复杂函数多次返回的矢量化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制包括复杂向量在内的各种数据.感谢贡献者看到答案https://stackoverflow.com/a/64480659/13953414,我设法生成了数据帧,但是当我想添加一些额外的计算时却卡住了.我收到一个错误:

I am trying to plot various data including complex vectors.Thanks to contributors see answers https://stackoverflow.com/a/64480659/13953414, i managed to generate the dataframes but i get stuck when i want to add some additional calculations. i get an error :

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.

all calculations starting from T_depth are not executed due to a format issue. the function were vectorized but yet I cannot execute and save the 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)

推荐答案

np.vectorize 可以返回多个列"作为数组的元组.这里我展示了如何添加列"到矢量化函数以及如何重新排列它们.

np.vectorize can return multiple "columns" as a tuple of arrays. Here I showcase how to add a "column" to the vectorized function and how to rearrange them.

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

这篇关于数据帧中复杂函数多次返回的矢量化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆