[英]Efficient way to find index of interval
I'm writing a spline class in Python. 我正在用Python编写样条类。 The method to calculate the the spline interpolated value requires the index of the closest x data points.
计算样条插值的方法需要最接近的x个数据点的索引。 Currently a simplified version looks like this:
当前,简化版本如下所示:
def evaluate(x):
for ii in range(N): # N = len(x_data)
if x_data[ii] <= x <= x_data[ii+1]:
return calc(x,ii)
So it iterates through the list of x_data
points until it finds the lower index ii
of interval in which x
lies and uses that in the function calc
, which performs the spline interpolation. 因此,它将遍历
x_data
点的列表,直到找到x
所在的区间的下标ii
,然后在执行样条插值的函数calc
使用它。 While functional, it seems like this would be inefficient for large x_data
arrays if x
is close to the end of the data set. 虽然功能正常,但如果
x
接近数据集的末尾,这对于大型x_data
数组似乎效率不高。 Is there a more efficient or elegant way to perform the same functionality, which does not require every interval to be checked iteratively? 是否有一种更有效或更优雅的方法来执行相同的功能,而不需要迭代检查每个间隔?
Note: x_data
may be assumed to be sorted so x_data[ii] < x_data[ii+1]
, but is not necessarily equally spaced. 注意:可以假定
x_data
已排序,因此x_data[ii] < x_data[ii+1]
,但不一定相等。
this is exactly what bisect is for https://docs.python.org/2/library/bisect.html 这正是bisect的含义, https: //docs.python.org/2/library/bisect.html
from bisect import bisect
index = bisect(x_data,x)
#I dont think you actually need the value of the 2 closest but if you do here it is
point_less = x_data[index-1] # note this will break if its index 0 so you probably want a special case for that
point_more = x_data[index]
closest_value = min([point_less,point_more],key=lambda y:abs(x-y))
alternatively you should use binary search(in fact im pretty sure thats what bisect uses under the hood) .... it should be worst case O(log n)
(assuming your input array is already sorted) 或者,您应该使用二进制搜索(实际上我很确定那是bisect在幕后使用的)....应该是最坏的情况
O(log n)
(假设您的输入数组已经排序)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.