From 8cb896c9e1106f1d1c3835df843e81375aecf0e5 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Wed, 30 Oct 2024 16:40:54 +0100 Subject: [PATCH] add a `connection_index` function for getting the index of a specific connection matrix --- src/GraphDynamics.jl | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/GraphDynamics.jl b/src/GraphDynamics.jl index 07bd3c3..aa03eab 100644 --- a/src/GraphDynamics.jl +++ b/src/GraphDynamics.jl @@ -32,6 +32,7 @@ end isstochastic, event_times, + connection_index ) export @@ -241,6 +242,23 @@ Base.getindex(m::ConnectionMatrices, i) = m.matrices[i] Base.length(m::ConnectionMatrices) = length(m.matrices) Base.size(m::ConnectionMatrix{N}) where {N} = (N, N) +""" + connection_index(ConnType, M::ConnectionMatrices) + +give the first index `n` such that `M[n]` is a `ConnectionMatrix{N, ConnType} where {N}`, or throw an error if no such index exists. +""" +connection_index(::Type{ConnType}, M::ConnectionMatrices) where {ConnType} = _conn_index(ConnType, M.matrices, 1) +function _conn_index(::Type{ConnType}, tup::Tuple, i) where {ConnType} + if first(tup) isa ConnectionMatrix{N, ConnType} where {N} + return i + else + _conn_index(ConnType, Base.tail(tup), i+1) + end +end +@noinline _conn_index(::Type{ConnType}, ::Tuple{}, _) where {ConnType} = + error("ConnectionMatrices did not contain a ConnectionMatrix with connection type ", ConnType) + + abstract type GraphSystem end @kwdef struct ODEGraphSystem{CM <: ConnectionMatrices, S, P, EVT, CDEP, CCEP, Ns, SNM, PNM} <: GraphSystem