Vector function representation

Some background: I’m new to writing distributed programs and I’m trying to write a variation of the SSA algorithm in Julia. My system has a lot of reactions, and I need to calculate the propensity of these reactions over and over. I use a dependency graph, so whenever a reaction fires I use that dependency graph to figure out which reactions’ propensities I need to update.

Now, I have these propensities stored in an array and I also have an array of functions, where the ith element is a function that calculates the propensity of the ith reaction. A basic version of these functions look something like this: () -> rate[x]*reactants[y] So if I know a reaction occurs and I need to update the propensities of, say, reactions 1,2 and 3, I run a simple loop and access the 1st, 2nd and 3rd elements of this array and run them, and update the propensity array based on their results.

My question is is there a more efficient way for me to represent/store these functions or rewrite them in a vectorized form? The actual calculation for all of these functions can be represented as matrix multiplication of some slice of the rate/reactant vectors, but I need a way to access each of these functions. The simpler way is probably to have a switch statement which will encode how to carry out the calculations, so I can just pass the reaction number and it will figure out which calculation to run, but that isn’t really “vectorized” and I would prefer to have as little control logic as possible.

Thanks a lot!

It is very hard to answer this question without an MWE we can run, to see how you implemented it, what the functions look like typically, the scale of the problem, etc.

1 Like