So I'm struggling to make this code more useful for larger data-sets. Here is the code, I will explain it thoroughly afterwards:
import numpy as np
np.set_printoptions(threshold='nan')
tri_nums = [3, 2, 1]
paths = [1, 3, 4, 5]
vol_list = [10, 10, 10, 15, 15, 25]
n = 0
array_list = []
while n <= len(tri_nums):
for num in tri_nums:
print "assigning volume", vol_list[sum(tri_nums)-len(vol_list)+1]
volume_array = np.zeros(shape = (6, 6))
volume_array[paths[num-1],paths[num]] = vol_list[sum(tri_nums)-len(vol_list)+1]
array_list.append(volume_array)
print paths[num-1], paths[num]
tri_nums.pop(0)
paths.pop(0)
n+=1
print paths
print tri_nums
final_array = sum(array_list)
print array_list
print final_array
Starting with tri_nums:
The values of tri_nums will always be a list of the triangular numbers of the length of paths. So a paths list of say, [1, 3, 4, 5, 6, 8], will give a tri_nums of [5, 4, 3, 2, 1].
tri_nums are also correlated to the number of values in vol_list. As you can see, there are three 10's in vol_list. The number of 10's is equal to the first value of tri_nums. There are also two 15's and a 2 for the second value of tri_nums. This pattern will never change! Another example of this is:
paths = [1, 3, 4, 5, 6, 8]
tri_nums = [5, 4, 3, 2, 1]
vol_list = [15, 15, 15, 15, 15, 250, 250, 250, 250, 25, 25, 25, 10, 10, 15]
The list paths (in the original case) is made up of four 'nodes', nodes 1,3,4 and 5. Between each adjacent node there is a path, i.e. path 1-3, 3-4, 4-5.
As one can see, volume_array is a 6x6 array and is made up of zeros. The row values in volume_array that are to be changed correspond to the first value of each path i.e. 1,3, 4. The column values correspond to the second number of each path i.e. 3, 4,5.
Here comes the tricky bit!
The values in vol_list are allocated to the aforementioned array items as follows:
- For each value of
tri_numsa value invol_listis added tovolume_array. The row value within this array is defined by the first value of a path i.e.[4]and the column value is defined by the second value of a path (for the value[4]this will mean[5]). - For
tri_nums[0], the value10is added three times, once tovolume_array[4][5], once tovolume_array[3][4]and once tovolume_array[1][3]. - For
tri_nums[1]the value15is added twice, once tovolume_array[4][5]and once tovolume_array[3][4]. - For
tri_nums[2]the value25is added once tovolume_array[4][5]. - Finally, all of the values in of the arrays generated in the previous three steps are added together to get
final_array.
Another thing worth mentioning is that the sum of tri_nums is equal to len(vol_list). Furthermore tri_nums[n] is always > tri_nums[n+1].
Ideally I would like to implement this code for path's, tri_num's and vol_list's with hundreds of items in them. The method I am using now would mean I need to make hundreds of while loops by hand. How can I make the while loops work simultaneously so I can avoid the "hundreds of while loops" scenario?
Everything is working just fine, but the final output is:
[[ 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 10. 0. 0.]
[ 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 25. 0.]
[ 0. 0. 0. 0. 0. 25.]
[ 0. 0. 0. 0. 0. 0.]]
Meaning that the final value of vol_list which is (25) has not been assigned to array_list[4][5] and thus was not in final_array. It just needs to do one more loop and it'll work, I'm not sure how to get it to do the last loop though.
Please let me know if anything is unclear!
Thanks
listis a datatype - it shouldn't be used as a variable name (rename it).while count < 1:is redundant since you'd get the same result if you removed it. It only makes sense to leave it in if you'll later want to iterate more than once.whileloops is subtly different. For example, the first one references two entries ofpathsat each iteration, namelypaths[n]andpaths[n+1], but the other loops reference one entry ofpathseach time through. Each loop computes its indices intovolume_arraydifferently. The third loop incrementsbbut doesn't actually use its value anywhere in the loop. Are these intentional inconsistencies? Why? What do the three indicesn,a, andbmean?