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

New logm implementation #21571

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
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
29 changes: 14 additions & 15 deletions base/linalg/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -554,33 +554,32 @@ julia> logm(A)
function logm(A::StridedMatrix{T}) where T
# If possible, use diagonalization
if issymmetric(A) && T <: Real
return full(logm(Symmetric(A)))
return logm(Symmetric(A))
end
if ishermitian(A)
return full(logm(Hermitian(A)))
return logm(Hermitian(A))
end

# Quicker return if A is diagonal
if isdiag(A)
retmat = copy(A)
for i in 1:n
retmat[i, i] = log(retmat[i, i])
end
return retmat
end

# Use Schur decomposition
n = checksquare(A)
if istriu(A)
retmat = full(logm(UpperTriangular(complex(A))))
d = diag(A)
retmat = logm(UpperTriangular(float.(A)))
else
S,Q,d = schur(complex(A))
R = logm(UpperTriangular(S))
R = logm(UpperTriangular(float.(S)))
retmat = Q * R * Q'
end

# Check whether the matrix has nonpositive real eigs
np_real_eigs = false
for i = 1:n
if imag(d[i]) < eps() && real(d[i]) <= 0
np_real_eigs = true
break
end
end

if isreal(A) && !np_real_eigs
if isreal(A)
return real(retmat)
else
return retmat
Expand Down
4 changes: 2 additions & 2 deletions base/linalg/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ for (funm, func) in ([:logm,:log], [:sqrtm,:sqrt])
@eval begin
function ($funm){T<:Real}(A::Symmetric{T})
F = eigfact(A)
if isposdef(F)
if all(λ -> λ ≥ 0, F.values)
Copy link
Member

@fredrikekre fredrikekre Apr 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change? isposdef(F) is more clear IMO and does the same thing? Same just below too.

Edit: Nevermind, isposdef checks λ > 0

retmat = (F.vectors * Diagonal(($func).(F.values))) * F.vectors'
else
retmat = (F.vectors * Diagonal(($func).(complex.(F.values)))) * F.vectors'
Expand All @@ -484,7 +484,7 @@ for (funm, func) in ([:logm,:log], [:sqrtm,:sqrt])
function ($funm){T}(A::Hermitian{T})
n = checksquare(A)
F = eigfact(A)
if isposdef(F)
if all(λ -> λ ≥ 0, F.values)
retmat = (F.vectors * Diagonal(($func).(F.values))) * F.vectors'
if T <: Real
return Hermitian(retmat)
Expand Down
Loading