Skip to content

Commit 0c36fb3

Browse files
committed
Add backslash method specialized for SparseMatrixCSCs and a test checking that that specialized method gets called, a simple if inelegant fix for #16548.
1 parent f65dea6 commit 0c36fb3

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

base/sparse/linalg.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,32 @@ end
856856
scale!(A::SparseMatrixCSC, b::Number) = (scale!(A.nzval, b); A)
857857
scale!(b::Number, A::SparseMatrixCSC) = (scale!(b, A.nzval); A)
858858

859+
function (\)(A::SparseMatrixCSC, B::AbstractVecOrMat)
860+
m, n = size(A)
861+
if m == n
862+
if istril(A)
863+
if istriu(A)
864+
return Diagonal(A) \ B
865+
else
866+
return LowerTriangular(A) \ B
867+
end
868+
elseif istriu(A)
869+
return UpperTriangular(A) \ B
870+
end
871+
if ishermitian(A)
872+
try
873+
return cholfact(Hermitian(A)) \ B
874+
catch e
875+
isa(e, PosDefException) || rethrow(e)
876+
return ldltfact(Hermitian(A)) \ B
877+
end
878+
end
879+
return lufact(A) \ B
880+
else
881+
return qrfact(A) \ B
882+
end
883+
end
884+
859885
function factorize(A::SparseMatrixCSC)
860886
m, n = size(A)
861887
if m == n

test/sparsedir/sparse.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1415,4 +1415,7 @@ let m = 5
14151415
intmat = fill(1, m, m)
14161416
ltintmat = LowerTriangular(rand(1:5, m, m))
14171417
@test isapprox(At_ldiv_B(ltintmat, sparse(intmat)), At_ldiv_B(ltintmat, intmat))
1418-
end
1418+
end
1419+
1420+
# Test temporary fix for issue #16548 in PR #16979. Brittle. Expect to remove with `\` revisions.
1421+
@test which(\, (SparseMatrixCSC, AbstractVecOrMat)).module == Base.SparseArrays

0 commit comments

Comments
 (0)