Skip to content

Commit 31b491e

Browse files
authored
Specialize multiplication of AbstractQ with sparse arrays (#317)
1 parent 72827cd commit 31b491e

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

src/sparsematrix.jl

+5
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,11 @@ function _show_with_braille_patterns(io::IO, S::AbstractSparseMatrixCSCInclAdjoi
469469
foreach(c -> print(io, Char(c)), @view brailleGrid[1:end-1])
470470
end
471471

472+
(*)(Q::AbstractQ, B::AbstractSparseMatrixCSC) = Q * Matrix(B)
473+
(*)(Q::AbstractQ, B::AdjOrTrans{<:Any,<:AbstractSparseMatrixCSC}) = Q * copy(B)
474+
(*)(A::AbstractSparseMatrixCSC, Q::AbstractQ) = Matrix(A) * Q
475+
(*)(A::AdjOrTrans{<:Any,<:AbstractSparseMatrixCSC}, Q::AbstractQ) = copy(A) * Q
476+
472477
## Reshape
473478

474479
function sparse_compute_reshaped_colptr_and_rowval!(colptrS::Vector{Ti}, rowvalS::Vector{Ti},

src/sparsevector.jl

+3
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,9 @@ end
12921292
# zero-preserving functions (z->z, nz->nz)
12931293
-(x::SparseVector) = SparseVector(length(x), copy(nonzeroinds(x)), -nonzeros(x))
12941294

1295+
(*)(Q::AbstractQ, B::AbstractSparseVector) = Q * Vector(B)
1296+
(*)(A::AbstractSparseVector, Q::AbstractQ) = Vector(A) * Q
1297+
12951298
# functions f, such that
12961299
# f(x) can be zero or non-zero when x != 0
12971300
# f(x) = 0 when x == 0

test/spqr.jl

+6
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ end
152152
perm = inv(Matrix(I, size(A)...)[q.prow, :])
153153
f = sum(q.R; dims=2) ./ sum(Dq.R; dims=2)
154154
@test perm * (transpose(f) .* sQ) sparse(Dq.Q)
155+
v, V = sprandn(100, 0.01), sprandn(100, 100, 0.01)
156+
@test Dq.Q * v Matrix(Dq.Q) * v
157+
@test Dq.Q * V Matrix(Dq.Q) * V
158+
@test Dq.Q * V' Matrix(Dq.Q) * V'
159+
@test V * Dq.Q V * Matrix(Dq.Q)
160+
@test V' * Dq.Q V' * Matrix(Dq.Q)
155161
end
156162

157163
@testset "no strategies" begin

0 commit comments

Comments
 (0)