-
Notifications
You must be signed in to change notification settings - Fork 8
/
example.jl
177 lines (141 loc) · 4.33 KB
/
example.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
module Tst
using Plots
using JacobiDavidson
using LinearMaps
using LinearAlgebra
using SparseArrays
import LinearAlgebra: ldiv!
function myA!(y::AbstractVector{T}, x::AbstractVector{T}) where {T<:Number}
for i = 1 : length(x)
@inbounds y[i] = sqrt(i) * x[i]
end
end
function myB!(y::AbstractVector{T}, x::AbstractVector{T}) where {T<:Number}
for i = 1 : length(x)
@inbounds y[i] = x[i] / sqrt(i)
end
end
struct SuperPreconditioner{numT <: Number}
target::numT
end
function ldiv!(p::SuperPreconditioner{T}, x::AbstractVector{T}) where {T<:Number}
for i = 1 : length(x)
@inbounds x[i] = x[i] * sqrt(i) / (i - p.target)
end
return x
end
function ldiv!(y::AbstractVector{T}, p::SuperPreconditioner{T}, x::AbstractVector{T}) where {T<:Number}
for i = 1 : length(x)
@inbounds y[i] = x[i] * sqrt(i) / (i - p.target)
end
return y
end
function another_example(; n = 1000, target = Near(31.0 + 0.1im))
A = LinearMap{Float64}(myA!, n; ismutating = true)
B = LinearMap{Float64}(myB!, n; ismutating = true)
P = SuperPreconditioner(target.τ)
schur2, residuals2 = jdqz(
A, B,
solver = GMRES(n, iterations = 3),
preconditioner = P,
testspace = Harmonic,
target = target,
pairs = 5,
tolerance = 1e-9,
subspace_dimensions = 10:20,
max_iter = 100,
verbosity = 1
)
schur, residuals = jdqz(
A, B,
solver = BiCGStabl(n, max_mv_products = 10, l = 2),
preconditioner = P,
testspace = Harmonic,
target = target,
pairs = 5,
tolerance = 1e-9,
subspace_dimensions = 10:20,
max_iter = 100,
verbosity = 1
)
plot(residuals, yscale = :log10, label = "BiCGStab", marker = :x)
plot!(residuals2, yscale = :log10, label = "GMRES", marker = :x)
end
function generalized(; n = 1_000, target = Near(0.5 + 0.1im))
A = 2I + sprand(ComplexF64, n, n, 1 / n)
B = 2I + sprand(ComplexF64, n, n, 1 / n)
values = eigvals(Matrix(A), Matrix(B))
@time schur, residuals = jdqz(
A, B,
solver = GMRES(n, iterations = 10),
target = target,
pairs = 10,
subspace_dimensions = 10:15,
max_iter = 300,
verbosity = 1
)
found = schur.alphas ./ schur.betas
p1 = scatter(real(values), imag(values), label = "eig")
scatter!(real(found), imag(found), marker = :+, label = "jdqz")
scatter!([real(target.τ)], [imag(target.τ)], marker = :star, label = "Target")
p2 = plot(residuals, yscale = :log10)
p1, p2
end
function test_harmonic_2(n = 1000; τ = Near(1.52 + 0.0im))
A = spdiagm(
-1 => fill(-1.0, n - 1),
0 => fill(2.0, n),
1 => fill(-1.0, n - 1)
)
B = spdiagm(0 => fill(1.0, n))
values = eigvals(Matrix(A), Matrix(B))
schur, residuals = jdqz(
A,
B,
solver = Exact(),
pairs = 10,
subspace_dimensions = 10:20,
max_iter = 300,
tolerance = 1e-5,
target = τ
)
found = schur.alphas ./ schur.betas
p1 = scatter(real(values), imag(values), ylims = (-1, 1))
scatter!(real(found), imag(found), marker = :+)
p2 = plot(residuals, yscale = :log10)
p1, p2
end
function test_harmonic(; n = 500, τ = Near(0.01 + 0.02im))
A = spdiagm(
-1 => fill(-1.0, n - 1),
0 => fill(2.0, n),
1 => fill(-1.0, n - 1)
)
schur, ritz_hist, conv_hist, residuals = jdqr(
A,
solver = BiCGStabl(size(A, 1), max_mv_products = 30, l = 2),
pairs = 10,
subspace_dimensions = 5:10,
max_iter = 300,
tolerance = 1e-5,
target = τ,
verbosity = 1
)
λs = real(eigvals(Matrix(A)))
# Total number of iterations
iterations = length(ritz_hist)
@show iterations
@show schur.values
# Plot the target as a horizontal line
p = plot([0.0, iterations + 1.0], [real(τ.τ), real(τ.τ)], linewidth=5, legend = :none, layout = (2, 1))
# Plot the actual eigenvalues
scatter!(fill(iterations + 1, n), λs, xlabel = "Iteration", ylims = (minimum(λs) - 2.0, maximum(λs) + 2.0), marker = (:diamond, :black), subplot = 1)
# Plot the approximate eigenvalues per iteration
for (k, (ritzvalues, eigenvalues)) = enumerate(zip(ritz_hist, conv_hist))
scatter!(k * ones(length(ritzvalues)), real(ritzvalues), marker = (:+, :green), subplot = 1)
scatter!(k * ones(length(eigenvalues)), real(eigenvalues), marker = (:hexagon, :yellow), subplot = 1)
end
plot!(residuals, xlabel = "Iteration", label = "Residual", xticks = 0.0 : 50.0 : 350.0, yaxis = :log10, subplot = 2)
p
end
end