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)