What is a concise and readable way of interpolating a 1D array such that the maximum difference between elements is minimized?
For instance, if I had the array [4 9 13 25] and I was allowed to add 1 more number in order to minimize the maximum difference between elements I would insert a 19 between 13 and 25 (max difference is now 6 rather than 12).
Of course a good ole' for loop will get it done, but for posterity is there a less verbose approach than below?
# current array
nums = np.array([4.0, 9.0, 13.0, 25.0])
# size of new array
N=10
# recursively find max gap (difference) and fill it with a mid point
for k in range(N-len(nums)):
inds = range(len(nums))
# get the maximum difference between two elements
max_gap = np.argmax(np.diff(nums))
# put a new number that's equidistant from the two element values
new_num = np.interp(np.mean([inds[max_gap],inds[max_gap+1]]), inds, nums)
nums = np.insert(nums, max_gap+1, new_num)
print nums
This example interpolates the 1D array, filling regions where the greatest difference was:
[ 4. 9. 13. 19. 25.]
[ 4. 9. 13. 16. 19. 25.]
[ 4. 9. 13. 16. 19. 22. 25.]
[ 4. 6.5 9. 13. 16. 19. 22. 25. ]
[ 4. 6.5 9. 11. 13. 16. 19. 22. 25. ]
[ 4. 6.5 9. 11. 13. 14.5 16. 19. 22. 25. ]
edit 1: As comments suggest, there is a trade-off between readability, efficiency, and accuracy. Of these three attributes, for me the most important is readability. I still give +1 for any and all improvements to my above algorithm though since it is a general problem and any answer that improves either of those three attributes is beneficial to someone if not me later on.