Skip to content

Commit bb51021

Browse files
authored
Merge pull request #345 from denius/fix-qr-type-return
Fix qr() type promotion
2 parents 68eca4c + 6ec428a commit bb51021

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/qr.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ end
1919
@inline qr(A::StaticMatrix, pivot::Union{Type{Val{false}}, Type{Val{true}}}, thin::Union{Type{Val{false}}, Type{Val{true}}}) = _qr(Size(A), A, pivot, thin)
2020

2121

22+
_qreltype(::Type{T}) where T = typeof(zero(T)/sqrt(abs2(one(T))))
23+
24+
2225
@generated function _qr(::Size{sA}, A::StaticMatrix{<:Any, <:Any, TA}, pivot::Union{Type{Val{false}}, Type{Val{true}}} = Val{false}, thin::Union{Type{Val{false}}, Type{Val{true}}} = Val{true}) where {sA, TA}
2326

2427
isthin = thin == Type{Val{true}}
@@ -30,7 +33,7 @@ end
3033
return quote
3134
@_inline_meta
3235
Q0, R0, p0 = Base.qr(Matrix(A), pivot, thin=$isthin)
33-
T = arithmetic_closure(TA)
36+
T = _qreltype(TA)
3437
return similar_type(A, T, $(SizeQ))(Q0),
3538
similar_type(A, T, $(SizeR))(R0),
3639
similar_type(A, Int, $(Size(sA[2])))(p0)
@@ -45,7 +48,7 @@ end
4548
return quote
4649
@_inline_meta
4750
Q0, R0 = Base.qr(Matrix(A), pivot, thin=$isthin)
48-
T = arithmetic_closure(TA)
51+
T = _qreltype(TA)
4952
return similar_type(A, T, $(SizeQ))(Q0),
5053
similar_type(A, T, $(SizeR))(R0)
5154
end
@@ -130,7 +133,7 @@ end
130133

131134
return quote
132135
@_inline_meta
133-
T = arithmetic_closure(TA)
136+
T = _qreltype(TA)
134137
@inbounds $(Expr(:block, initQ...))
135138
@inbounds $(Expr(:block, initR...))
136139
@inbounds $code
@@ -146,7 +149,7 @@ end
146149
## thin=true version of QR
147150
#function qr_unrolled(A::StaticMatrix{<:Any, <:Any, TA}) where {TA}
148151
# m, n = size(A)
149-
# T = arithmetic_closure(TA)
152+
# T = _qreltype(TA)
150153
# Q = eye(MMatrix{m,m,T,m*m})
151154
# R = MMatrix{m,n,T,m*n}(A)
152155
# for k = 1:min(m - 1 + !(TA<:Real), n)

test/qr.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ srand(42)
1111
@testset "QR decomposition" begin
1212
function test_qr(arr)
1313

14+
T = eltype(arr)
15+
1416
# thin=true case
1517
QR = @inferred qr(arr)
1618
@test QR isa Tuple
1719
@test length(QR) == 2
1820
Q, R = QR
1921
@test Q isa StaticMatrix
2022
@test R isa StaticMatrix
23+
@test eltype(Q) == eltype(R) == typeof((one(T)*zero(T) + zero(T))/norm([one(T)]))
2124

2225
Q_ref,R_ref = qr(Matrix(arr))
2326
@test abs.(Q) abs.(Q_ref) # QR is unique up to diag(Q) signs
@@ -33,6 +36,7 @@ srand(42)
3336
Q, R = QR
3437
@test Q isa StaticMatrix
3538
@test R isa StaticMatrix
39+
@test eltype(Q) == eltype(R) == typeof((one(T)*zero(T) + zero(T))/norm([one(T)]))
3640

3741
Q_ref,R_ref = qr(Matrix(arr), thin=false)
3842
@test abs.(Q) abs.(Q_ref) # QR is unique up to diag(Q) signs
@@ -72,6 +76,7 @@ srand(42)
7276
(@MMatrix randn(2,3)),
7377
(@SMatrix([0 1 2; 0 2 3; 0 3 4; 0 4 5])),
7478
(@SMatrix zeros(Int,4,4)),
79+
(@SMatrix([1//2 1//1])),
7580
(@SMatrix randn(17,18)), # fallback to LAPACK
7681
(@SMatrix randn(18,17))
7782
]

0 commit comments

Comments
 (0)