|
| 1 | +using IterativeSolvers |
| 2 | +using LinearMaps |
| 3 | +using Base.Test |
| 4 | + |
| 5 | +include("laplace_matrix.jl") |
| 6 | + |
| 7 | +function max_err(R) |
| 8 | + r = zeros(real(eltype(R)), size(R, 2)) |
| 9 | + for j in 1:length(r) |
| 10 | + for i in 1:size(R, 1) |
| 11 | + r[j] += conj(R[i,j])*R[i,j] |
| 12 | + end |
| 13 | + r[j] = sqrt(r[j]) |
| 14 | + end |
| 15 | + maximum(r) |
| 16 | +end |
| 17 | + |
| 18 | +@testset "Locally Optimal Block Preconditioned Conjugate Gradient" begin |
| 19 | + srand(1234321) |
| 20 | + @testset "Single eigenvalue" begin |
| 21 | + @testset "Small full system" begin |
| 22 | + n = 50 |
| 23 | + @testset "Simple eigenvalue problem" begin |
| 24 | + @testset "Matrix{$T}" for T in (Float32, Float64, Complex64, Complex128) |
| 25 | + @testset "largest = $largest" for largest in (true, false) |
| 26 | + A = rand(T, n, n) |
| 27 | + A = A' * A + I |
| 28 | + b = rand(T, n, 1) |
| 29 | + tol = √eps(real(T)) |
| 30 | + |
| 31 | + λ, x = lobpcg(A, largest, b; tol=tol, maxiter=Inf, log=false) |
| 32 | + @test norm(A*x - x*λ) ≤ tol |
| 33 | + |
| 34 | + # If you start from the exact solution, you should converge immediately |
| 35 | + λ, x, ch = lobpcg(A, largest, x; tol=10tol, log=true) |
| 36 | + @test length(ch) ≤ 1 |
| 37 | + end |
| 38 | + end |
| 39 | + end |
| 40 | + @testset "Generalized eigenvalue problem" begin |
| 41 | + @testset "Matrix{$T}" for T in (Float32, Float64, Complex64, Complex128) |
| 42 | + @testset "largest = $largest" for largest in (true, false) |
| 43 | + A = rand(T, n, n) |
| 44 | + A = A' * A + I |
| 45 | + B = rand(T, n, n) |
| 46 | + B = B' * B + I |
| 47 | + b = rand(T, n, 1) |
| 48 | + tol = √eps(real(T)) |
| 49 | + |
| 50 | + λ, x, ch = lobpcg(A, B, largest, b; tol=tol, maxiter=Inf, log=true) |
| 51 | + @show max_err(A*x - B*x*diagm(λ)), tol |
| 52 | + @test max_err(A*x - B*x*diagm(λ)) ≤ tol |
| 53 | + |
| 54 | + # If you start from the exact solution, you should converge immediately |
| 55 | + λ, x, ch = lobpcg(A, B, largest, x; tol=10tol, log=true) |
| 56 | + @show length(ch) |
| 57 | + @test length(ch) ≤ 1 |
| 58 | + end |
| 59 | + end |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + @testset "Sparse Laplacian" begin |
| 64 | + A = laplace_matrix(Float64, 20, 2) |
| 65 | + rhs = randn(size(A, 2), 1) |
| 66 | + scale!(rhs, inv(norm(rhs))) |
| 67 | + tol = 1e-5 |
| 68 | + |
| 69 | + @testset "Matrix" begin |
| 70 | + @testset "largest = $largest" for largest in (true, false) |
| 71 | + λ, xLOBPCG = lobpcg(A, largest, rhs; tol=tol, maxiter=Inf) |
| 72 | + @test norm(A * xLOBPCG - xLOBPCG * λ) ≤ tol |
| 73 | + end |
| 74 | + end |
| 75 | + end |
| 76 | + end |
| 77 | + @testset "Two eigenvalues" begin |
| 78 | + @testset "Small full system" begin |
| 79 | + n = 50 |
| 80 | + @testset "Simple eigenvalue problem" begin |
| 81 | + @testset "Matrix{$T}" for T in (Float32, Float64, Complex64, Complex128) |
| 82 | + @testset "largest = $largest" for largest in (true, false) |
| 83 | + A = rand(T, n, n) |
| 84 | + A = A' * A + I |
| 85 | + b = rand(T, n, 2) |
| 86 | + tol = √eps(real(T)) |
| 87 | + |
| 88 | + λ, x = lobpcg(A, largest, b; tol=tol, maxiter=Inf, log=false) |
| 89 | + @test max_err(A*x - x*diagm(λ)) ≤ tol |
| 90 | + |
| 91 | + # If you start from the exact solution, you should converge immediately |
| 92 | + λ, x, ch = lobpcg(A, largest, x; tol=10tol, log=true) |
| 93 | + @test length(ch) ≤ 1 |
| 94 | + end |
| 95 | + end |
| 96 | + end |
| 97 | + @testset "Generalized eigenvalue problem" begin |
| 98 | + @testset "Matrix{$T}" for T in (Float32, Float64, Complex64, Complex128) |
| 99 | + @testset "largest = $largest" for largest in (true, false) |
| 100 | + A = rand(T, n, n) |
| 101 | + A = A' * A + I |
| 102 | + B = rand(T, n, n) |
| 103 | + B = B' * B + I |
| 104 | + b = rand(T, n, 2) |
| 105 | + tol = √eps(real(T)) |
| 106 | + |
| 107 | + λ, x, ch = lobpcg(A, B, largest, b; tol=tol, maxiter=Inf, log=true) |
| 108 | + @show max_err(A*x - B*x*diagm(λ)), tol |
| 109 | + @test max_err(A*x - B*x*diagm(λ)) ≤ tol |
| 110 | + |
| 111 | + # If you start from the exact solution, you should converge immediately |
| 112 | + λ, x, ch = lobpcg(A, B, largest, x; tol=10tol, log=true) |
| 113 | + @show length(ch) |
| 114 | + @test length(ch) ≤ 1 |
| 115 | + end |
| 116 | + end |
| 117 | + end |
| 118 | + end |
| 119 | + end |
| 120 | +end |
0 commit comments