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

Make unitary matrices in svd/eigen of Diagonal be unitless #1155

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
15 changes: 9 additions & 6 deletions src/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -931,15 +931,18 @@ function pinv(D::Diagonal{T}, tol::Real) where T
Diagonal(Di)
end

_ortho_eltype(T) = Base.promote_op(/, T, T)
_ortho_eltype(T::Type{<:Number}) = typeof(one(T))

# TODO Docstrings for eigvals, eigvecs, eigen all mention permute, scale, sortby as keyword args
# but not all of them below provide them. Do we need to fix that?
#Eigensystem
eigvals(D::Diagonal{<:Number}; permute::Bool=true, scale::Bool=true) = copy(D.diag)
eigvals(D::Diagonal; permute::Bool=true, scale::Bool=true) =
reduce(vcat, eigvals(x) for x in D.diag) #For block matrices, etc.
function eigvecs(D::Diagonal{T}) where T<:AbstractMatrix
function eigvecs(D::Diagonal{T}) where {T<:AbstractMatrix}
diag_vecs = [ eigvecs(x) for x in D.diag ]
matT = reduce((a,b) -> promote_type(typeof(a),typeof(b)), diag_vecs)
matT = promote_type(map(typeof, diag_vecs)...)
jishnub marked this conversation as resolved.
Show resolved Hide resolved
ncols_diag = [ size(x, 2) for x in D.diag ]
nrows = size(D, 1)
vecs = Matrix{Vector{eltype(matT)}}(undef, nrows, sum(ncols_diag))
Expand All @@ -961,7 +964,7 @@ function eigen(D::Diagonal; permute::Bool=true, scale::Bool=true, sortby::Union{
if any(!isfinite, D.diag)
throw(ArgumentError("matrix contains Infs or NaNs"))
end
Td = Base.promote_op(/, eltype(D), eltype(D))
Td = _ortho_eltype(eltype(D))
λ = eigvals(D)
if !isnothing(sortby)
p = sortperm(λ; alg=QuickSort, by=sortby)
Expand Down Expand Up @@ -1019,13 +1022,13 @@ function svd(D::Diagonal{T}) where {T<:Number}
s = abs.(d)
piv = sortperm(s, rev = true)
S = s[piv]
Td = typeof(oneunit(T)/oneunit(T))
Td = _ortho_eltype(T)
U = zeros(Td, size(D))
Vt = copy(U)
for i in 1:length(d)
j = piv[i]
U[j,i] = iszero(d[j]) ? oneunit(Td) : d[j] / S[i]
Vt[i,j] = oneunit(Td)
U[j,i] = iszero(d[j]) ? one(Td) : d[j] / S[i]
Vt[i,j] = one(Td)
end
return SVD(U, S, Vt)
end
Expand Down
10 changes: 7 additions & 3 deletions test/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -478,14 +478,14 @@ Random.seed!(1)
U, s, V = F
@test map(x -> x.val, Matrix(F)) ≈ map(x -> x.val, Du)
@test svdvals(Du) == s
@test U isa AbstractMatrix{<:Furlong{0}}
@test V isa AbstractMatrix{<:Furlong{0}}
@test U isa AbstractMatrix{<:AbstractFloat}
dkarrasch marked this conversation as resolved.
Show resolved Hide resolved
@test V isa AbstractMatrix{<:AbstractFloat}
@test s isa AbstractVector{<:Furlong{1}}
E = eigen(Du)
vals, vecs = E
@test Matrix(E) == Du
@test vals isa AbstractVector{<:Furlong{1}}
@test vecs isa AbstractMatrix{<:Furlong{0}}
@test vecs isa AbstractMatrix{<:AbstractFloat}
end
end

Expand Down Expand Up @@ -858,6 +858,10 @@ end
@test eigD.values == evals
@test eigD.vectors ≈ evecs
@test D * eigD.vectors ≈ eigD.vectors * Diagonal(eigD.values)

# test concrete types
D = Diagonal([I2 for _ in 1:4])
@test eigen(D) isa Eigen{Vector{Float64}, Float64, Matrix{Vector{Float64}}, Vector{Float64}}
end

@testset "linear solve for block diagonal matrices" begin
Expand Down
Loading