Skip to content

Commit 2beca10

Browse files
committed
Inline documentation and tests for real power of a matrix
1 parent 9a72847 commit 2beca10

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

base/linalg/triangular.jl

+12
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,12 @@ function ^(A::UpperTriangular, p::Real)
997997
return powm(A,p - ceil(p)) * A^ceil(p)
998998
end
999999
end
1000+
# Complex matrix power for upper triangular factor, see:
1001+
# Higham and Lin, "A Schur-Padé algorithm for fractional powers of a Matrix",
1002+
# SIAM J. Matrix Anal. & Appl., 32 (3), (2011) 1056–1078.
1003+
# Higham and Lin, "An improved Schur-Padé algorithm for fractional powers of
1004+
# a matrix and their Fréchet derivatives", SIAM. J. Matrix Anal. & Appl.,
1005+
# 34(3), (2013) 1341–1360.
10001006
function powm{T<:Union{Float64,Complex{Float64}}}(A0::UpperTriangular{T}, p::Real)
10011007

10021008
if abs(p) >= 1
@@ -1153,6 +1159,8 @@ end
11531159
logm(A::LowerTriangular) = logm(A.').'
11541160

11551161
# Auxiliary functions for logm and matrix power
1162+
1163+
# Compute accurate diagonal of A = A0^s - I
11561164
function sqrt_diag!(A0::UpperTriangular, A::UpperTriangular, s)
11571165
n = chksquare(A0)
11581166
for i = 1:n
@@ -1177,6 +1185,7 @@ function sqrt_diag!(A0::UpperTriangular, A::UpperTriangular, s)
11771185
end
11781186
end
11791187

1188+
# Compute the repeated square root of A to match the conditions on theta
11801189
function invsquaring{T}(A0::UpperTriangular{T}, theta)
11811190
maxsqrt = 100
11821191
tmax = size(theta, 1)
@@ -1274,6 +1283,7 @@ function invsquaring{T}(A0::UpperTriangular{T}, theta)
12741283

12751284
end
12761285

1286+
# Compute accurate diagonal and superdiagonal of A = A0^p
12771287
function blockpower!(A0::UpperTriangular, A::UpperTriangular, p)
12781288
n = chksquare(A0)
12791289
for k = 1:n-1
@@ -1300,6 +1310,8 @@ function blockpower!(A0::UpperTriangular, A::UpperTriangular, p)
13001310
end
13011311
end
13021312
end
1313+
1314+
# Unwinding number
13031315
unw(x::Real) = 0
13041316
unw(x::Number) = ceil((imag(x) - pi) / (2 * pi))
13051317

test/linalg/dense.jl

+34
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,40 @@ eA10 = [ 1.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0i
387387
0.0+0.0im 0.0+0.0im 0.0+0.0im 1.0+0.0im]
388388
@test_approx_eq expm(A10) eA10
389389

390+
for elty in (Float64, Complex{Float64})
391+
A11 = convert(Matrix{elty}, [1 2 3; 4 7 1; 2 1 4])
392+
393+
OLD_STDERR = STDERR
394+
rd,wr = redirect_stderr()
395+
396+
@test A11^(1/2) sqrtm(A11)
397+
s = readline(rd)
398+
@test contains(s, "WARNING: Matrix with nonpositive real eigenvalues, a nonprincipal matrix power will be returned.")
399+
400+
@test A11^(-1/2) inv(sqrtm(A11))
401+
s = readline(rd)
402+
@test contains(s, "WARNING: Matrix with nonpositive real eigenvalues, a nonprincipal matrix power will be returned.")
403+
404+
@test A11^(3/4) A11 * inv(sqrtm(sqrtm(A11)))
405+
s = readline(rd)
406+
@test contains(s, "WARNING: Matrix with nonpositive real eigenvalues, a nonprincipal matrix power will be returned.")
407+
408+
@test A11^(-3/4) inv(A11) * sqrtm(sqrtm(A11))
409+
s = readline(rd)
410+
@test contains(s, "WARNING: Matrix with nonpositive real eigenvalues, a nonprincipal matrix power will be returned.")
411+
412+
@test A11^(17/8) A11^2 * sqrtm(sqrtm(sqrtm(A11)))
413+
s = readline(rd)
414+
@test contains(s, "WARNING: Matrix with nonpositive real eigenvalues, a nonprincipal matrix power will be returned.")
415+
416+
@test A11^(-17/8) inv(A11^2 * sqrtm(sqrtm(sqrtm(A11))))
417+
s = readline(rd)
418+
@test contains(s, "WARNING: Matrix with nonpositive real eigenvalues, a nonprincipal matrix power will be returned.")
419+
420+
redirect_stderr(OLD_STDERR)
421+
end
422+
423+
390424
# issue #7181
391425
A = [ 1 5 9
392426
2 6 10

0 commit comments

Comments
 (0)