|
| 1 | +module ApproxFunBaseTest |
| 2 | + |
| 3 | +using ApproxFunBase |
| 4 | +using ApproxFunBase: plan_transform, plan_itransform, israggedbelow, RaggedMatrix, isbandedbelow, isbanded, |
| 5 | + blockstart, blockstop, resizedata! |
| 6 | +using BandedMatrices: rowstart, rowstop, colstart, colstop, BandedMatrix, bandwidth |
| 7 | +using BlockArrays |
| 8 | +using BlockArrays: blockrowstop, blockcolstop |
| 9 | +using BlockBandedMatrices |
| 10 | +using BlockBandedMatrices: isbandedblockbanded |
| 11 | +using DomainSets: dimension |
| 12 | +using InfiniteArrays |
| 13 | +using LinearAlgebra |
| 14 | +using Test |
| 15 | + |
| 16 | +# These routines are for the unit tests |
| 17 | + |
| 18 | +export testspace, testfunctional, testraggedbelowoperator, testbandedblockbandedoperator, |
| 19 | + testbandedoperator, testtransforms, testcalculus, testmultiplication, testinfoperator, |
| 20 | + testblockbandedoperator, testbandedbelowoperator |
| 21 | + |
| 22 | +# assert type in convert |
| 23 | +strictconvert(::Type{T}, x) where {T} = convert(T, x)::T |
| 24 | + |
| 25 | +## Spaces Tests |
| 26 | + |
| 27 | + |
| 28 | +function testtransforms(S::Space;minpoints=1,invertibletransform=true) |
| 29 | + # transform tests |
| 30 | + v = rand(max(minpoints,min(100,dimension(S)))) |
| 31 | + plan = plan_transform(S,v) |
| 32 | + @test transform(S,v) == plan*v |
| 33 | + |
| 34 | + iplan = plan_itransform(S,v) |
| 35 | + @test itransform(S,v) == iplan*v |
| 36 | + |
| 37 | + if invertibletransform |
| 38 | + for k=max(1,minpoints):min(5,dimension(S)) |
| 39 | + v = [zeros(k-1);1.0] |
| 40 | + @test transform(S,itransform(S,v)) ≈ v |
| 41 | + end |
| 42 | + |
| 43 | + @test transform(S,itransform(S,v)) ≈ v |
| 44 | + @test itransform(S,transform(S,v)) ≈ v |
| 45 | + end |
| 46 | +end |
| 47 | + |
| 48 | +function testcalculus(S::Space;haslineintegral=true,hasintegral=true) |
| 49 | + @testset for k=1:min(5,dimension(S)) |
| 50 | + v = [zeros(k-1);1.0] |
| 51 | + f = Fun(S,v) |
| 52 | + @test abs(DefiniteIntegral()*f-sum(f)) < 100eps() |
| 53 | + if haslineintegral |
| 54 | + @test DefiniteLineIntegral()*f ≈ linesum(f) |
| 55 | + end |
| 56 | + @test norm(Derivative()*f-f') < 100eps() |
| 57 | + if hasintegral |
| 58 | + @test norm(differentiate(integrate(f))-f) < 100eps() |
| 59 | + @test norm(differentiate(cumsum(f))-f) < 200eps() |
| 60 | + @test norm(first(cumsum(f))) < 100eps() |
| 61 | + end |
| 62 | + end |
| 63 | +end |
| 64 | + |
| 65 | +function testmultiplication(spa,spb) |
| 66 | + @testset for k=1:10 |
| 67 | + a = Fun(spa,[zeros(k-1);1.]) |
| 68 | + M = Multiplication(a,spb) |
| 69 | + pts = ApproxFunBase.checkpoints(rangespace(M)) |
| 70 | + for j=1:10 |
| 71 | + b = Fun(spb,[zeros(j-1);1.]) |
| 72 | + @test (M*b).(pts) ≈ a.(pts).*b.(pts) |
| 73 | + end |
| 74 | + end |
| 75 | +end |
| 76 | + |
| 77 | +function testspace(S::Space; |
| 78 | + minpoints=1,invertibletransform=true,haslineintegral=true,hasintegral=true, |
| 79 | + dualspace=S) |
| 80 | + testtransforms(S;minpoints=minpoints,invertibletransform=invertibletransform) |
| 81 | + testcalculus(S;haslineintegral=haslineintegral,hasintegral=hasintegral) |
| 82 | + if dualspace ≠ nothing |
| 83 | + testmultiplication(dualspace,S) |
| 84 | + end |
| 85 | +end |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | +## Operator Tests |
| 92 | + |
| 93 | +function backend_testfunctional(A) |
| 94 | + @test rowstart(A,1) ≥ 1 |
| 95 | + @test colstop(A,1) ≤ 1 |
| 96 | + @test bandwidth(A,1) ≤ 0 |
| 97 | + @test blockbandwidth(A,1) ≤ 0 |
| 98 | + |
| 99 | + B=A[1:10] |
| 100 | + @test eltype(B) == eltype(A) |
| 101 | + for k=1:5 |
| 102 | + @test B[k] ≈ A[k] |
| 103 | + @test isa(A[k],eltype(A)) |
| 104 | + end |
| 105 | + @test isa(A[1,1:10],Vector) |
| 106 | + @test isa(A[1:1,1:10],AbstractMatrix) |
| 107 | + @test B ≈ A[1,1:10] |
| 108 | + @test transpose(B) ≈ A[1:1,1:10] |
| 109 | + @test B[3:10] ≈ A[3:10] |
| 110 | + @test B ≈ [A[k] for k=1:10] |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + co=cache(A) |
| 115 | + @test co[1:10] ≈ A[1:10] |
| 116 | + @test co[1:10] ≈ A[1:10] |
| 117 | + @test co[20:30] ≈ A[1:30][20:30] ≈ A[20:30] |
| 118 | +end |
| 119 | + |
| 120 | +# Check that the tests pass after conversion as well |
| 121 | +function testfunctional(A::Operator{T}) where T<:Real |
| 122 | + backend_testfunctional(A) |
| 123 | + backend_testfunctional(Operator{Float64}(A)) |
| 124 | + backend_testfunctional(Operator{Float32}(A)) |
| 125 | + backend_testfunctional(Operator{ComplexF64}(A)) |
| 126 | +end |
| 127 | + |
| 128 | +function testfunctional(A::Operator{T}) where T<:Complex |
| 129 | + backend_testfunctional(A) |
| 130 | + backend_testfunctional(Operator{ComplexF32}(A)) |
| 131 | + backend_testfunctional(Operator{ComplexF64}(A)) |
| 132 | +end |
| 133 | + |
| 134 | +function backend_testinfoperator(A) |
| 135 | + @test isinf(size(A,1)) |
| 136 | + @test isinf(size(A,2)) |
| 137 | + B=A[1:5,1:5] |
| 138 | + @test eltype(B) == eltype(A) |
| 139 | + |
| 140 | + for k=1:5,j=1:5 |
| 141 | + @test B[k,j] ≈ A[k,j] |
| 142 | + @test isa(A[k,j],eltype(A)) |
| 143 | + end |
| 144 | + |
| 145 | + @test A[1:5,1:5][2:5,1:5] ≈ A[2:5,1:5] |
| 146 | + @test A[1:5,2:5] ≈ A[1:5,1:5][:,2:end] |
| 147 | + @test A[1:10,1:10][5:10,5:10] ≈ [A[k,j] for k=5:10,j=5:10] |
| 148 | + @test A[1:10,1:10][5:10,5:10] ≈ A[5:10,5:10] |
| 149 | + @test A[1:30,1:30][20:30,20:30] ≈ A[20:30,20:30] |
| 150 | + |
| 151 | + @test Matrix(A[Block(1):Block(3),Block(1):Block(3)]) ≈ Matrix(A[blockstart(rangespace(A),1):blockstop(rangespace(A),3),blockstart(domainspace(A),1):blockstop(domainspace(A),3)]) |
| 152 | + @test Matrix(A[Block(3):Block(4),Block(2):Block(4)]) ≈ Matrix(A[blockstart(rangespace(A),3):blockstop(rangespace(A),4),blockstart(domainspace(A),2):blockstop(domainspace(A),4)]) |
| 153 | + |
| 154 | + for k=1:10 |
| 155 | + @test isfinite(colstart(A,k)) && colstart(A,k) > 0 |
| 156 | + @test isfinite(rowstart(A,k)) && colstart(A,k) > 0 |
| 157 | + end |
| 158 | + |
| 159 | + co=cache(A) |
| 160 | + @test co[1:10,1:10] ≈ A[1:10,1:10] |
| 161 | + @test co[1:10,1:10] ≈ A[1:10,1:10] |
| 162 | + @test co[20:30,20:30] ≈ A[1:30,1:30][20:30,20:30] |
| 163 | + |
| 164 | + let C=cache(A) |
| 165 | + resizedata!(C,5,35) |
| 166 | + resizedata!(C,10,35) |
| 167 | + @test C.data[1:10,1:C.datasize[2]] ≈ A[1:10,1:C.datasize[2]] |
| 168 | + end |
| 169 | +end |
| 170 | + |
| 171 | +# Check that the tests pass after conversion as well |
| 172 | +function testinfoperator(A::Operator{T}) where T<:Real |
| 173 | + backend_testinfoperator(A) |
| 174 | + backend_testinfoperator(strictconvert(Operator{Float64}, A)) |
| 175 | + backend_testinfoperator(strictconvert(Operator{Float32}, A)) |
| 176 | + backend_testinfoperator(strictconvert(Operator{ComplexF64}, A)) |
| 177 | +end |
| 178 | + |
| 179 | +function testinfoperator(A::Operator{T}) where T<:Complex |
| 180 | + backend_testinfoperator(A) |
| 181 | + backend_testinfoperator(strictconvert(Operator{ComplexF32}, A)) |
| 182 | + backend_testinfoperator(strictconvert(Operator{ComplexF64}, A)) |
| 183 | +end |
| 184 | + |
| 185 | +function testraggedbelowoperator(A) |
| 186 | + @test israggedbelow(A) |
| 187 | + for k=1:20 |
| 188 | + @test isfinite(colstop(A,k)) |
| 189 | + end |
| 190 | + |
| 191 | + R = RaggedMatrix(view(A, 1:10, 1:min(10,size(A,2)))) |
| 192 | + for j=1:size(R,2) |
| 193 | + @test colstop(R,j) == min(colstop(A,j),10) |
| 194 | + end |
| 195 | + |
| 196 | + testinfoperator(A) |
| 197 | +end |
| 198 | + |
| 199 | +function testbandedbelowoperator(A) |
| 200 | + @test isbandedbelow(A) |
| 201 | + @test isfinite(bandwidth(A,1)) |
| 202 | + testraggedbelowoperator(A) |
| 203 | + |
| 204 | + for k=1:10 |
| 205 | + @test colstop(A,k) ≤ max(0,k + bandwidth(A,1)) |
| 206 | + end |
| 207 | +end |
| 208 | + |
| 209 | + |
| 210 | +function testalmostbandedoperator(A) |
| 211 | + testbandedbelowoperator(A) |
| 212 | +end |
| 213 | + |
| 214 | +function testbandedoperator(A) |
| 215 | + @test isbanded(A) |
| 216 | + @test isfinite(bandwidth(A,2)) |
| 217 | + testalmostbandedoperator(A) |
| 218 | + for k=1:10 |
| 219 | + @test rowstop(A,k) ≤ k + bandwidth(A,2) |
| 220 | + end |
| 221 | + |
| 222 | + @test isa(A[1:10,1:10],BandedMatrix) |
| 223 | +end |
| 224 | + |
| 225 | + |
| 226 | +function testblockbandedoperator(A) |
| 227 | + @test isblockbanded(A) |
| 228 | + testraggedbelowoperator(A) |
| 229 | + @test isfinite(blockbandwidth(A,2)) |
| 230 | + @test isfinite(blockbandwidth(A,1)) |
| 231 | + |
| 232 | + |
| 233 | + if -blockbandwidth(A,1) ≤ blockbandwidth(A,2) |
| 234 | + for K=1:10 |
| 235 | + @test K - blockbandwidth(A,2) ≤ blockcolstop(A,Block(K)).n[1] ≤ K + blockbandwidth(A,1) < ∞ |
| 236 | + @test K - blockbandwidth(A,1) ≤ blockrowstop(A,Block(K)).n[1] ≤ K + blockbandwidth(A,2) < ∞ |
| 237 | + end |
| 238 | + end |
| 239 | +end |
| 240 | + |
| 241 | +function testbandedblockbandedoperator(A) |
| 242 | + @test isbandedblockbanded(A) |
| 243 | + testblockbandedoperator(A) |
| 244 | + @test isfinite(subblockbandwidth(A,1)) |
| 245 | + @test isfinite(subblockbandwidth(A,2)) |
| 246 | + |
| 247 | + @test isa(A[Block.(1:4),Block.(1:4)], BandedBlockBandedMatrix) |
| 248 | +end |
| 249 | + |
| 250 | +end # module |
0 commit comments