From 0c36fb344517cd5a569d35cd6c2660e68ac380c0 Mon Sep 17 00:00:00 2001 From: Sacha Verweij Date: Thu, 16 Jun 2016 21:05:10 -0700 Subject: [PATCH] Add backslash method specialized for `SparseMatrixCSC`s and a test checking that that specialized method gets called, a simple if inelegant fix for #16548. --- base/sparse/linalg.jl | 26 ++++++++++++++++++++++++++ test/sparsedir/sparse.jl | 5 ++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/base/sparse/linalg.jl b/base/sparse/linalg.jl index 1b24452007c97..05247ccf0343e 100644 --- a/base/sparse/linalg.jl +++ b/base/sparse/linalg.jl @@ -856,6 +856,32 @@ end scale!(A::SparseMatrixCSC, b::Number) = (scale!(A.nzval, b); A) scale!(b::Number, A::SparseMatrixCSC) = (scale!(b, A.nzval); A) +function (\)(A::SparseMatrixCSC, B::AbstractVecOrMat) + m, n = size(A) + if m == n + if istril(A) + if istriu(A) + return Diagonal(A) \ B + else + return LowerTriangular(A) \ B + end + elseif istriu(A) + return UpperTriangular(A) \ B + end + if ishermitian(A) + try + return cholfact(Hermitian(A)) \ B + catch e + isa(e, PosDefException) || rethrow(e) + return ldltfact(Hermitian(A)) \ B + end + end + return lufact(A) \ B + else + return qrfact(A) \ B + end +end + function factorize(A::SparseMatrixCSC) m, n = size(A) if m == n diff --git a/test/sparsedir/sparse.jl b/test/sparsedir/sparse.jl index 706a050be7daf..d4b941b0744fa 100644 --- a/test/sparsedir/sparse.jl +++ b/test/sparsedir/sparse.jl @@ -1415,4 +1415,7 @@ let m = 5 intmat = fill(1, m, m) ltintmat = LowerTriangular(rand(1:5, m, m)) @test isapprox(At_ldiv_B(ltintmat, sparse(intmat)), At_ldiv_B(ltintmat, intmat)) -end \ No newline at end of file +end + +# Test temporary fix for issue #16548 in PR #16979. Brittle. Expect to remove with `\` revisions. +@test which(\, (SparseMatrixCSC, AbstractVecOrMat)).module == Base.SparseArrays \ No newline at end of file