Skip to content

Commit

Permalink
specialize copyto! for Q from qr
Browse files Browse the repository at this point in the history
This fixes some performance problems reported in #38972 and #37102
(multiplying `Q` by a `Diagonal` or `UniformScaling`).
  • Loading branch information
ranocha committed Feb 5, 2021
1 parent 0ea19e4 commit c8e8b20
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions stdlib/LinearAlgebra/src/qr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,12 @@ function getindex(Q::AbstractQ, i::Integer, j::Integer)
return dot(x, lmul!(Q, y))
end

# specialization avoiding the fallback using slow `getindex`
function copyto!(dest::AbstractMatrix, src::LinearAlgebra.AbstractQ)
copyto!(dest, I)
lmul!(src, dest)
end

## Multiplication by Q
### QB
lmul!(A::QRCompactWYQ{T,S}, B::StridedVecOrMat{T}) where {T<:BlasFloat, S<:StridedMatrix} =
Expand Down
9 changes: 9 additions & 0 deletions stdlib/LinearAlgebra/test/qr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -322,4 +322,13 @@ end
end
end

@testset "QR factorization of Q" begin
for T in (Float32, Float64, ComplexF32, ComplexF64)
Q1, R1 = qr(randn(T,5,5))
Q2, R2 = qr(Q1)
@test Q1 Q2
@test R2 I
end
end

end # module TestQR

0 comments on commit c8e8b20

Please sign in to comment.