Skip to content

Commit

Permalink
Merge pull request #15204 from JuliaLang/ksh/fixlq
Browse files Browse the repository at this point in the history
Exports, bug fixes, and tests for LQ
  • Loading branch information
andreasnoack committed Feb 24, 2016
2 parents 4fd6489 + 3959e37 commit 86f42bc
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ export
qr,
qrfact!,
qrfact,
lq,
lqfact!,
lqfact,
rank,
Expand Down
1 change: 1 addition & 0 deletions base/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export
qr,
qrfact!,
qrfact,
lq,
lqfact!,
lqfact,
rank,
Expand Down
8 changes: 4 additions & 4 deletions base/linalg/lq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function getindex(A::LQPackedQ, i::Integer, j::Integer)
return dot(x, A*y)
end

getq(A::LQ) = LQPackedQ(A.factors, A.tau)
getq(A::LQ) = LQPackedQ(A.factors, A.τ)

convert{T}(::Type{LQPackedQ{T}}, Q::LQPackedQ) = LQPackedQ(convert(AbstractMatrix{T}, Q.factors), convert(Vector{T}, Q.τ))
convert{T}(::Type{AbstractMatrix{T}}, Q::LQPackedQ) = convert(LQPackedQ{T}, Q)
Expand Down Expand Up @@ -88,19 +88,19 @@ end

## Multiplication by LQ
A_mul_B!{T<:BlasFloat}(A::LQ{T}, B::StridedVecOrMat{T}) = A[:L]*LAPACK.ormlq!('L','N',A.factors,A.τ,B)
A_mul_B!{T<:BlasFloat}(A::LQ{T}, B::QR{T}) = L*LAPACK.ormlq!('L','N',A.factors,A.τ,full(B))
A_mul_B!{T<:BlasFloat}(A::LQ{T}, B::QR{T}) = A[:L]*LAPACK.ormlq!('L','N',A.factors,A.τ,full(B))
A_mul_B!{T<:BlasFloat}(A::QR{T}, B::LQ{T}) = A_mul_B!(zeros(full(A)), full(A), full(B))
function *{TA,TB}(A::LQ{TA},B::StridedVecOrMat{TB})
TAB = promote_type(TA, TB)
A_mul_B!(convert(Factorization{TAB},A), copy_oftype(B, TAB))
end
function *{TA,TB}(A::LQ{TA},B::QR{TB})
TAB = promote_type(TA, TB)
A_mul_B!(convert(Factorization{TAB},A), TB==TAB ? copy(B) : convert(Factorization{TAB},B))
A_mul_B!(convert(Factorization{TAB},A), convert(Factorization{TAB},B))
end
function *{TA,TB}(A::QR{TA},B::LQ{TB})
TAB = promote_type(TA, TB)
A_mul_B!(convert(Factorization{TAB},A), TB==TAB ? copy(B) : convert(Factorization{TAB},B))
A_mul_B!(convert(Factorization{TAB},A), convert(Factorization{TAB},B))
end

## Multiplication by Q
Expand Down
13 changes: 12 additions & 1 deletion test/linalg/lq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ for eltya in (Float32, Float64, Complex64, Complex128)
α = rand(eltya)
= fill(α,1,1)
@test lqfact(α)[:L]*lqfact(α)[:Q] lqfact(aα)[:L]*lqfact(aα)[:Q]
@test lq(α)[1]*lq(α)[2] lqfact(aα)[:L]*lqfact(aα)[:Q]
@test abs(lqfact(α)[:Q][1,1]) one(eltya)
tab = promote_type(eltya,eltyb)

Expand All @@ -45,19 +46,29 @@ debug && println("LQ decomposition")
let a = i == 1 ? a : sub(a, 1:n - 1, 1:n - 1), b = i == 1 ? b : sub(b, 1:n - 1), n = i == 1 ? n : n - 1
lqa = lqfact(a)
l,q = lqa[:L], lqa[:Q]
qra = qrfact(a)
@test size(lqa,1) == size(a,1)
@test size(lqa,3) == 1
@test Base.LinAlg.getq(lqa) == lqa[:Q]
@test_throws KeyError lqa[:Z]
@test full(lqa') a'
@test lqa * lqa' a * a'
@test lqa' * lqa a' * a
@test q*full(q, thin = false)' eye(eltya,n)
@test l*q a
@test full(lqa) a
@test full(copy(lqa)) a
@test_approx_eq_eps a*(lqa\b) b 3000ε
qra = qrfact(a)
@test_approx_eq_eps lqa*b qra[:Q]*qra[:R]*b 3000ε
@test_approx_eq_eps A_mul_Bc(eye(eltyb,size(q.factors,2)),q)*full(q, thin=false) eye(n) 5000ε
if eltya != Int
@test eye(eltyb,n)*q convert(AbstractMatrix{tab},q)
end
@test_approx_eq_eps q*b full(q, thin=false)*b 100ε
@test_approx_eq_eps q'*b full(q, thin=false)'*b 100ε
@test_throws DimensionMismatch q*b[1:n1 + 1]
@test_throws DimensionMismatch Ac_mul_B(q,ones(eltya,n+2,n+2))
@test_throws DimensionMismatch ones(eltyb,n+2,n+2)*q
end
end

Expand Down

0 comments on commit 86f42bc

Please sign in to comment.