|
| 1 | +using Test, LinearMaps, LinearAlgebra |
| 2 | + |
| 3 | +@testset "block maps" begin |
| 4 | + @testset "hcat" begin |
| 5 | + for elty in (Float32, Float64, ComplexF64) |
| 6 | + A11 = rand(elty, 10, 10) |
| 7 | + A12 = rand(elty, 10, 20) |
| 8 | + L = @inferred hcat(LinearMap(A11), LinearMap(A12)) |
| 9 | + @test L isa LinearMaps.BlockMap{elty} |
| 10 | + A = [A11 A12] |
| 11 | + x = rand(30) |
| 12 | + @test size(L) == size(A) |
| 13 | + @test Matrix(L) ≈ A |
| 14 | + @test L * x ≈ A * x |
| 15 | + L = @inferred hcat(LinearMap(A11), LinearMap(A12), LinearMap(A11)) |
| 16 | + A = [A11 A12 A11] |
| 17 | + @test Matrix(L) ≈ A |
| 18 | + A = [I I I A11 A11 A11] |
| 19 | + L = @inferred hcat(I, I, I, LinearMap(A11), LinearMap(A11), LinearMap(A11)) |
| 20 | + @test L == [I I I LinearMap(A11) LinearMap(A11) LinearMap(A11)] |
| 21 | + x = rand(elty, 60) |
| 22 | + @test L isa LinearMaps.BlockMap{elty} |
| 23 | + @test L * x ≈ A * x |
| 24 | + A11 = rand(elty, 11, 10) |
| 25 | + A12 = rand(elty, 10, 20) |
| 26 | + @test_throws DimensionMismatch hcat(LinearMap(A11), LinearMap(A12)) |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + @testset "vcat" begin |
| 31 | + for elty in (Float32, Float64, ComplexF64) |
| 32 | + A11 = rand(elty, 10, 10) |
| 33 | + L = @inferred vcat(LinearMap(A11)) |
| 34 | + @test L == [LinearMap(A11);] |
| 35 | + @test Matrix(L) ≈ A11 |
| 36 | + A21 = rand(elty, 20, 10) |
| 37 | + L = @inferred vcat(LinearMap(A11), LinearMap(A21)) |
| 38 | + @test L isa LinearMaps.BlockMap{elty} |
| 39 | + A = [A11; A21] |
| 40 | + x = rand(10) |
| 41 | + @test size(L) == size(A) |
| 42 | + @test Matrix(L) ≈ A |
| 43 | + @test L * x ≈ A * x |
| 44 | + A = [I; I; I; A11; A11; A11] |
| 45 | + L = @inferred vcat(I, I, I, LinearMap(A11), LinearMap(A11), LinearMap(A11)) |
| 46 | + @test L == [I; I; I; LinearMap(A11); LinearMap(A11); LinearMap(A11)] |
| 47 | + x = rand(elty, 10) |
| 48 | + @test L isa LinearMaps.BlockMap{elty} |
| 49 | + @test L * x ≈ A * x |
| 50 | + A11 = rand(elty, 10, 11) |
| 51 | + A21 = rand(elty, 20, 10) |
| 52 | + @test_throws DimensionMismatch vcat(LinearMap(A11), LinearMap(A21)) |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + @testset "hvcat" begin |
| 57 | + for elty in (Float32, Float64, ComplexF64) |
| 58 | + A11 = rand(elty, 10, 10) |
| 59 | + A12 = rand(elty, 10, 20) |
| 60 | + A21 = rand(elty, 20, 10) |
| 61 | + A22 = rand(elty, 20, 20) |
| 62 | + A = [A11 A12; A21 A22] |
| 63 | + @inferred hvcat((2,2), LinearMap(A11), LinearMap(A12), LinearMap(A21), LinearMap(A22)) |
| 64 | + L = [LinearMap(A11) LinearMap(A12); LinearMap(A21) LinearMap(A22)] |
| 65 | + @test @inferred !issymmetric(L) |
| 66 | + @test @inferred !ishermitian(L) |
| 67 | + x = rand(30) |
| 68 | + @test L isa LinearMaps.BlockMap{elty} |
| 69 | + @test size(L) == size(A) |
| 70 | + @test L * x ≈ A * x |
| 71 | + @test Matrix(L) ≈ A |
| 72 | + A = [I A12; A21 I] |
| 73 | + @inferred hvcat((2,2), I, LinearMap(A12), LinearMap(A21), I) |
| 74 | + L = @inferred hvcat((2,2), I, LinearMap(A12), LinearMap(A21), I) |
| 75 | + @test L isa LinearMaps.BlockMap{elty} |
| 76 | + @test size(L) == (30, 30) |
| 77 | + @test Matrix(L) ≈ A |
| 78 | + @test L * x ≈ A * x |
| 79 | + A = rand(elty, 10,10); LA = LinearMap(A) |
| 80 | + B = rand(elty, 20,30); LB = LinearMap(B) |
| 81 | + @test [LA LA LA; LB] isa LinearMaps.BlockMap{elty} |
| 82 | + @test Matrix([LA LA LA; LB]) ≈ [A A A; B] |
| 83 | + @test [LB; LA LA LA] isa LinearMaps.BlockMap{elty} |
| 84 | + @test Matrix([LB; LA LA LA]) ≈ [B; A A A] |
| 85 | + @test [I; LA LA LA] isa LinearMaps.BlockMap{elty} |
| 86 | + @test Matrix([I; LA LA LA]) ≈ [I; A A A] |
| 87 | + A12 = LinearMap(rand(elty, 10, 21)) |
| 88 | + A21 = LinearMap(rand(elty, 20, 10)) |
| 89 | + @test_throws DimensionMismatch A = [I A12; A21 I] |
| 90 | + @test_throws DimensionMismatch A = [I A21; A12 I] |
| 91 | + @test_throws DimensionMismatch A = [A12 A12; A21 A21] |
| 92 | + @test_throws DimensionMismatch A = [A12 A21; A12 A21] |
| 93 | + |
| 94 | + # basic test of "misaligned" blocks |
| 95 | + M = ones(elty, 3, 2) # non-square |
| 96 | + A = LinearMap(M) |
| 97 | + B = [I A; A I] |
| 98 | + C = [I M; M I] |
| 99 | + @test B isa LinearMaps.BlockMap{elty} |
| 100 | + @test Matrix(B) == C |
| 101 | + @test Matrix(transpose(B)) == transpose(C) |
| 102 | + @test Matrix(adjoint(B)) == C' |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + @testset "adjoint/transpose" begin |
| 107 | + for elty in (Float32, Float64, ComplexF64), transform in (transpose, adjoint) |
| 108 | + A12 = rand(elty, 10, 10) |
| 109 | + A = [I A12; transform(A12) I] |
| 110 | + L = [I LinearMap(A12); transform(LinearMap(A12)) I] |
| 111 | + if elty <: Complex |
| 112 | + if transform == transpose |
| 113 | + @test @inferred issymmetric(L) |
| 114 | + else |
| 115 | + @test @inferred ishermitian(L) |
| 116 | + end |
| 117 | + end |
| 118 | + if elty <: Real |
| 119 | + @test @inferred ishermitian(L) |
| 120 | + @test @inferred issymmetric(L) |
| 121 | + end |
| 122 | + x = rand(elty, 20) |
| 123 | + @test L isa LinearMaps.LinearMap{elty} |
| 124 | + @test size(L) == size(A) |
| 125 | + @test L * x ≈ A * x |
| 126 | + @test Matrix(L) ≈ A |
| 127 | + Lt = @inferred transform(L) |
| 128 | + @test Lt isa LinearMaps.LinearMap{elty} |
| 129 | + @test Lt * x ≈ transform(A) * x |
| 130 | + Lt = @inferred transform(LinearMap(L)) |
| 131 | + @test Lt * x ≈ transform(A) * x |
| 132 | + @test Matrix(Lt) ≈ Matrix(transform(A)) |
| 133 | + A21 = rand(elty, 10, 10) |
| 134 | + A = [I A12; A21 I] |
| 135 | + L = [I LinearMap(A12); LinearMap(A21) I] |
| 136 | + Lt = @inferred transform(L) |
| 137 | + @test Lt isa LinearMaps.LinearMap{elty} |
| 138 | + @test Lt * x ≈ transform(A) * x |
| 139 | + @test Matrix(Lt) ≈ Matrix(transform(A)) |
| 140 | + end |
| 141 | + end |
| 142 | +end |
0 commit comments