我有一个函数总是收敛到一个不动点,例如f(x)= (x-a)/2+a
。我有一个函数,可以通过重复调用函数来找到这个固定点:
def find_fix_point(f,x):
while f(x)>0.1:
x = f(x)
return x
这很好,现在我想做一个矢量化的版本
def find_fix_point(f,x):
while (f(x)>0.1).any():
x = f(x)
return x
但是,如果大多数实例只需要大约10次迭代,而一个实例需要1000次迭代,那么这是非常低效的。什么是删除已找到的`x的快速方法?你知道吗
代码可以使用numpy
或scipy
。你知道吗
解决这个问题的一种方法是使用递归:
在这个实现中,我们只在需要更新的点上调用
f
。你知道吗注意,通过使用递归,我们避免了每次调用都要在越来越小的数组上进行检查
x > 0.1
。你知道吗一般的方法是使用布尔索引来计算那些还没有达到平衡的。你知道吗
我调整了Jonas Adler给出的算法以避免最大递归深度:
编辑: 在这里,我回顾了提出的3种解决方案。我将根据它们的提议者来调用不同的函数,
find_fix_Jonas, find_fix_Jurg, find_fix_BM
。我根据BM修改了所有函数中的不动点条件(见最后更新的Jonas函数)。你知道吗速度:
根据可读性我认为乔纳斯的版本是最容易理解的,所以应该选择在速度不是很重要的时候。你知道吗
然而,Jonas的版本可能会引发Runtimeerror,当到达fixpoint之前的迭代次数很大时(>;1000)。其他两种解决方案没有这个缺点。你知道吗
然而,B.M.的版本可能比我提出的版本更容易理解。你知道吗
#使用的Jonas版本:
首先为了通用性,我修改了标准以适应定点定义:当
|x-f(x)|<=epsilon
时停止。你知道吗您可以混合boolean indexing and integer indexing以保持每次活动的点。这里有一个方法:
有很多固定点的例子:
相关问题 更多 >
编程相关推荐