Skip to content

Commit 28c087d

Browse files
committed
Fix matrix inverse for varying dims
1 parent ef19282 commit 28c087d

File tree

7 files changed

+40
-28
lines changed

7 files changed

+40
-28
lines changed

src/GraphBasedSystems.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ include(joinpath("system", "graph_functions.jl"))
4747
include(joinpath("system", "interface.jl"))
4848
include(joinpath("system", "dense.jl"))
4949

50+
include(joinpath("solvers", "matrix.jl"))
5051
include(joinpath("solvers", "lu.jl"))
5152
include(joinpath("solvers", "llt.jl"))
5253
include(joinpath("solvers", "ldlt.jl"))

src/solvers/matrix.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Assumes quadratic matrices of same size
2+
function matrix_backsubsitution!(systemA::System{N}, B::SparseMatrixCSC{Entry, Int64}, backsubsitution!) where N
3+
vector_entries = systemA.vector_entries
4+
dims = systemA.dims
5+
6+
maxnnzC = Int(widelength(systemA.matrix_entries))
7+
C = _allocres((N,N), Int64, Entry, maxnnzC)
8+
# _densestructure!(C)
9+
10+
for i = 1:N
11+
for j = 1:N
12+
Bji = B[j,i]
13+
Bji isa Entry{Nothing} ? vector_entries[j] = Entry(dims[j], dims[i]) : vector_entries[j] = Bji
14+
end
15+
backsubsitution!(systemA)
16+
for j = 1:N
17+
C[j,i] = vector_entries[j]
18+
end
19+
end
20+
21+
return C
22+
end

src/system/system.jl

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -139,27 +139,4 @@ function Base.:/(S1::System, r::Real)
139139
return S2
140140
end
141141
Base.:\(r::Real, S::System) = S/r
142-
Base.:\(A::System, B::SparseMatrixCSC{Entry, Int64}) = ldu_matrix_solve!(A, B; keep_vector = true)
143-
144-
# Assumes quadratic matrices of same size
145-
function matrix_backsubsitution!(systemA::System{N}, B::SparseMatrixCSC{Entry, Int64}, backsubsitution!) where N
146-
vector_entries = systemA.vector_entries
147-
dims = systemA.dims
148-
149-
maxnnzC = Int(widelength(systemA.matrix_entries))
150-
C = _allocres((N,N), Int64, Entry, maxnnzC)
151-
_densestructure!(C)
152-
153-
for i = 1:N
154-
for j = 1:N
155-
Bji = B[j,i]
156-
Bji isa Entry{Nothing} ? vector_entries[j] = Entry(dims[i], dims[j]) : vector_entries[j] = Bji
157-
end
158-
backsubsitution!(systemA)
159-
for j = 1:N
160-
C[j,i] = vector_entries[j]
161-
end
162-
end
163-
164-
return C
165-
end
142+
Base.:\(A::System, B::SparseMatrixCSC{Entry, Int64}) = lu_matrix_solve!(A, B; keep_vector = true)

test/ldlt_test.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ for i=1:5
1515
end
1616

1717
for i=1:5
18-
system = System{Float64}(A, ones(Int64, size(A)[1]), symmetric=true)
18+
system = System{Float64}(A, rand(0:3, size(A)[1]), symmetric=true)
1919
initialize!(system)
2020
Bmat = deepcopy(system.matrix_entries)
2121
F2 = full_matrix(Bmat,false,system.dims,system.dims)

test/ldu_test.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ for i=1:5
1515
end
1616

1717
for i=1:5
18-
system = System{Float64}(A, ones(Int64, size(A)[1]))
18+
system = System{Float64}(A, rand(0:3, size(A)[1]))
1919
initialize!(system)
2020
Bmat = deepcopy(system.matrix_entries)
2121
F2 = full_matrix(Bmat,false,system.dims,system.dims)

test/llt_test.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ for i=1:5
2222
end
2323

2424
for i=1:5
25-
system = System{Float64}(A, ones(Int64, size(A)[1]), symmetric=true)
25+
system = System{Float64}(A, rand(0:3, size(A)[1]), symmetric=true)
2626
initialize!(system)
2727
Bmat = deepcopy(system.matrix_entries)
2828
F2 = full_matrix(Bmat,false,system.dims,system.dims)

test/lu_test.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ for i=1:5
1515
end
1616

1717
for i=1:5
18-
system = System{Float64}(A, ones(Int64, size(A)[1]))
18+
system = System{Float64}(A, rand(0:3, size(A)[1]))
1919
initialize!(system)
2020
Bmat = deepcopy(system.matrix_entries)
2121
F2 = full_matrix(Bmat,false,system.dims,system.dims)
@@ -26,6 +26,18 @@ for i=1:5
2626
@test maximum(abs.(full_matrix(Cmat,false,system.dims,system.dims)-F1\F2)) < 1e-3
2727
end
2828

29+
for i=1:5
30+
system = System{Float64}(A, rand(0:3, size(A)[1]))
31+
initialize!(system)
32+
Bmat = deepcopy(system.matrix_entries)
33+
F2 = full_matrix(Bmat,false,system.dims,system.dims)
34+
initialize!(system)
35+
F1 = full_matrix(system)
36+
37+
Cmat = system \ Bmat
38+
@test maximum(abs.(full_matrix(Cmat,false,system.dims,system.dims)-F1\F2)) < 1e-3
39+
end
40+
2941
system = System{Float64}(A, rand(0:3, size(A)[1]))
3042

3143
display(system)

0 commit comments

Comments
 (0)