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

Improve performance of svd and eigen of Diagonals #43856

Merged
merged 13 commits into from
Feb 8, 2022
22 changes: 14 additions & 8 deletions stdlib/LinearAlgebra/src/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -672,14 +672,20 @@ end
#Singular system
svdvals(D::Diagonal{<:Number}) = sort!(abs.(D.diag), rev = true)
svdvals(D::Diagonal) = [svdvals(v) for v in D.diag]
function svd(D::Diagonal{T}) where T<:Number
S = abs.(D.diag)
piv = sortperm(S, rev = true)
U = Diagonal(D.diag ./ S)
Up = hcat([U[:,i] for i = 1:length(D.diag)][piv]...)
V = Diagonal(fill!(similar(D.diag), one(T)))
Vp = hcat([V[:,i] for i = 1:length(D.diag)][piv]...)
return SVD(Up, S[piv], copy(Vp'))
function svd(D::Diagonal{T}) where {T<:Number}
d = D.diag
s = abs.(d)
piv = sortperm(s, rev = true)
S = s[piv]
Td = typeof(oneunit(T)/oneunit(T))
U = zeros(Td, size(D))
Vt = copy(U)
for i in 1:length(d)
j = piv[i]
U[j,i] = d[j] >= zero(T) ? one(Td) : -one(Td)
dkarrasch marked this conversation as resolved.
Show resolved Hide resolved
Vt[i,j] = one(Td)
end
return SVD(U, S, Vt)
end

# disambiguation methods: * and / of Diagonal and Adj/Trans AbsVec
Expand Down