Update: The question may seem too broad, but the answer provided by belisarius responds perfectly to the question, as the use of $ListConvolve$ and $NestList$ meets the desired criteria in in addition to the speed performance.
I'm currently simulating an infection (traditional Volterra infection agent simulation) in Mathematica. A very complicated but complete model can be found in the Wolfram blog
The set up is the following:
- We have a $n\times n$ matrix that represent the world. In each box we have three numbers: infected, susceptible and dead.
- The equations that govern the model are the following:
$S^{n+1}_{ij}= S^n_{ij} - \beta \left( I^n_{ij} S^n_{ij}+S^n_{i+1j}I^n_{i+1j}+S^n_{i-1j} I^n_{i-1j}+S^n_{ij-1} I^n_{ij-1}+S^n_{ij+1} I^n_{ij+1} \right) $ $I^{n+1}_{ij}= I^n_{ij} + \beta \left( I^n_{ij} S^n_{ij}+S^n_{i+1j}I^n_{i+1j}+S^n_{i-1j} I^n_{i-1j}+S^n_{ij-1} I^n_{ij-1}+S^n_{ij+1} I^n_{ij+1} \right) $
$D^{n+1}=S^0-S^{n+1}-I^{n+1}$
(With the initial conditions $I^0$ and $S^0$ to choose according pleases).
As you can see, for each time interval (n) you have to go through the matrix (i,j) in order to calculate (n+1), so it's a recursive thing. As you can see in the Wolfram blog, they use a combination of $Table$ (or $ParallelTable$) and recursive functions declared as $f[n,i,j]:=f[n-1,i,j]+...$.
The question is the following:
I want a functional-programming style approach to do this in order to be elegant and speed up the calculations. I was wondering if ListConvolve
(as you can find in my other question ( Functional programming approach to avoid traditional loops ) can be used with NestList
somehow.
The ideal features of the approach would be:
- No loops.
- Functional-programming style.
- Speed (Real time evaluation ?¿ )
- Elegant code.
As you can see, the core of the question is how to avoid recursive and procedural programing functions with loops that work very bad in Mathematica in order to speed up things. Of course the following is allowed:
- Compiled functions.
- Parallel evaluation.
- Magic.
Thank you all for your time and i hope you will find this question interesting enough.