我有相当多的阶跃函数(大约1000个),每个函数只有两个间隔。我想把它们加起来,然后求出最大值。最好的办法是什么?我试过sympy,代码如下:
from sympy import Piecewise, piecewise_fold, evalf
from sympy.abc import x
from sympy.plotting import *
import numpy as np
S = 20
t = np.random.random(20)
sum_piecewise = None
for s in range(S):
p = Piecewise((np.random.random(), x<t[s]), (np.random.random(), x>=t[s]))
if not sum_piecewise:
sum_piecewise = p
else:
sum_piecewise += p
print sum_piecewise.evalf(0.2)
但是,这会输出一个大的符号表达式,而不是一个实际值,这正是我想要的。在
考虑到数值函数,使用Numpy更好(就性能而言)。有一种方法:
以下是函数和的图:
^{pr2}$显然,为了找到最大值,只考虑
S
中包含的S
时间瞬间就足够了。对于这个例子使用substitution怎么样?{{cd2>
相关问题 更多 >
编程相关推荐