Several alternatives
make a list and then convert it to arary:
from itertools import product
import numpy as np
k = n = 0
K = N = 5 # you can change back to 1000
my_list = []
for k, n in product(range(k, K), range(n, N)):
real_part = np.cos(((2 * np.pi) / N) * k * n)
imag_part = np.sin(((2 * np.pi) / N) * k * n)
my_list.append(np.complex(real_part, -imag_part))
my_array = np.array(my_list)
print(my_array)
work directly with np.array:
from itertools import product
import numpy as np
k = n = 0
K = N = 5 # you can change back to 1000
my_array = np.empty((K * N), dtype=np.complex)
for idx, (k, n) in enumerate(product(range(k, K), range(n, N))):
real_part = np.cos(((2 * np.pi) / N) * k * n)
imag_part = np.sin(((2 * np.pi) / N) * k * n)
my_array[idx] = np.complex(real_part, -imag_part)
print(my_array)
make function that returns complex number and use list comprehension
from itertools import product
import numpy as np
def cmplx(k, n):
real_part = np.cos(((2 * np.pi) / N) * k * n)
imag_part = np.sin(((2 * np.pi) / N) * k * n)
return np.complex(real_part, -imag_part)
k = n = 0
K = N = 5 # you can change back to 1000
my_array = np.array([cmplx(k, n) for k, n in product(range(k, K), range(n, N))])
print(my_array)
make a generator and use np.fromiter() to create the array
from itertools import product
import numpy as np
def cmplx(k, K, n, N):
for k, n in product(range(k, K), range(n, N)):
real_part = np.cos(((2 * np.pi) / N) * k * n)
imag_part = np.sin(((2 * np.pi) / N) * k * n)
yield np.complex(real_part, -imag_part)
k = n = 0
K = N = 5 # you can change back to 1000
my_array = np.fromiter(cmplx(k, K, n, N), dtype=np.complex)
print(my_array)
now, because n=k and K=N, you can pass
repeat
to itertools.product(). Basically in all example you can replace
product(range(k, K), range(n, N))
with
product(range(start, end), repeat=2)
from itertools import product
import numpy as np
def cmplx(start, end):
N = end
for k, n in product(range(start, end), repeat=2):
real_part = np.cos(((2 * np.pi) / N) * k * n)
imag_part = np.sin(((2 * np.pi) / N) * k * n)
yield np.complex(real_part, -imag_part)
start = 0
end = 5 # you can change back to 1000
my_array = np.fromiter(cmplx(start, end), dtype=np.complex)
print(my_array)
all of the above will produce
Output:
[ 1. -0.j 1. -0.j 1. -0.j
1. -0.j 1. -0.j 1. -0.j
0.30901699-0.95105652j -0.80901699-0.58778525j -0.80901699+0.58778525j
0.30901699+0.95105652j 1. -0.j -0.80901699-0.58778525j
0.30901699+0.95105652j 0.30901699-0.95105652j -0.80901699+0.58778525j
1. -0.j -0.80901699+0.58778525j 0.30901699-0.95105652j
0.30901699+0.95105652j -0.80901699-0.58778525j 1. -0.j
0.30901699+0.95105652j -0.80901699+0.58778525j -0.80901699-0.58778525j
0.30901699-0.95105652j]
Others may suggest yet another/even better ways to do it.