3
3
idx = VIndex(comp, sub)
4
4
5
5
A symbolic index for a vertex state variable.
6
- - `comp`: the component index, either int or a collection of ints
6
+ - `comp`: the component index, either int, symbol or a collection
7
7
- `sub`: the subindex, either int, symbol or a collection of those.
8
8
9
9
```
10
10
VIndex(1, :P) # vertex 1, variable :P
11
11
VIndex(1:5, 1) # first state of vertices 1 to 5
12
12
VIndex(7, (:x,:y)) # states :x and :y of vertex 7
13
13
VIndex(2) # references the second vertex model
14
+ VIndex(:a) # references vertex with unique name :a
14
15
```
15
16
16
17
Can be used to index into objects supporting the `SymbolicIndexingInterface`,
@@ -28,14 +29,16 @@ VIndex(ci::Union{Symbol,Int}) = VIndex(ci, nothing)
28
29
idx = EIndex(comp, sub)
29
30
30
31
A symbolic index for an edge state variable.
31
- - `comp`: the component index, either int or a collection of ints
32
+ - `comp`: the component index, either int, symbol, pair or a collection
32
33
- `sub`: the subindex, either int, symbol or a collection of those.
33
34
34
35
```
35
36
EIndex(1, :P) # edge 1, variable :P
36
37
EIndex(1:5, 1) # first state of edges 1 to 5
37
38
EIndex(7, (:x,:y)) # states :x and :y of edge 7
38
39
EIndex(2) # references the second edge model
40
+ EIndex(1=>2) # references edge from v1 to v2
41
+ EIndex(:a=>:b) # references edge from (uniquely named) vertex :a to :b
39
42
```
40
43
41
44
Can be used to index into objects supporting the `SymbolicIndexingInterface`,
@@ -53,7 +56,7 @@ EIndex(ci::Union{Symbol,Int}) = EIndex(ci, nothing)
53
56
idx = VPIndex(comp, sub)
54
57
55
58
A symbolic index into the parameter a vertex:
56
- - `comp`: the component index, either int or a collection of ints
59
+ - `comp`: the component index, either int, symbol or a collection
57
60
- `sub`: the subindex, either int, symbol or a collection of those.
58
61
59
62
Can be used to index into objects supporting the `SymbolicIndexingInterface`,
69
72
EPIndex{C,S} <: SymbolicStateIndex{C,S}
70
73
idx = VEIndex(comp, sub)
71
74
72
- A symbolic index into the parameter a vertex :
73
- - `comp`: the component index, either int or a collection of ints
75
+ A symbolic index into the parameter of an edge :
76
+ - `comp`: the component index, either int, symbol, pair or a collection
74
77
- `sub`: the subindex, either int, symbol or a collection of those.
75
78
76
79
Can be used to index into objects supporting the `SymbolicIndexingInterface`,
@@ -95,18 +98,25 @@ SSI Maintainer assured that f.sys is really only used for symbolic indexig so me
95
98
SciMLBase. __has_sys (nw:: Network ) = true
96
99
Base. getproperty (nw:: Network , s:: Symbol ) = s=== :sys ? nw : getfield (nw, s)
97
100
98
- SII. symbolic_type (:: Type{<:SymbolicIndex{<:Union{Symbol,Int},<:Union{Symbol,Int}}} ) = SII. ScalarSymbolic ()
101
+ SII. symbolic_type (:: Type{<:SymbolicIndex{<:Union{<:Pair, Symbol,Int},<:Union{Symbol,Int}}} ) = SII. ScalarSymbolic ()
99
102
SII. symbolic_type (:: Type{<:SymbolicIndex} ) = SII. ArraySymbolic ()
100
103
101
104
SII. hasname (:: SymbolicIndex ) = false
102
- SII. hasname (:: SymbolicIndex{<:Union{Symbol,Int},<:Union{Symbol,Int}} ) = true
105
+ SII. hasname (:: SymbolicIndex{<:Union{<:Pair, Symbol,Int},<:Union{Symbol,Int}} ) = true
103
106
function SII. getname (x:: SymbolicVertexIndex )
104
107
prefix = x. compidx isa Int ? :v : Symbol ()
105
108
Symbol (prefix, Symbol (x. compidx), :₊ , Symbol (x. subidx))
106
109
end
107
110
function SII. getname (x:: SymbolicEdgeIndex )
108
- prefix = x. compidx isa Int ? :e : Symbol ()
109
- Symbol (prefix, Symbol (x. compidx), :₊ , Symbol (x. subidx))
111
+ if x. compidx isa Pair
112
+ src, dst = x. compidx
113
+ _src = src isa Int ? Symbol (:v , src) : Symbol (src)
114
+ _dst = dst isa Int ? Symbol (:v , dst) : Symbol (dst)
115
+ Symbol (_src, " ₜₒ" , _dst, :₊ , Symbol (x. subidx))
116
+ else
117
+ prefix = x. compidx isa Int ? :e : Symbol ()
118
+ Symbol (prefix, Symbol (x. compidx), :₊ , Symbol (x. subidx))
119
+ end
110
120
end
111
121
112
122
resolvecompidx (nw:: Network , sni) = resolvecompidx (nw. im, sni)
@@ -119,20 +129,49 @@ function resolvecompidx(im::IndexManager, sni::SymbolicIndex{Symbol})
119
129
throw (ArgumentError (" Could not resolve component index for $sni , the name might not be unique?" ))
120
130
end
121
131
end
132
+ function resolvecompidx (im:: IndexManager , sni:: SymbolicEdgeIndex{<:Pair} )
133
+ src, dst = sni. compidx
134
+
135
+ src_i = try
136
+ resolvecompidx (im, VIndex (src))
137
+ catch
138
+ throw (ArgumentError (" Could not resolve edge source $src " ))
139
+ end
140
+ dst_i = try
141
+ resolvecompidx (im, VIndex (dst))
142
+ catch
143
+ throw (ArgumentError (" Could not resolve edge destination $dst " ))
144
+ end
145
+
146
+ eidx = findfirst (im. edgevec) do e
147
+ e. src == src_i && e. dst == dst_i
148
+ end
149
+ if isnothing (eidx)
150
+ reverse = findfirst (im. edgevec) do e
151
+ e. src == dst_i && e. dst == src_i
152
+ end
153
+ err = " Invalid Index: Network does not contain edge from $(src) => $(dst) !"
154
+ if ! isnothing (reverse)
155
+ err *= " Maybe you meant the reverse edge from $(dst) => $(src) ?"
156
+ end
157
+ throw (ArgumentError (err))
158
+ end
159
+ return eidx
160
+ end
122
161
getcomp (nw:: Network , sni) = getcomp (nw. im, sni)
123
162
getcomp (im:: IndexManager , sni:: SymbolicEdgeIndex ) = im. edgem[resolvecompidx (im, sni)]
124
163
getcomp (im:: IndexManager , sni:: SymbolicVertexIndex ) = im. vertexm[resolvecompidx (im, sni)]
125
164
126
165
getcomprange (nw:: Network , sni) = getcomprange (nw. im, sni)
127
166
getcomprange (im:: IndexManager , sni:: VIndex{<:Union{Symbol,Int}} ) = im. v_data[resolvecompidx (im, sni)]
128
- getcomprange (im:: IndexManager , sni:: EIndex{<:Union{Symbol,Int}} ) = im. e_data[resolvecompidx (im, sni)]
167
+ getcomprange (im:: IndexManager , sni:: EIndex{<:Union{<:Pair, Symbol,Int}} ) = im. e_data[resolvecompidx (im, sni)]
129
168
130
169
getcompoutrange (nw:: Network , sni) = getcompoutrange (nw. im, sni)
131
170
getcompoutrange (im:: IndexManager , sni:: VIndex{<:Union{Symbol,Int}} ) = im. v_out[resolvecompidx (im, sni)]
132
- getcompoutrange (im:: IndexManager , sni:: EIndex{<:Union{Symbol,Int}} ) = flatrange (im. e_out[resolvecompidx (im, sni)])
171
+ getcompoutrange (im:: IndexManager , sni:: EIndex{<:Union{<:Pair, Symbol,Int}} ) = flatrange (im. e_out[resolvecompidx (im, sni)])
133
172
134
173
getcompprange (nw:: Network , sni:: SymbolicVertexIndex{<:Union{Symbol,Int}} ) = nw. im. v_para[resolvecompidx (nw, sni)]
135
- getcompprange (nw:: Network , sni:: SymbolicEdgeIndex{<:Union{Symbol,Int}} ) = nw. im. e_para[resolvecompidx (nw, sni)]
174
+ getcompprange (nw:: Network , sni:: SymbolicEdgeIndex{<:Union{<:Pair, Symbol,Int}} ) = nw. im. e_para[resolvecompidx (nw, sni)]
136
175
137
176
subsym_has_idx (sym:: Symbol , syms) = sym ∈ syms
138
177
subsym_has_idx (idx:: Int , syms) = 1 ≤ idx ≤ length (syms)
@@ -143,7 +182,7 @@ subsym_to_idx(idx::Int, _) = idx
143
182
# ### Iterator/Broadcast interface for ArraySymbolic types
144
183
# ###
145
184
# TODO : not broadcasting over idx with colon is weird
146
- Base. broadcastable (si:: SymbolicIndex{<:Union{Int,Symbol,Colon},<:Union{Int,Symbol,Colon}} ) = Ref (si)
185
+ Base. broadcastable (si:: SymbolicIndex{<:Union{Int,Symbol,<:Pair, Colon},<:Union{Int,Symbol,Colon}} ) = Ref (si)
147
186
148
187
const _IterableComponent = SymbolicIndex{<: Union{AbstractVector,Tuple} ,<: Union{Int,Symbol} }
149
188
Base. length (si:: _IterableComponent ) = length (si. compidx)
@@ -166,7 +205,7 @@ function Base.iterate(si::_IterableComponent, state=nothing)
166
205
_similar (si, it[1 ], si. subidx), it[2 ]
167
206
end
168
207
169
- const _IterableSubcomponent = SymbolicIndex{<: Union{Symbol,Int} ,<: Union{AbstractVector,Tuple} }
208
+ const _IterableSubcomponent = SymbolicIndex{<: Union{<:Pair, Symbol,Int} ,<: Union{AbstractVector,Tuple} }
170
209
Base. length (si:: _IterableSubcomponent ) = length (si. subidx)
171
210
Base. size (si:: _IterableSubcomponent ) = (length (si),)
172
211
Base. IteratorSize (si:: _IterableSubcomponent ) = Base. HasShape {1} ()
@@ -200,9 +239,9 @@ _resolve_colon(nw::Network, sni::EIndex{Colon}) = EIndex(1:ne(nw), sni.subidx)
200
239
_resolve_colon (nw:: Network , sni:: VPIndex{Colon} ) = VPIndex (1 : nv (nw), sni. subidx)
201
240
_resolve_colon (nw:: Network , sni:: EPIndex{Colon} ) = EPIndex (1 : ne (nw), sni. subidx)
202
241
_resolve_colon (nw:: Network , sni:: VIndex{<:Union{Symbol,Int},Colon} ) = VIndex {Int, UnitRange{Int}} (sni. compidx, 1 : dim (getcomp (nw,sni)))
203
- _resolve_colon (nw:: Network , sni:: EIndex{<:Union{Symbol,Int},Colon} ) = EIndex {Int, UnitRange{Int}} (sni. compidx, 1 : dim (getcomp (nw,sni)))
242
+ _resolve_colon (nw:: Network , sni:: EIndex{<:Union{<:Pair, Symbol,Int},Colon} ) = EIndex {Int, UnitRange{Int}} (sni. compidx, 1 : dim (getcomp (nw,sni)))
204
243
_resolve_colon (nw:: Network , sni:: VPIndex{<:Union{Symbol,Int},Colon} ) = VPIndex {Int, UnitRange{Int}} (sni. compidx, 1 : pdim (getcomp (nw,sni)))
205
- _resolve_colon (nw:: Network , sni:: EPIndex{<:Union{Symbol,Int},Colon} ) = EPIndex {Int, UnitRange{Int}} (sni. compidx, 1 : pdim (getcomp (nw,sni)))
244
+ _resolve_colon (nw:: Network , sni:: EPIndex{<:Union{<:Pair, Symbol,Int},Colon} ) = EPIndex {Int, UnitRange{Int}} (sni. compidx, 1 : pdim (getcomp (nw,sni)))
206
245
207
246
208
247
# ### Implmentation of index provider interface
@@ -220,7 +259,7 @@ SII.all_symbols(nw::Network) = vcat(SII.all_variable_symbols(nw), SII.parameter_
220
259
# ###
221
260
# ### variable indexing
222
261
# ###
223
- const POTENTIAL_SCALAR_SIDX = Union{SymbolicStateIndex{<: Union{Symbol,Int} ,<: Union{Int,Symbol} }}
262
+ const POTENTIAL_SCALAR_SIDX = Union{SymbolicStateIndex{<: Union{<:Pair, Symbol,Int} ,<: Union{Int,Symbol} }}
224
263
function SII. is_variable (nw:: Network , sni)
225
264
if _hascolon (sni)
226
265
SII. is_variable (nw, _resolve_colon (nw,sni))
277
316
# ###
278
317
# when using an number instead of symbol only PIndex is valid
279
318
const POTENTIAL_SCALAR_PIDX = Union{
280
- SymbolicParameterIndex{<: Union{Symbol,Int} ,<: Union{Int,Symbol} },
281
- SymbolicIndex{<: Union{Symbol,Int} ,Symbol}
319
+ SymbolicParameterIndex{<: Union{<:Pair, Symbol,Int} ,<: Union{Int,Symbol} },
320
+ SymbolicIndex{<: Union{<:Pair, Symbol,Int} ,Symbol}
282
321
}
283
322
function SII. is_parameter (nw:: Network , sni)
284
323
if _hascolon (sni)
@@ -410,7 +449,7 @@ function SII.is_observed(nw::Network, sni)
410
449
end
411
450
end
412
451
_is_observed (nw:: Network , _) = false
413
- function _is_observed (nw:: Network , sni:: SymbolicStateIndex{<:Union{Symbol,Int},Symbol} )
452
+ function _is_observed (nw:: Network , sni:: SymbolicStateIndex{<:Union{<:Pair, Symbol,Int},Symbol} )
414
453
cf = getcomp (nw, sni)
415
454
return sni. subidx ∈ obssym_all (cf)
416
455
end
@@ -1212,7 +1251,7 @@ Base.getindex(s::NWState, idx::ObservableExpression) = SII.getu(s, idx)(s)
1212
1251
Base. getindex (s:: NWParameter , idx:: ObservableExpression ) = SII. getp (s, idx)(s)
1213
1252
1214
1253
# using getindex to access component models
1215
- function Base. getindex (nw:: Network , i:: EIndex{<:Union{Symbol,Int}, Nothing} )
1254
+ function Base. getindex (nw:: Network , i:: EIndex{<:Union{<:Pair, Symbol,Int}, Nothing} )
1216
1255
return getcomp (nw, i)
1217
1256
end
1218
1257
function Base. getindex (nw:: Network , i:: VIndex{<:Union{Symbol,Int}, Nothing} )
0 commit comments