Skip to content

Commit 717f213

Browse files
committed
Some fixes
1 parent ecaad98 commit 717f213

File tree

5 files changed

+29
-28
lines changed

5 files changed

+29
-28
lines changed

src/bipartite_graph.jl

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ badjlist = [[1,2,5,6],[3,4,6]]
6666
bg = BipartiteGraph(7, fadjlist, badjlist)
6767
```
6868
"""
69-
mutable struct BipartiteGraph{I<:Integer,F<:Vector{Vector{I}},B<:Union{Vector{Vector{I}},Nothing},M} <: LightGraphs.AbstractGraph{I}
69+
mutable struct BipartiteGraph{I<:Integer,F<:Vector{Vector{I}},B<:Union{Vector{Vector{I}},I},M} <: LightGraphs.AbstractGraph{I}
7070
ne::Int
7171
fadjlist::F # `fadjlist[src] => dsts`
72-
badjlist::B # `badjlist[dst] => srcs`
72+
badjlist::B # `badjlist[dst] => srcs` or `ndsts`
7373
metadata::M
7474
end
75-
BipartiteGraph(ne::Integer, fadj::AbstractVector, badj::Union{AbstractVector}=nothing) = BipartiteGraph(ne, fadj, badj, nothing)
75+
BipartiteGraph(ne::Integer, fadj::AbstractVector, badj::Union{AbstractVector}=maximum(maximum, fadj); metadata=nothing) = BipartiteGraph(ne, fadj, badj, metadata)
7676

7777
"""
7878
```julia
@@ -84,9 +84,7 @@ Test whether two [`BipartiteGraph`](@ref)s are equal.
8484
function Base.isequal(bg1::BipartiteGraph{T}, bg2::BipartiteGraph{T}) where {T<:Integer}
8585
iseq = (bg1.ne == bg2.ne)
8686
iseq &= (bg1.fadjlist == bg2.fadjlist)
87-
if bg1.badjlist !== nothing && bg2.badjlist !== nothing
88-
iseq &= (bg1.badjlist == bg2.badjlist)
89-
end
87+
iseq &= (bg1.badjlist == bg2.badjlist)
9088
iseq
9189
end
9290

@@ -97,14 +95,18 @@ Build an empty `BipartiteGraph` with `nsrcs` sources and `ndsts` destinations.
9795
"""
9896
function BipartiteGraph(nsrcs::T, ndsts::T, backedge::Val{B}=Val(true); metadata=nothing) where {T,B}
9997
fadjlist = map(_->T[], 1:nsrcs)
100-
badjlist = B ? map(_->T[], 1:ndsts) : nothing
98+
badjlist = B ? map(_->T[], 1:ndsts) : ndsts
10199
BipartiteGraph(0, fadjlist, badjlist, metadata)
102100
end
103101

104102
Base.eltype(::Type{<:BipartiteGraph{I}}) where I = I
105103
function Base.empty!(g::BipartiteGraph)
106104
foreach(empty!, g.fadjlist)
107-
g.badjlist === nothing || foreach(empty!, g.badjlist)
105+
if g.badjlist isa AbstractVector
106+
foreach(empty!, g.badjlist)
107+
else
108+
g.badjlist = 0
109+
end
108110
g.ne = 0
109111
if g.metadata !== nothing
110112
foreach(empty!, g.metadata)
@@ -121,12 +123,12 @@ end
121123
LightGraphs.is_directed(::Type{<:BipartiteGraph}) = false
122124
LightGraphs.vertices(g::BipartiteGraph) = (𝑠vertices(g), 𝑑vertices(g))
123125
𝑠vertices(g::BipartiteGraph) = axes(g.fadjlist, 1)
124-
𝑑vertices(g::BipartiteGraph) = g.badjlist === nothing ? throw_no_back_edges() : axes(g.badjlist, 1)
126+
𝑑vertices(g::BipartiteGraph) = g.badjlist isa AbstractVector ? axes(g.badjlist, 1) : Base.OneTo(g.badjlist)
125127
has_𝑠vertex(g::BipartiteGraph, v::Integer) = v in 𝑠vertices(g)
126128
has_𝑑vertex(g::BipartiteGraph, v::Integer) = v in 𝑑vertices(g)
127129
𝑠neighbors(g::BipartiteGraph, i::Integer, with_metadata::Val{M}=Val(false)) where M = M ? zip(g.fadjlist[i], g.metadata[i]) : g.fadjlist[i]
128130
function 𝑑neighbors(g::BipartiteGraph, j::Integer, with_metadata::Val{M}=Val(false)) where M
129-
g.badjlist === nothing && throw_no_back_edges()
131+
g.badjlist isa AbstractVector || throw_no_back_edges()
130132
M ? zip(g.badjlist[j], (g.metadata[i][j] for i in g.badjlist[j])) : g.badjlist[j]
131133
end
132134
LightGraphs.ne(g::BipartiteGraph) = g.ne
@@ -152,7 +154,6 @@ const NO_METADATA = NoMetadata()
152154
LightGraphs.add_edge!(g::BipartiteGraph, i::Integer, j::Integer, md=NO_METADATA) = add_edge!(g, BipartiteEdge(i, j), md)
153155
function LightGraphs.add_edge!(g::BipartiteGraph, edge::BipartiteEdge, md=NO_METADATA)
154156
@unpack fadjlist, badjlist = g
155-
verts = vertices(g)
156157
s, d = src(edge), dst(edge)
157158
(has_𝑠vertex(g, s) && has_𝑑vertex(g, d)) || error("edge ($edge) out of range.")
158159
@inbounds list = fadjlist[s]
@@ -164,7 +165,7 @@ function LightGraphs.add_edge!(g::BipartiteGraph, edge::BipartiteEdge, md=NO_MET
164165
end
165166

166167
g.ne += 1
167-
if badjlist !== nothing
168+
if badjlist isa AbstractVector
168169
@inbounds list = badjlist[d]
169170
index = searchsortedfirst(list, s)
170171
insert!(list, index, s)
@@ -173,8 +174,12 @@ function LightGraphs.add_edge!(g::BipartiteGraph, edge::BipartiteEdge, md=NO_MET
173174
end
174175

175176
function LightGraphs.add_vertex!(g::BipartiteGraph{T}, type::VertType) where T
176-
if type === DST && g.badjlist !== nothing
177-
push!(g.badjlist, T[])
177+
if type === DST
178+
if g.badjlist isa AbstractVector
179+
push!(g.badjlist, T[])
180+
else
181+
g.badjlist += 1
182+
end
178183
elseif type === SRC
179184
push!(g.fadjlist, T[])
180185
else

src/systems/systemstructure.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ Base.@kwdef struct SystemStructure
5050
inv_varassoc::Vector{Int}
5151
varmask::BitVector # `true` if the variable has the highest order derivative
5252
algeqs::BitVector
53-
graph::BipartiteGraph{Int,Vector{Vector{Int}},Nothing,Nothing}
54-
solvable_graph::BipartiteGraph{Int,Vector{Vector{Int}},Nothing,Nothing}
53+
graph::BipartiteGraph{Int,Vector{Vector{Int}},Int,Nothing}
54+
solvable_graph::BipartiteGraph{Int,Vector{Vector{Int}},Int,Nothing}
5555
assign::Vector{Int}
5656
inv_assign::Vector{Int}
5757
scc::Vector{Vector{Int}}

test/reduction.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ test_equal.(equations(reduced_system), reduced_eqs)
120120

121121
observed_eqs = [
122122
s ~ lorenz2.y
123+
a ~ lorenz2.y - lorenz1.x
123124
lorenz1.F ~ -((lorenz2.z) - (lorenz2.x) - (lorenz2.y))
124125
lorenz2.F ~ -((lorenz1.z) - (lorenz1.x) - (lorenz1.y))
125-
a ~ s - (lorenz1.x)
126126
lorenz2.u ~ lorenz1.F
127127
lorenz1.u ~ lorenz2.F
128128
]

test/structural_transformation/tearing.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ eqs = [
145145
daesys = ODESystem(eqs, t)
146146
newdaesys = tearing(daesys)
147147
@test equations(newdaesys) == [D(x) ~ z; 0 ~ x + sin(z) - p*t]
148-
@test isequal(states(newdaesys), [x, z])
148+
@test isequal(states(newdaesys), [z, x])
149149
prob = ODAEProblem(newdaesys, [x=>1.0], (0, 1.0), [p=>0.2])
150150
du = [0.0]; u = [1.0]; pr = 0.2; tt = 0.1
151151
@test (@ballocated $(prob.f)($du, $u, $pr, $tt)) == 0

test/structural_transformation/utils.jl

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,16 @@ sys = initialize_system_structure(pendulum)
2020
StructuralTransformations.find_solvables!(sys)
2121
sss = structure(sys)
2222
@unpack graph, solvable_graph, fullvars, varassoc = sss
23-
@test isequal(fullvars, [x, y, w, z, D(x), D(y), D(w), D(z), T])
24-
@test graph.fadjlist == sort.([[5, 3], [6, 4], [7, 1, 9], [8, 2, 9], [2, 1]])
25-
@test graph.badjlist == sort.([[3, 5], [4, 5], [1], [2], [1], [2], [3], [4], [3, 4]])
23+
@test isequal(fullvars, [w, D(x), x, z, D(y), y, T, D(w), D(z)])
24+
@test graph.fadjlist == sort.([[1, 2], [4, 5], [3, 7, 8], [6, 7, 9], [3, 6]])
25+
@test graph.badjlist == 9 == length(fullvars)
2626
@test ne(graph) == nnz(incidence_matrix(graph)) == 12
2727
@test nv(solvable_graph) == nv(solvable_graph) == 9 + 5
28-
@test varassoc == [5, 6, 7, 8, 0, 0, 0, 0, 0]
28+
@test varassoc == [8, 0, 2, 9, 0, 5, 0, 0, 0]
2929

3030
se = collect(StructuralTransformations.𝑠edges(graph))
3131
@test se == mapreduce(vcat, enumerate(graph.fadjlist)) do (s, d)
3232
StructuralTransformations.BipartiteEdge.(s, d)
3333
end
34-
de = collect(StructuralTransformations.𝑑edges(graph))
35-
@test de == mapreduce(vcat, enumerate(graph.badjlist)) do (d, s)
36-
StructuralTransformations.BipartiteEdge.(s, d)
37-
end
38-
ae = collect(StructuralTransformations.edges(graph))
39-
@test ae == vcat(se, de)
34+
@test_throws ArgumentError collect(StructuralTransformations.𝑑edges(graph))
35+
@test_throws ArgumentError collect(StructuralTransformations.edges(graph))

0 commit comments

Comments
 (0)