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