1- """
1+ """
22 singleindices(idxhandler::AbstractIndexHandler, arr)
33
44Returns all indices `I` in `arr`. Defaults to CartesianIndices, but can
5- be overloaded for arbitrary index handlers.
5+ be overloaded for arbitrary index handlers.
66"""
77singleindices (:: AbstractIndexHandler , arr:: AbstractArray ) = CartesianIndices (arr)
88singleindices (:: AbstractIndexHandler , arr:: Tuple ) = CartesianIndices (arr)
99
10- """
10+ """
1111 pairedindices(idxhandler::AbstractIndexHandler, arr, shift::CartesianIndex)
1212
1313Returns all pairs of indices `(I .- shift, I)` in `arr`.
@@ -17,16 +17,16 @@ function pairedindices end
1717"""
1818 getsubstitutions(idxhandler::AbstractIndexHandler, rs::ReactionSystem; state_sym::Symbol)
1919
20- Returns a dict of the form `S_i => f_i(state_sym)`, where each `f_i` is an expression
20+ Returns a dict of the form `S_i => f_i(state_sym)`, where each `f_i` is an expression
2121for the abundance of species `S_i` in terms of the state variable `state_sym`.
2222"""
2323function getsubstitutions end
2424
2525"""
2626 vec(idxhandler::AbstractIndexHandler, arr)
2727
28- Converts the right-hand side defining the solution of the CME into a
29- one-dimensional vector to which a matrix can be applied.
28+ Converts the right-hand side defining the solution of the CME into a
29+ one-dimensional vector to which a matrix can be applied.
3030
3131See also: [`LinearIndices`](@ref Base.LinearIndices)
3232"""
@@ -50,15 +50,15 @@ function LinearIndices end
5050# #
5151
5252
53- """
53+ """
5454 struct NaiveIndexHandler <: AbstractIndexHandler
5555 offset::Int
5656 end
5757
5858Basic index handler that stores the state of a system with
5959`s` species in an `s`-dimensional array. The `offset` parameter
6060denotes the offset by which the array is indexed (defaults to 1
61- in Julia).
61+ in Julia).
6262
6363This is the simplest index handler, but it will not be optimal
6464if some states cannot be reached from the initial state, e.g.
@@ -79,24 +79,24 @@ NaiveIndexHandler() = NaiveIndexHandler(1)
7979Base. vec (:: NaiveIndexHandler , arr) = vec (arr)
8080Base. LinearIndices (:: NaiveIndexHandler , arr) = LinearIndices (arr)
8181
82- function pairedindices (ih:: NaiveIndexHandler , arr:: AbstractArray{T,N} ,
82+ function pairedindices (ih:: NaiveIndexHandler , arr:: AbstractArray{T,N} ,
8383 shift:: CartesianIndex{N} ) where {T,N}
8484 pairedindices (ih, axes (arr), shift)
8585end
8686
87- function pairedindices (ih:: NaiveIndexHandler , dims:: NTuple{N,T} ,
87+ function pairedindices (ih:: NaiveIndexHandler , dims:: NTuple{N,T} ,
8888 shift:: CartesianIndex{N} ) where {N,T<: Number }
8989 pairedindices (ih, Base. OneTo .(dims), shift)
9090end
9191
92- function pairedindices (:: NaiveIndexHandler , dims:: NTuple{N,T} ,
92+ function pairedindices (:: NaiveIndexHandler , dims:: NTuple{N,T} ,
9393 shift:: CartesianIndex{N} ) where {N,T<: AbstractVector }
94- ranges = tuple ((UnitRange (max (first (ax), first (ax)+ shift[i]),
95- min (last (ax), last (ax)+ shift[i]))
94+ ranges = tuple ((UnitRange (max (first (ax), first (ax)+ shift[i]),
95+ min (last (ax), last (ax)+ shift[i]))
9696 for (i, ax) in enumerate (dims)). .. )
97-
97+
9898 ranges_shifted = tuple ((rng .- shift[i] for (i, rng) in enumerate (ranges)). .. )
99-
99+
100100 zip (CartesianIndices (ranges_shifted), CartesianIndices (ranges))
101101end
102102
@@ -111,13 +111,13 @@ end
111111
112112# #
113113
114- """
114+ """
115115 struct ReducingIndexHandler <: AbstractIndexHandler
116116
117117More efficient index handler that improves upon [`NaiveIndexHandler`](@ref)
118118by eliminating variables whose abundances can be computed from other variables
119119using conservation laws. Describes the system using a subset of the original
120- species which can be obtained via [`reducedspecies`](@ref). Reduces the
120+ species which can be obtained via [`reducedspecies`](@ref). Reduces the
121121dimensionality of the FSP by the number of conservation laws in the system.
122122
123123Note: This feature is currently being moved into Catalyst.jl.
@@ -135,12 +135,12 @@ struct ReducingIndexHandler <: AbstractIndexHandler
135135end
136136
137137function ReducingIndexHandler (rs:: ReactionSystem , offset:: Int = 1 )
138- cons_laws = conservationlaws (sys )
138+ cons_laws = conservationlaws (rs )
139139 specs_elided = elidedspecies (cons_laws)
140- specs_red = [ i for i in 1 : length (species (sys . rs)) if ! (i in specs_elided) ]
141-
140+ specs_red = [ i for i in 1 : length (species (rs)) if ! (i in specs_elided) ]
141+
142142 cons_syms = [ gensym (" c$i " ) for i in 1 : size (cons_laws, 1 ) ]
143-
143+
144144 return ReducingIndexHandler (cons_laws, offset, specs_red, specs_elided, cons_syms)
145145end
146146
@@ -170,7 +170,7 @@ elidedspecies(idxhandler::ReducingIndexHandler) = idxhandler.specs_elided
170170
171171# #
172172
173- """
173+ """
174174 elidedspecies(cons_laws::AbstractMatrix{Int})::Vector
175175
176176Returns a list of species ``[ s_1, ... ]`` which can be removed from the reaction system
@@ -186,9 +186,9 @@ function elidedspecies(cons_laws::AbstractMatrix{Int})::Vector{Int}
186186 idx = possible_specs[end ]
187187 ret[i] = idx
188188 end
189-
189+
190190 @assert length (ret) == length (unique (ret))
191-
191+
192192 return ret
193193end
194194
@@ -204,19 +204,19 @@ See also: [`getsubstitutions`](@ref)
204204function elisions (idxhandler:: ReducingIndexHandler , rs:: ReactionSystem )
205205 ret = Dict ()
206206 spec_syms = species (rs)
207-
207+
208208 for (i, spec) in enumerate (elidedspecies (idxhandler))
209209 sym = spec_syms[spec]
210210 cons_law = idxhandler. cons_laws[i,:]
211-
211+
212212 # Does this always work? What if some of the species on the RHS
213213 # also end up getting elided at some point?
214214 rhs = (Variable (idxhandler. cons_syms[i]) - sum (cons_law[j] * spec_syms[j] for j in 1 : length (spec_syms) if j != spec))
215215 rhs /= cons_law[i]
216-
216+
217217 ret[sym] = rhs
218218 end
219-
219+
220220 ret
221221end
222222
@@ -230,28 +230,28 @@ from the conserved quantities and the reduced species.
230230"""
231231function getsubstitutions (idxhandler:: ReducingIndexHandler , rs:: ReactionSystem ; state_sym:: Symbol )
232232 symbols = states (rs)
233-
234- ret = Dict {Any,Any} (symbols[spec] => Term (Base. getindex, (state_sym, i)) - idxhandler. offset
233+
234+ ret = Dict {Any,Any} (symbols[spec] => Term (Base. getindex, (state_sym, i)) - idxhandler. offset
235235 for (i, spec) in enumerate (reducedspecies (idxhandler)))
236-
236+
237237 elision_dict = elisions (idxhandler, rs)
238238 for (spec, ex) in pairs (elision_dict)
239239 ret[spec] = substitute (ex, ret)
240240 end
241-
241+
242242 ret
243243end
244244
245245"""
246246 build_rhs_header(idxhandler::ReducingIndexHandler, sys::FSPSystem)::Expr
247247
248- Assumes `p` is of the form `(params, cons::AbstractVector{Int})` where `params`
248+ Assumes `p` is of the form `(params, cons::AbstractVector{Int})` where `params`
249249are the system parameters and `cons` the conserved quantities.
250250"""
251251function build_rhs_header (sys:: FSPSystem{ReducingIndexHandler} ):: Expr
252252 cons_names = Expr (:tuple , sys. ih. cons_syms... )
253-
254- quote
253+
254+ quote
255255 (ps, $ (cons_names)) = p
256256 $ (unpackparams (sys, :ps ))
257257 end
@@ -263,13 +263,13 @@ end
263263Similar to its `NaiveIndexHandler` variant, but converts the indices into indices into
264264the reduced state space array.
265265"""
266- function pairedindices (idxhandler:: ReducingIndexHandler , arr:: AbstractArray{T,M} ,
266+ function pairedindices (idxhandler:: ReducingIndexHandler , arr:: AbstractArray{T,M} ,
267267 shift:: CartesianIndex{N} ) where {T,M,N}
268268 shift_red = CartesianIndex {M} (convert (Tuple, shift)[reducedspecies (idxhandler)]. .. )
269269 pairedindices (NaiveIndexHandler (idxhandler. offset), arr, shift_red)
270270end
271271
272- function pairedindices (idxhandler:: ReducingIndexHandler , dims:: NTuple{M} ,
272+ function pairedindices (idxhandler:: ReducingIndexHandler , dims:: NTuple{M} ,
273273 shift:: CartesianIndex{N} ) where {M,N}
274274 shift_red = CartesianIndex {M} (convert (Tuple, shift)[reducedspecies (idxhandler)]. .. )
275275 pairedindices (NaiveIndexHandler (idxhandler. offset), dims, shift_red)
0 commit comments