2

At line

e[i][j][k]=np.divide(ftf[i][j][k],ftg[i][j][k]) 

while running below code,

''' Online Python Compiler. Code, Compile, Run and Debug python program online. Write your code in this editor and press "Run" button to execute it. ''' import numpy as np f=np.zeros((2,2,2)) g=np.zeros((2,2,2)) f=np.add(f,1) g=np.add(g,2) for i in range(0,2): for j in range(0,2): for k in range(0,2): f[i][j][k] = (i+1)*(j+1)+(k+1) g[i][j][k] = (i+2)*(j+3)+(k+4) ftf = np.fft.fftn(f) ftg = np.fft.fftn(g) print(f) print(g) print(ftf) print(ftg) print("------") e =np.zeros((2,2,2)) for i in range(0,2): for j in range(0,2): for k in range(0,2): if np.isnan(ftg[i][j][k]): e[i][j][k]=0 else: e[i][j][k]=np.divide(ftf[i][j][k],ftg[i][j][k]) print(e) print("------") h= np.fft.ifftn(e) print(h) 

an error is returned:

/home/main.py:36: ComplexWarning: Casting complex values to real discards the imaginary part
e[i][j][k]=np.divide(ftf[i][j][k],ftg[i][j][k])

I think I have to create e as complex. How can I create a complex matrix with zeros as elements?

1 Answer 1

4

Set the dtype argument in np.zeros to complex:

np.zeros((10,10), dtype=complex) array([[0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j], [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j], [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],... 
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, it worked, error is gone, thank you, now there is another error /home/main.py:36: RuntimeWarning: invalid value encountered in true_divide e[i][j][k]=np.divide(ftf[i][j][k],ftg[i][j][k]) but its another question to be asked right?
Yes, IMO you could make this question more specific about this issue, so explaining what you want and an expected output, all the rest is unnecessary to answer this question. And for what you just commented you could ask a new question. Glad it helped!
Thank you. I found it out myself, there were zeros in right side of divide method. I was checking against only NaNs.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.