separate real and imaginary parts of a function evaluated in mpmath python

318 views Asked by At

I am evaluation a MeijerG function in mpmath, the result is a complex. i would like to separate real and imaginary parts to save them in a dataframe and subsequently plot them. I get an error

TypeError: cannot create mpf from array followed by a series of complex numbers.

anybody would have a clean approach to separate and save those data so the user can plot them in any format.

from mpmath import *
import sympy
import numpy as np
import cmath
import math
import pandas as pd
import matplotlib.pyplot as plt

mp.dps = 5; mp.pretty = True
a = mpf(0.25)
b = mpf(0.25)
z = mpc(0.75)
frequency = np.arange(1, 1e4, 100)

def q():
  return (-j/frequency)*meijerg([[1, 3/2], []], [[1, 1], [1/2, 0]], j*frequency)

T=q()
Re_q = np.real.T
Im_q = np.imag.T

print(Re_q)
print(Im_q)

data = pd.DataFrame({
    'Frequency (Hz)': frequency,
    'Re': Re_q,
    'Im': Im_q
}
)
data.to_csv('C:\\Users\\T.csv')
1

There are 1 answers

0
datac On BEST ANSWER

this way it separates real and imaginary parts.

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)  # frequeny range
bh = np.arange(10e-6, 30e-6, 10e-6) #10e-6 # width
print(bh)
D = 1e-6 #7.8e-4  # diffusivity
gamma = 0.5772 # Euler constant
v = []
w =[]
i = []
def q(frequency):
  for i in bh:
    # for f in frequency:
      omega = (((i ** 2) * 2 * math.pi * frequency) / D)  # depends on bh and frequency
      u = ((-j/(math.pi * omega))*meijerg([[1, 3/2], []], [[1, 1], [0.5, 0]], j*omega))
      v = np.real(u)
      w = np.imag(u)
      return i, frequency, v, w
#transpose arrays
T = np.vectorize(q)
print(T(frequency))
df = np.array(T(frequency)).T
print(df)
# create DataFrame
df1 = pd.DataFrame(data=df, columns=['bh', 'frequency','Re', 'Im'])
print(df1)
#save in .csv
df1.to_csv('C:\\Users\\Mohamed Boutchich\\PycharmProjects\\calculations\\T.csv')