Skip to content

Commit 9bc292d

Browse files
authored
Improve AbstractQ coverage (#1160)
1 parent a622302 commit 9bc292d

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

src/abstractq.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ QRPackedQ(factors::AbstractMatrix{T}, τ::AbstractVector{T}) where {T} =
277277
QRPackedQ{T,typeof(factors),typeof(τ)}(factors, τ)
278278
QRPackedQ{T}(factors::AbstractMatrix, τ::AbstractVector) where {T} =
279279
QRPackedQ(convert(AbstractMatrix{T}, factors), convert(AbstractVector{T}, τ))
280+
QRPackedQ{T}(Q::QRPackedQ) where {T} = QRPackedQ{T}(Q.factors, Q.τ)
280281
# backwards-compatible constructors (remove with Julia 2.0)
281282
@deprecate(QRPackedQ{T,S}(factors::AbstractMatrix{T}, τ::AbstractVector{T}) where {T,S},
282283
QRPackedQ{T,S,typeof(τ)}(factors, τ), false)
@@ -300,12 +301,11 @@ QRCompactWYQ(factors::AbstractMatrix{S}, T::AbstractMatrix{S}) where {S} =
300301
QRCompactWYQ{S,typeof(factors),typeof(T)}(factors, T)
301302
QRCompactWYQ{S}(factors::AbstractMatrix, T::AbstractMatrix) where {S} =
302303
QRCompactWYQ(convert(AbstractMatrix{S}, factors), convert(AbstractMatrix{S}, T))
304+
QRCompactWYQ{S}(Q::QRCompactWYQ) where {S} = QRCompactWYQ{S}(Q.factors, Q.T)
303305
# backwards-compatible constructors (remove with Julia 2.0)
304306
@deprecate(QRCompactWYQ{S,M}(factors::AbstractMatrix{S}, T::AbstractMatrix{S}) where {S,M},
305307
QRCompactWYQ{S,M,typeof(T)}(factors, T), false)
306308

307-
QRPackedQ{T}(Q::QRPackedQ) where {T} = QRPackedQ(convert(AbstractMatrix{T}, Q.factors), convert(AbstractVector{T}, Q.τ))
308-
QRCompactWYQ{S}(Q::QRCompactWYQ) where {S} = QRCompactWYQ(convert(AbstractMatrix{S}, Q.factors), convert(AbstractMatrix{S}, Q.T))
309309

310310
# override generic square fallback
311311
Matrix{T}(Q::Union{QRCompactWYQ{S},QRPackedQ{S}}) where {T,S} =

test/abstractq.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@ n = 5
3030
F = qr(A)
3131
Q = MyQ(F.Q)
3232
@test ndims(Q) == 2
33+
@test eltype([A, Q]) == Union{AbstractQ{T}, AbstractMatrix{T}} # promotion
3334
T <: Real && @test transpose(Q) == adjoint(Q)
3435
T <: Complex && @test_throws ErrorException transpose(Q)
3536
@test convert(AbstractQ{complex(T)}, Q) isa MyQ{complex(T)}
3637
@test convert(AbstractQ{complex(T)}, Q') isa AdjointQ{<:complex(T),<:MyQ{complex(T)}}
38+
@test Q == Matrix(Q) == Q
39+
@test Q Matrix(Q) Q
3740
@test *(Q) == Q
3841
@test Q*I Q.Q*I rtol=2eps(real(T))
3942
@test Q'*I Q.Q'*I rtol=2eps(real(T))
@@ -47,6 +50,7 @@ n = 5
4750
@test (Q')^2 Q'*Q'
4851
@test abs(det(Q)) 1
4952
@test logabsdet(Q)[1] 0 atol=2n*eps(real(T))
53+
@test logdet(Q) log(sign(det(Q)))
5054
y = rand(T, n)
5155
@test Q * y Q.Q * y Q' \ y ldiv!(Q', copy(y)) ldiv!(zero(y), Q', y)
5256
@test Q'y Q.Q' * y Q \ y ldiv!(Q, copy(y)) ldiv!(zero(y), Q, y)

test/hessenberg.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,4 +305,12 @@ end
305305
end
306306
end
307307

308+
@testset "Hessenberg factorization of Q" begin
309+
for T in (Float32, Float64, ComplexF32, ComplexF64)
310+
Q1, H1 = hessenberg(randn(T,5,5))
311+
Q2, H2 = hessenberg(Q1)
312+
@test Q2'Q2 I
313+
end
314+
end
315+
308316
end # module TestHessenberg

test/lq.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,13 @@ end
234234

235235
end
236236

237+
@testset "LQ factorization of Q" begin
238+
for T in (Float32, Float64, ComplexF32, ComplexF64)
239+
L1, Q1 = lq(randn(T, 5, 5))
240+
L2, Q2 = lq(Q1)
241+
@test Matrix(Q1) Matrix(Q2)
242+
@test L2 I
243+
end
244+
end
245+
237246
end # module TestLQ

0 commit comments

Comments
 (0)