0

I want to compute an integral of the form: large integral.

I am struggling to implement this in python. Is there a programic way to accomplish this? I tried doing this with several for loops but I'm stuck on the error 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)
kilojoules
  • 9,768
  • 18
  • 77
  • 149
  • Line length limit? – miradulo Feb 22 '17 at 18:55
  • Isn't there a limit? Is that just pep8? – kilojoules Feb 22 '17 at 18:58
  • That is just PEP8 yep, you're probably in Pycharm or something? – miradulo Feb 22 '17 at 19:00
  • 2
    is Recursion possible? – AsheKetchum Feb 22 '17 at 19:01
  • 1
    `scipy.integrate`? https://docs.scipy.org/doc/scipy-0.18.1/reference/tutorial/integrate.html – Steven Morad Feb 22 '17 at 19:06
  • I was also looking at scipy.integrate, more specifically https://docs.scipy.org/doc/scipy-0.18.1/reference/tutorial/integrate.html#general-multiple-integration-dblquad-tplquad-nquad – Kelvin Feb 22 '17 at 19:07
  • I would love to reduce the number of lines needed in my code. It feels like I should be able to do this in ten lines of code. – kilojoules Feb 22 '17 at 19:09
  • If your goal is simply to write little code use `itertools.combinations_with_replacement(np.arange(0, 1, 0.1), 100)` – BlackBear Feb 22 '17 at 19:42
  • @BlackBear I need a more general solution. My actual code uses numpy spline interpolations as the f functions. – kilojoules Feb 22 '17 at 19:58
  • 3
    Since nobody said it: even if you *could* nest that many for loops, it's not going to work done that way. The innermost loop would run 100^100 times. If every one takes 1 nanosecond (in reality, it takes longer), the heat death of the universe is well underway, the supermassive black hole at the center of the galaxy has evaporated via Hawking radiation, and the sun has long ago turned into a white dwarf star and cooled, before the computation finishes. – pv. Feb 22 '17 at 20:57

1 Answers1

1

Of course it is slow: The "for i in range(100)" (1) part is reached 100 * 100 * 100 * ... * 100 (100 '*100's) = 100 ^ 100; and people say n^2 is bad... Anyways I would do it like this:

- 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.
DaOnlyOwner
  • 343
  • 3
  • 8
  • 1
    This is not a problem. You don't have more than one for loop; just look at my pseudocode, it solves the problem. But really, you shouldn't bother executing this; as someone (and me) said in the comments the algorithm isn't going to end very soon. You should try to compensate the equation. – DaOnlyOwner Feb 22 '17 at 21:10
  • Interested in learning more about this—I don't understand your answer. I wound up approximating the integral using random sampling. – kilojoules Mar 14 '17 at 02:36