Skip to content

Commit d763a37

Browse files
Fix non-square sparse matrix preallocations for defaults
Part 2 of fixes SciML/NonlinearSolve.jl#599
1 parent 1fca18c commit d763a37

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

ext/LinearSolveSparseArraysExt.jl

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,14 @@ function LinearSolve.init_cacheval(
8383
maxiters::Int, abstol,
8484
reltol,
8585
verbose::Bool, assumptions::OperatorAssumptions)
86-
A = convert(AbstractMatrix, A)
87-
zerobased = SparseArrays.getcolptr(A)[1] == 0
88-
return SparseArrays.UMFPACK.UmfpackLU(SparseMatrixCSC(size(A)..., getcolptr(A),
89-
rowvals(A), nonzeros(A)))
86+
if size(A,1) == size(A,2)
87+
A = convert(AbstractMatrix, A)
88+
zerobased = SparseArrays.getcolptr(A)[1] == 0
89+
return SparseArrays.UMFPACK.UmfpackLU(SparseMatrixCSC(size(A)..., getcolptr(A),
90+
rowvals(A), nonzeros(A)))
91+
else
92+
PREALLOCATED_UMFPACK
93+
end
9094
end
9195

9296
function SciMLBase.solve!(
@@ -141,9 +145,13 @@ function LinearSolve.init_cacheval(
141145
maxiters::Int, abstol,
142146
reltol,
143147
verbose::Bool, assumptions::OperatorAssumptions)
144-
A = convert(AbstractMatrix, A)
145-
return KLU.KLUFactorization(SparseMatrixCSC(size(A)..., getcolptr(A), rowvals(A),
146-
nonzeros(A)))
148+
if size(A,1) == size(A,2)
149+
A = convert(AbstractMatrix, A)
150+
return KLU.KLUFactorization(SparseMatrixCSC(size(A)..., getcolptr(A), rowvals(A),
151+
nonzeros(A)))
152+
else
153+
PREALLOCATED_KLU
154+
end
147155
end
148156

149157
# TODO: guard this against errors

test/default_algs.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,18 @@ cache.A = [2.0 1.0
144144
sol = solve!(cache)
145145

146146
@test !SciMLBase.successful_retcode(sol.retcode)
147+
148+
## Non-square Sparse Defaults
149+
# https://github.com/SciML/NonlinearSolve.jl/issues/599
150+
151+
A = SparseMatrixCSC{Float64, Int32}([
152+
1.0 0.0
153+
1.0 1.0
154+
])
155+
b = ones(2)
156+
A2 = hcat(A,A)
157+
prob = LinearProblem(A, b)
158+
@test SciMLBase.successful_retcode(solve(prob))
159+
160+
prob2 = LinearProblem(A2, b)
161+
@test SciMLBase.successful_retcode(solve(prob2))

0 commit comments

Comments
 (0)