Skip to content

Commit cb7c7fb

Browse files
committed
Merge pull request #12236 from mfasi/logm_test_redirect
Fix warnings due to logm in tests/linalg3.jl
2 parents 4264b50 + 6fdad80 commit cb7c7fb

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

base/linalg/dense.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,14 @@ function logm(A::StridedMatrix)
286286

287287
# Use Schur decomposition
288288
n = chksquare(A)
289-
S,Q,d = schur(complex(A))
290-
R = logm(UpperTriangular(S))
291-
retmat = Q * R * Q'
289+
if istriu(A)
290+
retmat = full(logm(UpperTriangular(complex(A))))
291+
d = diag(A)
292+
else
293+
S,Q,d = schur(complex(A))
294+
R = logm(UpperTriangular(S))
295+
retmat = Q * R * Q'
296+
end
292297

293298
# Check whether the matrix has nonpositive real eigs
294299
np_real_eigs = false

base/linalg/diagonal.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ end
119119
eye{T}(::Type{Diagonal{T}}, n::Int) = Diagonal(ones(T,n))
120120

121121
expm(D::Diagonal) = Diagonal(exp(D.diag))
122+
logm(D::Diagonal) = Diagonal(log(D.diag))
122123
sqrtm(D::Diagonal) = Diagonal(sqrt(D.diag))
123124

124125
#Linear solver

base/linalg/triangular.jl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -891,11 +891,14 @@ end
891891

892892
# Complex matrix logarithm for the upper triangular factor, see:
893893
# Al-Mohy and Higham, "Improved inverse scaling and squaring algorithms for
894-
# the matrix logarithm", SIAM J. Sci. Comput., 34(4), (2012), pp. C153-C169.
894+
# the matrix logarithm", SIAM J. Sci. Comput., 34(4), (2012), pp. C153-C169.
895895
# Al-Mohy, Higham and Relton, "Computing the Frechet derivative of the matrix
896-
# logarithm and estimating the condition number", SIAM J. Sci. Comput., 35(4),
897-
# (2013), C394-C410.
898-
# http://eprints.ma.man.ac.uk/1687/02/logm.zip
896+
# logarithm and estimating the condition number", SIAM J. Sci. Comput., 35(4),
897+
# (2013), C394-C410.
898+
#
899+
# Based on the code available at http://eprints.ma.man.ac.uk/1851/02/logm.zip,
900+
# Copyright (c) 2011, Awad H. Al-Mohy and Nicholas J. Higham
901+
# Julia version relicensed with permission from original authors
899902
function logm{T<:Union{Float64,Complex{Float64}}}(A0::UpperTriangular{T})
900903
maxsqrt = 100
901904
theta = [1.586970738772063e-005,
@@ -907,7 +910,7 @@ function logm{T<:Union{Float64,Complex{Float64}}}(A0::UpperTriangular{T})
907910
2.879093714241194e-001]
908911
tmax = size(theta, 1)
909912
n = size(A0, 1)
910-
A = A0
913+
A = copy(A0)
911914
p = 0
912915
m = 0
913916

@@ -916,7 +919,7 @@ function logm{T<:Union{Float64,Complex{Float64}}}(A0::UpperTriangular{T})
916919
dm1 = Array(T, n)
917920
s = 0
918921
for i = 1:n
919-
dm1[i] = dm1[i] - 1.
922+
dm1[i] = d[i] - 1.
920923
end
921924
while norm(dm1, Inf) > theta[tmax]
922925
for i = 1:n
@@ -1075,6 +1078,7 @@ function logm{T<:Union{Float64,Complex{Float64}}}(A0::UpperTriangular{T})
10751078
return UpperTriangular(Y)
10761079

10771080
end
1081+
logm(A::LowerTriangular) = logm(A.').'
10781082

10791083
function sqrtm{T}(A::UpperTriangular{T})
10801084
n = size(A, 1)

test/linalg3.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,20 +162,29 @@ for elty in (Float32, Float64, Complex64, Complex128)
162162
end
163163

164164
for elty in (Float64, Complex{Float64})
165+
165166
A4 = convert(Matrix{elty}, [1/2 1/3 1/4 1/5+eps();
166167
1/3 1/4 1/5 1/6;
167168
1/4 1/5 1/6 1/7;
168169
1/5 1/6 1/7 1/8])
169170
@test_approx_eq expm(logm(A4)) A4
170171

172+
OLD_STDERR = STDERR
173+
rd,wr = redirect_stderr()
171174
A5 = convert(Matrix{elty}, [1 1 0 1; 0 1 1 0; 0 0 1 1; 1 0 0 1])
172175
@test_approx_eq expm(logm(A5)) A5
176+
s = readline(rd)
177+
@test contains(s, "WARNING: Matrix with nonpositive real eigenvalues, a nonprimary matrix logarithm will be returned.")
173178

174179
A6 = convert(Matrix{elty}, [-5 2 0 0 ; 1/2 -7 3 0; 0 1/3 -9 4; 0 0 1/4 -11])
175180
@test_approx_eq expm(logm(A6)) A6
181+
s = readline(rd)
182+
@test contains(s, "WARNING: Matrix with nonpositive real eigenvalues, a nonprimary matrix logarithm will be returned.")
183+
redirect_stderr(OLD_STDERR)
176184

177185
A7 = convert(Matrix{elty}, [1 0 0 1e-8; 0 1 0 0; 0 0 1 0; 0 0 0 1])
178186
@test_approx_eq expm(logm(A7)) A7
187+
179188
end
180189

181190
A8 = 100 * [-1+1im 0 0 1e-8; 0 1 0 0; 0 0 1 0; 0 0 0 1];

0 commit comments

Comments
 (0)