Skip to content
Merged
Changes from 2 commits
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
21 changes: 18 additions & 3 deletions src/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,12 @@ function svdvals!(A::RealHermSymComplexHerm)
return sort!(vals, rev = true)
end

#computes U * Diagonal(abs2.(v)) * U', destroying U
function _psd_spectral_product!(v, U)
rmul!(U, Diagonal(v))
return U * U'
end

# Matrix functions
^(A::SymSymTri{<:Complex}, p::Integer) = sympow(A, p)
^(A::SelfAdjoint, p::Integer) = sympow(A, p)
Expand All @@ -848,7 +854,8 @@ function ^(A::SelfAdjoint, p::Real)
isinteger(p) && return integerpow(A, p)
F = eigen(A)
if all(λ -> λ ≥ 0, F.values)
retmat = (F.vectors * Diagonal((F.values).^p)) * F.vectors'
map!(λ -> λ^0.5p, F.values)
retmat = _psd_spectral_product!(F.values, F.vectors)
return wrappertype(A)(retmat)
else
retmat = (F.vectors * Diagonal(complex.(F.values).^p)) * F.vectors'
Expand All @@ -860,7 +867,7 @@ function ^(A::SymSymTri{<:Complex}, p::Real)
return Symmetric(schurpow(A, p))
end

for func in (:exp, :cos, :sin, :tan, :cosh, :sinh, :tanh, :atan, :asinh, :atanh, :cbrt)
for func in (:cos, :sin, :tan, :cosh, :sinh, :tanh, :atan, :asinh, :atanh, :cbrt)
@eval begin
function ($func)(A::SelfAdjoint)
F = eigen(A)
Expand All @@ -870,6 +877,13 @@ for func in (:exp, :cos, :sin, :tan, :cosh, :sinh, :tanh, :atan, :asinh, :atanh,
end
end

function exp(A::SelfAdjoint)
F = eigen(A)
map!(λ -> exp(0.5λ), F.values)
retmat = _psd_spectral_product!(F.values, F.vectors)
return wrappertype(A)(retmat)
end

function cis(A::SelfAdjoint)
F = eigen(A)
retmat = F.vectors .* cis.(F.values') * F.vectors'
Expand Down Expand Up @@ -929,7 +943,8 @@ function sqrt(A::SelfAdjoint; rtol = eps(real(float(eltype(A)))) * size(A, 1))
F = eigen(A)
λ₀ = -maximum(abs, F.values) * rtol # treat λ ≥ λ₀ as "zero" eigenvalues up to roundoff
if all(λ -> λ ≥ λ₀, F.values)
retmat = (F.vectors * Diagonal(sqrt.(max.(0, F.values)))) * F.vectors'
map!(λ -> λ < 0 ? zero(λ) : fourthroot(λ), F.values)
retmat = _psd_spectral_product!(F.values, F.vectors)
return wrappertype(A)(retmat)
else
retmat = (F.vectors * Diagonal(sqrt.(complex.(F.values)))) * F.vectors'
Expand Down