Reputation: 111
import numpy as np
list_shot = list(np.arange(0, 100, 50))
list_lens = list(np.arange(10))
list_total = []
for shot in list_shot:
print("-", shot)
list_out = []
for lens in list_lens:
val = shot * lens
list_out.append(val)
print(list_out)
list_total.append(list_out)
Basically, the above code is just a simple representation of this code below that needs to be optimised for it to run faster.
CN_list_shot = []
counter = 0
for shot in bec[1:1000:500]: ### reads from time snapshots
CN_list = []
for length in length_list: ### all the lenghts for the same snapshot
i = neighbor_list("i", shot, {('Ge', 'Ge'): length}, self_interaction= False)
coord = np.bincount(i)
CN_list.append(np.sum(coord) / np.count_nonzero(coord))
print("CN_list: ", CN_list) ### value added to the list for the snapshot
CN_list_shot.append(CN_List) ### the list added for each snapshots
Upvotes: 1
Views: 281
Reputation: 862
Yes there is a simpler way to achieve this using linear algebra methods. When using numpy
try to avoid transforming a numpy
array back to a python list. This defeats the purpose.
Essentially, what you are doing here is a simple matrix multiplication. All you have to do is reshape the numpy
arrays in order to achieve what you want.
import numpy as np
list_shot = np.arange(0, 100, 50).reshape((2, 1))
list_lens = np.arange(10).reshape((1, 10))
print(list_shot)
print(list_lens)
print(np.matmul(list_shot, list_lens))
Output:
[[ 0]
[50]]
[[0 1 2 3 4 5 6 7 8 9]]
[[ 0 0 0 0 0 0 0 0 0 0]
[ 0 50 100 150 200 250 300 350 400 450]]
Upvotes: 3