2017-02-22 29 views
0

我想计算表格的积分:large integralPython:集成了100个函数和集成变量

我很努力在Python中实现这一点。有没有一种程序化的方式来实现这一点?我试着用几个for循环做这个,但是我被卡在错误SystemError: too many statically nested blocks上。

fs = {} 
for i in range(100): 
    fs[i] = lamda x: x*i 

for x1 in np.arange(0,1,.01): 
    for x2 in np.arange(0,1,.01): 
     .... 
      for x100 in np.arange(0,1,.01): 
      for i in range(100): 
       exec("summ+= fs[i](x%i"%i) 
+0

线长限制? – miradulo

+0

是不是有限制?那只是pep8? – kilojoules

+0

这只是PEP8是的,你可能在Pycharm或什么? – miradulo

回答

0

当然是慢: 的 “对于i在范围(100)”(1)部分达到100 * 100 * 100 * ... * 100(100“* 100的)= 100^100;和人们说n^2是坏的... 反正我会这样做的:

- Put 100 values initialized with 0 into a list. 
- for i = 0 to 100^100: 
    - go through the list and use the k-th value as the parameter for the k-th function. do the summing thing 
    - Add 0.1 to the n-th value in the list; 
    if this value is now == 1; set it to 0 and go to the (n+1)-th value and repeat this procedure until you reach the end of the list or one value is < 1. 
    Start with the first value in the list. 
now you have your sum. 
+1

这不是问题。你没有超过一个for循环;看看我的伪代码,它解决了这个问题。但是,真的,你不应该执行此操作;因为有人(和我)在评论中说算法不会很快结束。你应该尝试补偿这个等式。 – DaOnlyOwner

+0

有兴趣了解更多信息 - 我不明白你的答案。我用随机抽样结束了近似积分。 – kilojoules