Complex floating issue - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Complex floating issue (/thread-22225.html) |
Complex floating issue - arshad - Nov-04-2019 Can anyone make a look to following code why it cant calculate complex numbers in the defined arrray. import numpy as np n=5 alpha = 0.7 A=np.zeros([n+1,n+1]) B=np.zeros([n+1]) #sum=0 for i in range(n+1): for j in range(n+1): A[i][j]=(i-j)**(alpha) print(A)---------------------------------------------------------------------------
RE: Complex floating issue - DeaD_EyE - Nov-04-2019 Please use Code-Tags for code, otherwise the indentation is lost. You have to chose the right data type. Use numpy.complex128 as dtype .The real part is a 64 bit float and the imaginary part is a 64 bit float. In addition you can use product from itertools, to get rid of the nested loop. You can also access more than one axis in a multidimensional array. Last thing is: You can reuse the range object. import numpy as np from itertools import product n = 5 alpha = 0.7 A = np.zeros([n+1,n+1], dtype=np.complex128) B = np.zeros([n+1], dtype=np.complex128) r = range(n+1) for i,j in product(r, r): A[i,j] = (i-j) ** alpha print(A) RE: Complex floating issue - arshad - Nov-05-2019 (Nov-04-2019, 04:18 PM)DeaD_EyE Wrote: Please use Code-Tags for code, otherwise the indentation is lost. You have to chose the right data type. Use Thanks for the suggestion Why you choose n+1 instead of n=5 in array ? |