Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Complex floating issue arshad Unladen Swallow Posts: 4 Threads: 2 Joined: Nov 2019 Reputation: 0 Likes received: 0 #1 Nov-04-2019, 03:55 PM (This post was last modified: Nov-04-2019, 11:44 PM by scidam. Edited 1 time in total.) 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) ```--------------------------------------------------------------------------- ``````Error: TypeError Traceback (most recent call last) in 8 for i in range(n+1): 9 for j in range(n+1): ---> 10 A[i][j]=(i-j)**(alpha) 11 12 TypeError: can't convert complex to float `````` scidam wrote Nov-04-2019, 11:44 PM:Please post all code, output and errors (in it's entirety) between their respective tags. I did it for you this time, Here are instructions on how to do it yourself next time. DeaD_EyE Da Bishop Posts: 1,240 Threads: 5 Joined: May 2017 Reputation: 131 Likes received: 273 #2 Nov-04-2019, 04:18 PM (This post was last modified: Nov-04-2019, 04:18 PM by DeaD_EyE. Edited 2 times in total.) 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) ``` My code examples are always for Python >=3.6.0 Almost dead, but too lazy to die: https://sourceserver.info All humans together. We don't need politicians! arshad Unladen Swallow Posts: 4 Threads: 2 Joined: Nov 2019 Reputation: 0 Likes received: 0 #3 Nov-05-2019, 03:26 PM (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 `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) ` Thanks for the suggestion Why you choose n+1 instead of n=5 in array ? « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post floating point arithmetic exDeveloper 2 235 Sep-25-2019, 04:33 PM Last Post: DeaD_EyE

Forum Jump:

Users browsing this thread: 1 Guest(s)