-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschur_parlett.jl
211 lines (190 loc) · 5.86 KB
/
schur_parlett.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
################################################################################
# This file is a part of the Julia package: MatrixFunctions.jl
# Released under the MIT license, see LICENSE file for details.
# Copyright (C) 2023 Aravindh Krishnamoorthy and contributors.
#
# Functions in this file:
# 1. fm_schur_parlett_recurrence(f::Function, X::AbstractMatrix)
# 2. fm_schur_parlett_block(f::Function, X::AbstractMatrix)
#
# In all functions:
# - Intermediate results are computed in the input element type unless
# conversion to complex type is needed.
################################################################################
using LinearAlgebra
using TaylorSeries
################################################################################
# Schur-Parlett algorithm based on Algorithm 4.13 In
# Matrix Functions, Higham, 2008, SIAM, ISBN 978-0-89871-646-7.
#
# NOTE: This function implements a basic algorithm that is used internally as
# a reference and for testing; this function must not be used in
# production code.
################################################################################
function fm_schur_parlett_recurrence(f::Function, X::AbstractMatrix)
m,n = size(X)
@assert m == n
@assert m > 0
# Schur decomposition
S = schur(X)
if !istriu(S.T)
# T is only triangular in the complex field
S = Schur{Complex}(S)
end
# Iterate to obtain the matrices
T, Z, λ = S
# For diagonalisable matrices, use the straightforward formula
if isdiag(T)
# Move to complex field in case the current domain is insufficient
# to compute f.(λ)
λ′ = λ
try
λ′ = f.(λ)
catch e
if isa(e, DomainError)
λ = complex.(λ)
λ′ = f.(λ)
else
throw(e)
end
end
D = Diagonal(λ′)
F = Z*D*Z'
return F
end
# Move to complex field in case the current domain is insufficient
# to compute f.(λ)
λ′ = λ
try
λ′ = f.(λ)
catch e
if isa(e, DomainError)
λ = complex.(λ)
λ′ = f.(λ)
else
throw(e)
end
end
F = zeros(eltype(λ′), n, n)
F[diagind(F)] = λ′
# Compute the upper triangular elements via Algorithm 4.13
for i = 1:n
F[i,i] = f(λ[i])
end
for j = 2:n
for i = j-1:-1:1
if λ[i] == λ[j]
throw(RepeatedEigenvalueException())
else
δ = λ[i] - λ[j]
# TODO: Improve numerical stability
F[i,j] = T[i,j]*(λ′[i] - λ′[j])/δ + (sum(F[i,i+1:j-1].*T[i+1:j-1,j]) - sum(T[i,i+1:j-1].*F[i+1:j-1,j]))/δ
end
end
end
return Z*F*Z'
end
################################################################################
#
# Schur-Parlett block algorithm based on Algorithm 9.6 in
# Matrix Functions, Higham, 2008, SIAM, ISBN 978-0-89871-646-7.
#
# NOTE: This algorithm does not perform reordering and blocking in Steps 3--5
# (Algorithm 3.6 + confluent permutation search + reordering). Instead
# this algorithm assumes 'schur' to return an appropriately blocked T.
#
################################################################################
function fm_schur_parlett_block(f::Function, X::AbstractMatrix; MAXITER::Int = 100)
m,n = size(X)
@assert m == n
@assert m > 0
# Schur decomposition
S = schur(X)
T, Z, λ = S.T, S.Z, S.values
# For diagonalisable matrices, use the straightforward formula
if isdiag(T)
# Move to complex field in case the current domain is insufficient
# to compute f.(λ)
λ′ = λ
try
λ′ = f.(λ)
catch e
if isa(e, DomainError)
λ = complex.(λ)
λ′ = f.(λ)
else
throw(e)
end
end
D = Diagonal(λ′)
F = Z*D*Z'
return F
end
if ~istriu(T)
# If T is quasi upper triangular, convert to complex-valued
# upper triangular as required by Algorithm 9.6
S = Schur{Complex}(S)
T, Z, λ = S.T, S.Z, S.values
end
# Algorithm 9.5
# Assign diagonal elements to groups with blocking parameter δ = 0.1
δ = 0.1
q = zeros(Int,n)
ql = zeros(Int,n)
D = [abs(λ[i] - λ[j]) for i = 1:n, j = 1:n]
for i = 1:n
if q[i] == 0
q[i] = maximum(q) + 1
ql[q[i]] += 1
end
for j = i:n
if q[j] == 0
if D[i,j] < δ
q[j] = q[i]
ql[q[j]] += 1
end
end
end
end
#TODO: Reordering. Here, we assume that schur decomposition already
#TODO: reorders as per requirement.
display(ql)
ix = 1
S = fill!(similar(T), 0)
for i=1:n
# Break if done
if ql[i] == 0 break end
# Current m×m diagonal matrix
ixe = ix+ql[i]-1
Tii = T[ix:ixe,ix:ixe]
# Algorithm 9.4 for diagonal blocks
E = Matrix{eltype(Tii)}(I, ql[i], ql[i])
σ = sum(diag(Tii))/ql[i]
Mii = Tii - σ*E
tol = sqrt(eps())
F0 = f(σ)*E
F1 = F0
Pii = Mii
ϵ = σ + Taylor1(MAXITER)
ft = f(ϵ)
for s=1:MAXITER
F1 = F0 + ft[s]*Pii
Pii = Pii*Mii/(s+1)
#TODO: Improve exit condition based on Algorithm 9.4
if norm(F1 - F0) <= tol*norm(F1)
break
end
F0 = F1
end
S[ix:ixe,ix:ixe] = F1
# Off-diagonal blocks
jx = j-ql[i]
for j = i-1:-1:1
jxe = jx-ql[j]+1
end
# Next block
ix = ixe + 1
end
F = Z*S*Z'
return F
end