Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

complex hessenberg #32

Merged
merged 23 commits into from
Aug 11, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixes tridiag.jl
smataigne committed Aug 9, 2022
commit 806c79a995b2be55654e96465e668d525916d2ec
20 changes: 10 additions & 10 deletions src/tridiag.jl
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
struct SkewHermTridiagonal{T, V<:AbstractVector{T}} <: AbstractMatrix{T}
ev::V # subdiagonal
function SkewHermTridiagonal{T, V}(ev) where {T, V<:AbstractVector{T}}
require_one_based_indexing(ev)
LA.require_one_based_indexing(ev)
new{T, V}(ev)
end
end
@@ -100,7 +100,7 @@ function Base.size(A::SkewHermTridiagonal, d::Integer)
end
end

Base.similar(S::SkewHermTridiagonal, ::Type{T}) where {T} = SkewHermTridiagonal(similar(S.dv, T), similar(S.ev, T))
Base.similar(S::SkewHermTridiagonal, ::Type{T}) where {T} = SkewHermTridiagonal(similar(S.ev, T))
Base.similar(S::SkewHermTridiagonal, ::Type{T}, dims::Union{Dims{1},Dims{2}}) where {T} = zeros(T, dims...)

Base.copyto!(dest::SkewHermTridiagonal, src::SkewHermTridiagonal) =
@@ -205,7 +205,7 @@ Base.:\(B::Number, A::SkewHermTridiagonal) = SkewHermTridiagonal(B\A.ev)
end

function LA.dot(x::AbstractVector, S::SkewHermTridiagonal, y::AbstractVector)
require_one_based_indexing(x, y)
LA.require_one_based_indexing(x, y)
nx, ny = length(x), length(y)
(nx == size(S, 1) == ny) || throw(DimensionMismatch())
if iszero(nx)
@@ -274,25 +274,25 @@ end
end

@views function skewtrieigen!(S::SkewHermTridiagonal)
n = size(S.data,1)
n = size(S,1)
H = SymTridiagonal(zeros(eltype(S.ev),n),S.ev)
trisol = eigen!(H)

vals = trisol.values*1im
vals .*= -1
Qdiag = zeros(eltype(trisol.vectors),n,n)*1im
Qdiag = similar(trisol.vectors,n,n)*1im
c = 1
@inbounds for j=1:n
c = 1
@simd for i=1:2:n-1
k=(i+1)÷2
Qdiag[i,j].=trisol.vectors[i,j]*c
Qdiag[i+1,j].=trisol.vectors[i+1,j]*c*1im
Qdiag[i,j] = trisol.vectors[i,j]*c
Qdiag[i+1,j] = trisol.vectors[i+1,j]*c*1im
c *= (-1)
end
c *= (-1)

end
if n%2==1
Qdiag[n,:].=trisol.vectors[i,j]*c
Qdiag[n,:]=trisol.vectors[n,:]*c
end
return Eigen(vals,Qdiag)
end
26 changes: 24 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -122,7 +122,7 @@ end
@test Matrix(HA.H) ≈ Matrix(HB.H)
@test Matrix(HA.Q) ≈ Matrix(HB.Q)
end
"""

A=zeros(4,4)
A[2:4,1]=ones(3)
A[1,2:4]=-ones(3)
@@ -131,7 +131,7 @@ end
HA=hessenberg(A)
HB=hessenberg(B)
@test Matrix(HA.H)≈Matrix(HB.H)
"""

end
@testset "eigen.jl" begin
for n in [2,20,153,200]
@@ -174,3 +174,25 @@ end
end
end

@testset "tridiag.jl" begin
for n in [2,20,151,200]
A=SLA.SkewHermTridiagonal(randn(n-1))
B=Matrix(A)
@test size(A,1)==n
EA=eigen(A)
EB=eigen(B)
Q = EA.vectors
@test real(Q*diagm(EA.values)*adjoint(Q)) ≈ B
#display(real(Q*diagm(EA.values)*adjoint(Q)))
#display(B)
valA = imag(EA.values)
valB = imag(EB.values)
sort!(valA)
sort!(valB)
@test valA ≈ valB
Svd = svd(A)
@test real(Svd.U*Diagonal(Svd.S)*Svd.Vt) ≈ B
@test svdvals(A)≈svdvals(B)

end
end