Skip to content

Commit dd434ca

Browse files
authored
Fix size of Householder (#101)
Also improve error messages and extend Base's convert instead of defining a new one
1 parent 10b5265 commit dd434ca

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/householder.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ struct HouseholderBlock{T,S<:StridedMatrix,U<:StridedMatrix}
1010
T::UpperTriangular{T,U}
1111
end
1212

13-
size(H::Householder) = (length(H.v), length(H.v))
14-
size(H::Householder, i::Integer) = i <= 2 ? length(H.v) : 1
13+
size(H::Householder) = (length(H.v) + 1, length(H.v) + 1)
14+
size(H::Householder, i::Integer) = i <= 2 ? length(H.v) + 1 : 1
1515

1616
eltype(H::Householder{T}) where T = T
1717
eltype(H::HouseholderBlock{T}) where T = T
@@ -21,7 +21,7 @@ adjoint(H::HouseholderBlock{T}) where {T} = Adjoint{T,typeof(H)}(H)
2121

2222
function lmul!(H::Householder, A::StridedMatrix)
2323
m, n = size(A)
24-
length(H.v) == m - 1 || throw(DimensionMismatch(""))
24+
length(H.v) == m - 1 || throw(DimensionMismatch("size of reflector is $(length(H.v) + 1) but first dimension of matrix is $(size(A, 1))"))
2525
v = view(H.v, 1:m - 1)
2626
τ = H.τ
2727
for j = 1:n
@@ -37,7 +37,7 @@ end
3737

3838
function rmul!(A::StridedMatrix, H::Householder)
3939
m, n = size(A)
40-
length(H.v) == n - 1 || throw(DimensionMismatch(""))
40+
length(H.v) == n - 1 || throw(DimensionMismatch("size of reflector is $(length(H.v) + 1) but second dimension of matrix is $(size(A, 2))"))
4141
v = view(H.v, :)
4242
τ = H.τ
4343
a1 = view(A, :, 1)
@@ -52,7 +52,7 @@ end
5252
function lmul!(adjH::Adjoint{<:Any,<:Householder}, A::StridedMatrix)
5353
H = parent(adjH)
5454
m, n = size(A)
55-
length(H.v) == m - 1 || throw(DimensionMismatch(""))
55+
length(H.v) == m - 1 || throw(DimensionMismatch("size of reflector is $(length(H.v) + 1) but first dimension of matrix is $(size(A, 1))"))
5656
v = view(H.v, 1:m - 1)
5757
τ = H.τ
5858
for j = 1:n
@@ -142,5 +142,5 @@ end
142142
(*)(adjH::Adjoint{T,<:HouseholderBlock{T}}, A::StridedMatrix{T}) where {T} =
143143
lmul!(adjH, copy(A), similar(A, (min(size(parent(adjH).V)...), size(A, 2))))
144144

145-
convert(::Type{Matrix}, H::Householder{T}) where {T} = lmul!(H, Matrix{T}(I, size(H, 1), size(H, 1)))
146-
convert(::Type{Matrix{T}}, H::Householder{T}) where {T} = lmul!(H, Matrix{T}(I, size(H, 1), size(H, 1)))
145+
Base.convert(::Type{Matrix}, H::Householder{T}) where {T} = convert(Matrix{T}, H)
146+
Base.convert(::Type{Matrix{T}}, H::Householder) where {T} = lmul!(H, Matrix{T}(I, size(H, 1), size(H, 1)))

test/eigengeneral.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,16 @@ end
144144
@test sort(imag(vals)) sort(imag(λs)) atol=1e-25
145145
end
146146

147+
@testset "_hessenberg! and Hessenberg" begin
148+
n = 10
149+
A = randn(n, n)
150+
HF = GenericLinearAlgebra._hessenberg!(copy(A))
151+
for i in 1:length(HF.τ)
152+
HM = convert(Matrix, HF.τ[i])
153+
A[(i + 1):end, :] = HM * A[(i + 1):end, :]
154+
A[:, (i + 1):end] = A[:, (i + 1):end] * HM'
155+
end
156+
@test tril(A, -2) zeros(n, n) atol = 1e-14
157+
end
158+
147159
end

0 commit comments

Comments
 (0)