Add function simplifyForLoops to collapse for loops that share the same index ranges
#40
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently when a model uses several macros, it can lead to (valid) NIMBLE code which is not particularly simple/readable. For example, if you specify a simple occupancy model with macros like this:
The resulting model code is
There's nothing incorrect about that code, but it's not how someone would normally write it. They'd probably put the
zmodel in the same loop as the calculation forpsi, and theymodel in the same loop asp, etc. Furthermore because we are cautious about not mixing up for loop indices when expanding macros, we end up with six indices (i_1-i_6) when strictly speaking only two are needed.This PR adds a function
simplifyForLoopswhich takes NIMBLE code and attemps to collapse for loops that share indices, and also to replace indices with a smaller set of simpler ones. This is possible because the function is meant to be run on the final expanded code, when we know the entire structure. For example, when applied to the model above, the result isWhich is simpler and much closer to how a user would typically write the model.
Right now this is just a standalone function and is not run automatically at any point. Furthermore to use it you'd have to write the code with macros, create the model object, run the function on the output model code (post macro processing), and then re-create the model with the new code, which is a bit clunky.