Skip to content

Commit 8551454

Browse files
committed
up
1 parent ce8e110 commit 8551454

File tree

3 files changed

+37
-45
lines changed

3 files changed

+37
-45
lines changed

src/network_analysis.jl

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -198,19 +198,22 @@ end
198198
Return the negative of the graph Laplacian of the reaction network. The ODE system of a chemical reaction network can be factorized as ``\frac{dx}{dt} = Y A_k Φ(x)``, where ``Y`` is the [`complexstoichmat`](@ref) and ``A_k`` is the negative of the graph Laplacian, and ``Φ`` is the [`massactionvector`](@ref). ``A_k`` is an n-by-n matrix, where n is the number of complexes, where ``A_{ij} = k_{ij}`` if a reaction exists between the two complexes and 0 otherwise.
199199
Returns a symbolic matrix by default, but will return a numerical matrix if parameter values are specified via pmap.
200200
"""
201-
function laplacianmat(rn::ReactionSystem, pmap = Dict(); sparse = false)
202-
D = incidencemat(rn; sparse)
203-
K = fluxmat(rn, pmap; sparse)
204-
D*K
201+
function laplacianmat(rn::ReactionSystem, pmap::Dict = Dict())
202+
D = incidencemat(rn)
203+
K = fluxmat(rn, pmap)
204+
D * K
205205
end
206206

207+
Base.zero(::Type{Union{R, Symbolics.BasicSymbolic{Real}}}) where R <: Real = zero(R)
208+
Base.one(::Type{Union{R, Symbolics.BasicSymbolic{Real}}}) where R <: Real = one(R)
209+
207210
@doc raw"""
208211
fluxmat(rn::ReactionSystem, pmap = Dict(); sparse=false)
209212
210213
Return an r×c matrix ``K`` such that, if complex ``j`` is the substrate complex of reaction ``i``, then ``K_{ij} = k``, the rate constant for this reaction. Mostly a helper function for the network Laplacian, [`laplacianmat`](@ref). Has the useful property that ``\frac{dx}{dt} = S*K*Φ(x)``, where S is the [`netstoichmat`](@ref) or net stoichiometry matrix and ``Φ(x)`` is the [`massactionvector`](@ref).
211214
Returns a symbolic matrix by default, but will return a numerical matrix if rate constants are specified as a `Tuple`, `Vector`, or `Dict` of symbol-value pairs via `pmap`.
212215
"""
213-
function fluxmat(rn::ReactionSystem, pmap::Dict = Dict(); sparse=false)
216+
function fluxmat(rn::ReactionSystem, pmap::Dict = Dict())
214217
rates = if isempty(pmap)
215218
reactionrates(rn)
216219
else
@@ -221,49 +224,45 @@ function fluxmat(rn::ReactionSystem, pmap::Dict = Dict(); sparse=false)
221224
nc = length(rcmap)
222225
nr = length(rates)
223226
mtype = eltype(rates) <: Symbolics.BasicSymbolic ? Num : eltype(rates)
224-
mat = if sparse
225-
fluxmat(SparseMatrixCSC{mtype, Int}, rcmap, rates)
226-
else
227-
fluxmat(Matrix{mtype}, rcmap, rates)
228-
end
229-
mtype == Num ? Matrix{Any}(mat) : mat
230-
end
231-
232-
function fluxmat(::Type{SparseMatrixCSC{T, Int}}, rcmap, rates) where T
233-
Is = Int[]
234-
Js = Int[]
235-
Vs = T[]
236-
for (i, (complex, rxs)) in enumerate(rcmap)
237-
for (rx, dir) in rxs
238-
dir == -1 && begin
239-
push!(Is, rx)
240-
push!(Js, i)
241-
push!(Vs, rates[rx])
242-
end
243-
end
244-
end
245-
Z = sparse(Is, Js, Vs, length(rates), length(rcmap))
246-
end
227+
Matrix{Any}(fluxmat(Matrix{mtype}, rcmap, rates))
228+
end
229+
230+
# function fluxmat(::Type{SparseMatrixCSC{T, Int}}, rcmap, rates) where T
231+
# Is = Int[]
232+
# Js = Int[]
233+
# Vs = T[]
234+
# for (i, (complex, rxs)) in enumerate(rcmap)
235+
# for (rx, dir) in rxs
236+
# dir == -1 && begin
237+
# push!(Is, rx)
238+
# push!(Js, i)
239+
# push!(Vs, rates[rx])
240+
# end
241+
# end
242+
# end
243+
# Z = sparse(Is, Js, Vs, length(rates), length(rcmap))
244+
# end
247245

248246
function fluxmat(::Type{Matrix{T}}, rcmap, rates) where T
249247
nr = length(rates)
250248
nc = length(rcmap)
251249
K = zeros(T, nr, nc)
252250
for (i, (complex, rxs)) in enumerate(rcmap)
253-
for (rx, dir) in rxs dir == -1 && (K[rx, i] = rates[rx])
251+
for (rx, dir) in rxs
252+
dir == -1 && (K[rx, i] = rates[rx])
254253
end
255254
end
256255
K
257256
end
258257

259-
function fluxmat(rn::ReactionSystem, pmap::Vector; sparse = false)
258+
function fluxmat(rn::ReactionSystem, pmap::Vector)
260259
pdict = Dict(pmap)
261-
fluxmat(rn, pdict; sparse)
260+
fluxmat(rn, pdict)
262261
end
263262

264-
function fluxmat(rn::ReactionSystem, pmap::Tuple; sparse = false)
263+
function fluxmat(rn::ReactionSystem, pmap::Tuple)
265264
pdict = Dict(pmap)
266-
fluxmat(rn, pdict; sparse)
265+
fluxmat(rn, pdict)
267266
end
268267

269268
# Helper to substitute values into a (vector of) symbolic expressions. The syms are the symbols to substitute and the symexprs are the expressions to substitute into.
@@ -307,7 +306,7 @@ function massactionvector(rn::ReactionSystem, scmap::Dict = Dict(); combinatoric
307306
push!(Φ, maprod)
308307
end
309308

310-
vtype == Num ? Vector{Any}(Φ) : Φ
309+
Vector{Any}(Φ)
311310
end
312311

313312
function massactionvector(rn::ReactionSystem, scmap::Tuple; combinatoric_ratelaws = Catalyst.get_combinatoric_ratelaws(rn))

test/network_analysis/network_properties.jl

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,8 @@ let
380380
@test isequal(Φ, truevec)
381381

382382
K = Catalyst.fluxmat(MAPK)
383-
# Construct matrix from incidence matrix
384-
mat = zeros(Num, 30, 26)
383+
# Construct flux matrix from incidence matrix
384+
mat = Matrix{Any}(zeros(30, 26))
385385
D = incidencemat(MAPK)
386386
rates = reactionrates(MAPK)
387387
for (i, col) in enumerate(eachcol(D))
@@ -396,13 +396,9 @@ let
396396
@test isequal(K[3, 2], MAPK.k₃)
397397
@test all(==(0), vcat(K[3,1], K[3,3:end]))
398398
@test count(k -> !isequal(k, 0), K) == length(reactions(MAPK))
399-
K = Catalyst.fluxmat(MAPK; sparse = true)
400-
@test Catalyst.issparse(K)
401399

402400
A_k = Catalyst.laplacianmat(MAPK)
403401
@test all(col -> sum(col) == 0, eachcol(A_k))
404-
A_k = Catalyst.laplacianmat(MAPK; sparse = true)
405-
@test Catalyst.issparse(A_k)
406402

407403
S = netstoichmat(MAPK)
408404
Y = complexstoichmat(MAPK)
@@ -419,13 +415,10 @@ let
419415
ratetup = Tuple(ratevec)
420416

421417
@test Catalyst.fluxmat(MAPK, ratemap) == Catalyst.fluxmat(MAPK, ratevec) == Catalyst.fluxmat(MAPK, ratetup)
422-
K = Catalyst.fluxmat(MAPK, ratemap; sparse = true)
423-
@test Catalyst.issparse(K)
424418

419+
K = Catalyst.fluxmat(MAPK, ratemap)
425420
A_k = Catalyst.laplacianmat(MAPK, ratemap)
426421
@test all(col -> sum(col) == 0, eachcol(A_k))
427-
A_k = Catalyst.laplacianmat(MAPK, ratemap; sparse = true)
428-
@test Catalyst.issparse(A_k)
429422

430423
numeqs = similar(eqs)
431424
for i in 1:length(eqs)

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ end
1515
### Run Tests ###
1616
@time begin
1717
if GROUP == "All" || GROUP == "Core"
18-
# Tests the `ReactionSystem` structure and its properties.
18+
Tests the `ReactionSystem` structure and its properties.
1919
@time @safetestset "Reaction Structure" begin include("reactionsystem_core/reaction.jl") end
2020
@time @safetestset "ReactionSystem Structure" begin include("reactionsystem_core/reactionsystem.jl") end
2121
@time @safetestset "Higher Order Reactions" begin include("reactionsystem_core/higher_order_reactions.jl") end

0 commit comments

Comments
 (0)