vectorization of multiple return of a complex function in a dataframe

半世苍凉 提交于 2021-01-29 07:43:50

问题


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

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)

回答1:


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


来源:https://stackoverflow.com/questions/64485696/vectorization-of-multiple-return-of-a-complex-function-in-a-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!