|
| 1 | +using Test, LinearMaps, LinearAlgebra, SparseArrays, IterativeSolvers |
| 2 | + |
| 3 | +@testset "inversemap" begin |
| 4 | + # First argument to cg! doubles as the initial guess so we make sure it is 0 instead |
| 5 | + # of potential garbage since we don't control the allocation of the output vector. |
| 6 | + cgz! = (x, A, b) -> IterativeSolvers.cg!(fill!(x, 0), A, b) |
| 7 | + # Dense test data |
| 8 | + A = rand(10, 10) + 5I; A = A'A |
| 9 | + B = rand(10, 10) |
| 10 | + b = rand(10) |
| 11 | + # LU |
| 12 | + @test A \ b ≈ InverseMap(lu(A)) * b |
| 13 | + # Cholesky |
| 14 | + @test A \ b ≈ InverseMap(cholesky(A)) * b |
| 15 | + # Specify solver |
| 16 | + @test A \ b ≈ InverseMap(A; solver=cgz!) * b atol=1e-8 |
| 17 | + # Composition with other maps |
| 18 | + @test A \ B * b ≈ InverseMap(lu(A)) * B * b |
| 19 | + # Composition: make sure solvers called with vector B * b and not matrix B |
| 20 | + my_ldiv! = (y, A, x) -> begin |
| 21 | + @test x isa AbstractVector |
| 22 | + @test x ≈ B * b |
| 23 | + return ldiv!(y, lu(A), x) |
| 24 | + end |
| 25 | + @test A \ B * b ≈ InverseMap(A; solver=my_ldiv!) * B * b |
| 26 | + |
| 27 | + # Sparse test data |
| 28 | + A = sprand(10, 10, 0.2) + 5I; A = A'A |
| 29 | + B = sprand(10, 10, 0.5) |
| 30 | + @test A \ b ≈ InverseMap(lu(A)) * b |
| 31 | + # Cholesky (CHOLMOD doesn't support inplace ldiv!) |
| 32 | + my_ldiv! = (y, A, x) -> copy!(y, A \ x) |
| 33 | + @test A \ b ≈ InverseMap(cholesky(A); solver=my_ldiv!) * b |
| 34 | + # Specify solver |
| 35 | + @test A \ b ≈ InverseMap(A; solver=cgz!) * b atol=1e-8 |
| 36 | + # Composition with other maps |
| 37 | + @test A \ (B * b) ≈ InverseMap(lu(A)) * B * b |
| 38 | + # Composition: make sure solver is called with vector B * b and not matrix B |
| 39 | + my_ldiv! = (y, A, x) -> begin |
| 40 | + @test x isa AbstractVector |
| 41 | + @test x ≈ B * b |
| 42 | + return ldiv!(y, lu(A), x) |
| 43 | + end |
| 44 | + @test A \ (B * b) ≈ InverseMap(A; solver=my_ldiv!) * B * b |
| 45 | + |
| 46 | + # Interface testing: note that not all combinations of factorization and |
| 47 | + # is(symmetric|hermitian|posdef) and transpose/adjoint are supported by LinearAlgebra, |
| 48 | + # so we test this for a custom type just to make sure the call is forwarded correctly |
| 49 | + # and then run some tests for supported combinations. |
| 50 | + struct TestMap{T} <: LinearMap{T} |
| 51 | + A::Matrix{T} |
| 52 | + end |
| 53 | + Base.size(tm::TestMap) = size(tm.A) |
| 54 | + Base.transpose(tm::TestMap) = transpose(tm.A) |
| 55 | + Base.adjoint(tm::TestMap) = adjoint(tm.A) |
| 56 | + LinearAlgebra.issymmetric(tm::TestMap) = issymmetric(tm.A) |
| 57 | + LinearAlgebra.ishermitian(tm::TestMap) = ishermitian(tm.A) |
| 58 | + LinearAlgebra.isposdef(tm::TestMap) = isposdef(tm.A) |
| 59 | + A = [5.0 2.0; 2.0 4.0] |
| 60 | + itm = InverseMap(TestMap(A)) |
| 61 | + @test size(itm) == size(A) |
| 62 | + @test transpose(itm).A === transpose(A) |
| 63 | + @test adjoint(itm).A === adjoint(A) |
| 64 | + @test issymmetric(itm) |
| 65 | + @test ishermitian(itm) |
| 66 | + @test isposdef(itm) |
| 67 | + ## Real symmetric (and Hermitian) |
| 68 | + A = Float64[3 2; 2 5]; x = rand(2) |
| 69 | + ### Wrapping a matrix and factorize in solver |
| 70 | + iA = InverseMap(A; solver=(y, A, x)->ldiv!(y, cholesky(A), x)) |
| 71 | + @test ishermitian(A) == ishermitian(iA) == true |
| 72 | + @test issymmetric(A) == issymmetric(iA) == true |
| 73 | + @test isposdef(A) == isposdef(iA) == true |
| 74 | + @test A \ x ≈ iA * x |
| 75 | + if VERSION >= v"1.8.0-" |
| 76 | + @test transpose(A) \ x ≈ transpose(iA) * x |
| 77 | + @test adjoint(A) \ x ≈ adjoint(iA) * x |
| 78 | + end |
| 79 | + ### Wrapping a factorization |
| 80 | + iA = InverseMap(cholesky(A)) |
| 81 | + # @test ishermitian(A) == ishermitian(iA) == true |
| 82 | + # @test issymmetric(A) == issymmetric(iA) == true |
| 83 | + @test isposdef(A) == isposdef(iA) == true |
| 84 | + @test A \ x ≈ iA * x |
| 85 | + # @test transpose(A) \ x ≈ transpose(iA) * x |
| 86 | + if VERSION >= v"1.7.0" |
| 87 | + @test adjoint(A) \ x ≈ adjoint(iA) * x |
| 88 | + end |
| 89 | + ## Real non-symmetric |
| 90 | + A = Float64[3 2; -2 5]; x = rand(2) |
| 91 | + ### Wrapping a matrix and factorize in solver |
| 92 | + iA = InverseMap(A; solver=(y, A, x)->ldiv!(y, lu(A), x)) |
| 93 | + @test ishermitian(A) == ishermitian(iA) == false |
| 94 | + @test issymmetric(A) == issymmetric(iA) == false |
| 95 | + @test isposdef(A) == isposdef(iA) == false |
| 96 | + @test A \ x ≈ iA * x |
| 97 | + @test transpose(A) \ x ≈ transpose(iA) * x |
| 98 | + @test adjoint(A) \ x ≈ adjoint(iA) * x |
| 99 | + ### Wrapping a factorization |
| 100 | + iA = InverseMap(lu(A)) |
| 101 | + # @test ishermitian(A) == ishermitian(iA) == true |
| 102 | + # @test issymmetric(A) == issymmetric(iA) == true |
| 103 | + # @test isposdef(A) == isposdef(iA) == true |
| 104 | + @test A \ x ≈ iA * x |
| 105 | + @test transpose(A) \ x ≈ transpose(iA) * x |
| 106 | + @test adjoint(A) \ x ≈ adjoint(iA) * x |
| 107 | + ## Complex Hermitian |
| 108 | + A = ComplexF64[3 2im; -2im 5]; x = rand(ComplexF64, 2) |
| 109 | + ### Wrapping a matrix and factorize in solver |
| 110 | + iA = InverseMap(A; solver=(y, A, x)->ldiv!(y, cholesky(A), x)) |
| 111 | + @test ishermitian(A) == ishermitian(iA) == true |
| 112 | + @test issymmetric(A) == issymmetric(iA) == false |
| 113 | + @test isposdef(A) == isposdef(iA) == true |
| 114 | + @test A \ x ≈ iA * x |
| 115 | + if VERSION >= v"1.8.0-" |
| 116 | + @test transpose(A) \ x ≈ transpose(iA) * x |
| 117 | + @test adjoint(A) \ x ≈ adjoint(iA) * x |
| 118 | + end |
| 119 | + ### Wrapping a factorization |
| 120 | + iA = InverseMap(cholesky(A)) |
| 121 | + # @test ishermitian(A) == ishermitian(iA) == true |
| 122 | + # @test issymmetric(A) == issymmetric(iA) == true |
| 123 | + @test isposdef(A) == isposdef(iA) == true |
| 124 | + @test A \ x ≈ iA * x |
| 125 | + # @test transpose(A) \ x ≈ transpose(iA) * x |
| 126 | + if VERSION >= v"1.7.0" |
| 127 | + @test adjoint(A) \ x ≈ adjoint(iA) * x |
| 128 | + end |
| 129 | + ## Complex non-Hermitian |
| 130 | + A = ComplexF64[3 2im; 3im 5]; x = rand(ComplexF64, 2) |
| 131 | + ### Wrapping a matrix and factorize in solver |
| 132 | + iA = InverseMap(A; solver=(y, A, x)->ldiv!(y, lu(A), x)) |
| 133 | + @test ishermitian(A) == ishermitian(iA) == false |
| 134 | + @test issymmetric(A) == issymmetric(iA) == false |
| 135 | + @test isposdef(A) == isposdef(iA) == false |
| 136 | + @test A \ x ≈ iA * x |
| 137 | + @test transpose(A) \ x ≈ transpose(iA) * x |
| 138 | + @test adjoint(A) \ x ≈ adjoint(iA) * x |
| 139 | + ### Wrapping a factorization |
| 140 | + iA = InverseMap(lu(A)) |
| 141 | + # @test ishermitian(A) == ishermitian(iA) == true |
| 142 | + # @test issymmetric(A) == issymmetric(iA) == true |
| 143 | + # @test isposdef(A) == isposdef(iA) == true |
| 144 | + @test A \ x ≈ iA * x |
| 145 | + @test transpose(A) \ x ≈ transpose(iA) * x |
| 146 | + @test adjoint(A) \ x ≈ adjoint(iA) * x |
| 147 | + |
| 148 | + # Example from https://www.dealii.org/current/doxygen/deal.II/step_20.html#SolvingusingtheSchurcomplement |
| 149 | + M = sparse(2.0I, 10, 10) + sprand(10, 10, 0.1); M = M'M |
| 150 | + iM = InverseMap(M; solver=cgz!) |
| 151 | + B = sparse(5.0I, 10, 5) |
| 152 | + F = rand(10) |
| 153 | + G = rand(5) |
| 154 | + ## Solve using Schur complement |
| 155 | + G′ = B' * iM * F - G |
| 156 | + S = B' * iM * B |
| 157 | + P = IterativeSolvers.cg(S, G′) |
| 158 | + U = IterativeSolvers.cg(M, F - B * P) |
| 159 | + ## Solve using standard method and compare |
| 160 | + @test [M B; B' 0I] \ [F; G] ≈ [U; P] atol=1e-8 |
| 161 | +end |
0 commit comments