0

This is a very general question -- is there any way to vectorize consequential simulation (where next step depends on previous), or any such iterative algorithm in general?

Obviously, if one need to run M simulations (each N steps) you can use for i in range(N) and calculate M values on each step to get a significant speed-up. But say you only need one or two simulations with a lot of steps, or your simulations don't have a fixed amount of steps (like radiation detection), or you are solving a differential system (again, for a lot of steps). Is there any way to shove upper for-loop under the numpy hood (with a speed gain, I am not talking passing python function object to numpy.vectorize), or cython-ish approaches are the only option? Or maybe this is possible in R or some similar language, but not (currently?) in Python?

Physmatik
  • 256
  • 1
  • 6
  • 2
    Unfortunately no, that's just the nature of python. You might want to take a look at `numba` - it can compile some loops into very efficient code without much effort from the programmer. – user2699 Apr 08 '19 at 14:09
  • What about other high-level computational languages/frameworks? – Physmatik Apr 08 '19 at 14:37
  • `numpy.vectorize`is simply a wrapper with a `for` loop, so it is convenient in terms of coding but not in terms of performance. The question is very broad so it really depends on what you want to do. A lot of `scipy` packages run in C and thus are highly performant, or at least close to a performant language, e.g. ODE integration. My recommendation would be to check if there's a scipy function for what you want to do, if not, either check `numba` as pointed out, or try to see if there's a way to "roll" your function using numpy available methods. – Tarifazo Apr 08 '19 at 17:40

1 Answers1

0

Perhaps Multigrid in time methods can give some improvements.

pyano
  • 1,885
  • 10
  • 28