|
| 1 | +using ApproxFunBase |
| 2 | +@testset "ApproxFunOrthogonalPolynomials" begin |
| 3 | + @test (@inferred Fun()) == Fun(x->x, Chebyshev()) |
| 4 | + @test (@inferred norm(Fun())) ≈ norm(Fun(), 2) ≈ √(2/3) # √∫x^2 dx over -1..1 |
| 5 | + |
| 6 | + v = rand(4) |
| 7 | + v2 = transform(NormalizedChebyshev(), v) |
| 8 | + @test itransform(NormalizedChebyshev(), v2) ≈ v |
| 9 | + |
| 10 | + f = @inferred Fun(x->x^2, Chebyshev()) |
| 11 | + v = @inferred coefficients(f, Chebyshev(), Legendre()) |
| 12 | + @test eltype(v) == eltype(coefficients(f)) |
| 13 | + @test v ≈ coefficients(Fun(x->x^2, Legendre())) |
| 14 | + |
| 15 | + # inference check for coefficients |
| 16 | + v = @inferred coefficients(Float64[0,0,1], Chebyshev(), Ultraspherical(1)) |
| 17 | + @test v ≈ [-0.5, 0, 0.5] |
| 18 | + |
| 19 | + @testset "int coeffs" begin |
| 20 | + f = Fun(Chebyshev(), [0,1]) |
| 21 | + @test f(0.4) ≈ 0.4 |
| 22 | + f = Fun(NormalizedChebyshev(), [0,1]) |
| 23 | + @test f(0.4) ≈ 0.4 * √(2/pi) |
| 24 | + |
| 25 | + f = Fun(Chebyshev(), [1]) |
| 26 | + @test f(0.4) ≈ 1 |
| 27 | + f = Fun(NormalizedChebyshev(), [1]) |
| 28 | + @test f(0.4) ≈ √(1/pi) |
| 29 | + end |
| 30 | + |
| 31 | + @testset "pad" begin |
| 32 | + @testset "Fun" begin |
| 33 | + f = Fun() |
| 34 | + zf = zero(f) |
| 35 | + @test (@inferred pad([f], 3)) == [f, zf, zf] |
| 36 | + @test (@inferred pad([f, zf], 1)) == [f] |
| 37 | + v = [f, zf] |
| 38 | + @test @inferred pad!(v, 1) == [f] |
| 39 | + @test length(v) == 1 |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + @testset "inplace transform" begin |
| 44 | + @testset for sp_c in Any[Legendre(), Chebyshev(), Jacobi(1,2), Jacobi(0.3, 2.3), |
| 45 | + Ultraspherical(1), Ultraspherical(2)] |
| 46 | + @testset for sp in Any[sp_c, NormalizedPolynomialSpace(sp_c)] |
| 47 | + v = rand(10) |
| 48 | + v2 = copy(v) |
| 49 | + @test itransform!(sp, transform!(sp, v)) ≈ v |
| 50 | + @test transform!(sp, v) ≈ transform(sp, v2) |
| 51 | + @test itransform(sp, v) ≈ v2 |
| 52 | + @test itransform!(sp, v) ≈ v2 |
| 53 | + |
| 54 | + # different vector |
| 55 | + p_fwd = ApproxFunBase.plan_transform!(sp, v) |
| 56 | + p_inv = ApproxFunBase.plan_itransform!(sp, v) |
| 57 | + @test p_inv * copy(p_fwd * copy(v)) ≈ v |
| 58 | + end |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + @testset "conversion" begin |
| 63 | + C12 = Conversion(Chebyshev(), NormalizedLegendre()) |
| 64 | + C21 = Conversion(NormalizedLegendre(), Chebyshev()) |
| 65 | + @test Matrix((C12 * C21)[1:10, 1:10]) ≈ I |
| 66 | + @test Matrix((C21 * C12)[1:10, 1:10]) ≈ I |
| 67 | + |
| 68 | + C12 = Conversion(Chebyshev(), NormalizedPolynomialSpace(Ultraspherical(1))) |
| 69 | + C1C2 = Conversion(Ultraspherical(1), NormalizedPolynomialSpace(Ultraspherical(1))) * |
| 70 | + Conversion(Chebyshev(), Ultraspherical(1)) |
| 71 | + @test Matrix(C12[1:10, 1:10]) ≈ Matrix(C1C2[1:10, 1:10]) |
| 72 | + end |
| 73 | + |
| 74 | + @testset "union" begin |
| 75 | + @test union(Chebyshev(), NormalizedLegendre()) == Jacobi(Chebyshev()) |
| 76 | + @test union(Chebyshev(), Legendre()) == Jacobi(Chebyshev()) |
| 77 | + end |
| 78 | + |
| 79 | + @testset "Fun constructor" begin |
| 80 | + # we make the fun go through somewhat complicated chains of functions |
| 81 | + # that break inference of the space |
| 82 | + # however, the type of coefficients should be inferred correctly. |
| 83 | + f = Fun(Chebyshev(0..1)) |
| 84 | + newfc(f) = coefficients(Fun(Fun(f, Legendre(0..1)), space(f))) |
| 85 | + newvals(f) = values(Fun(Fun(f, Legendre(0..1)), space(f))) |
| 86 | + @test newfc(f) ≈ coefficients(f) |
| 87 | + @test newvals(f) ≈ values(f) |
| 88 | + |
| 89 | + newfc2(f) = coefficients(chop(pad(f, 10))) |
| 90 | + @test newfc2(f) == coefficients(f) |
| 91 | + |
| 92 | + f2 = Fun(space(f), view(Float64[1:4;], :)) |
| 93 | + f3 = Fun(space(f), Float64[1:4;]) |
| 94 | + @test newvals(f2) ≈ values(f3) |
| 95 | + @test values(f2) ≈ values(f3) |
| 96 | + |
| 97 | + # Ensure no trailing zeros |
| 98 | + f = Fun(Ultraspherical(0.5, 0..1)) |
| 99 | + cf = coefficients(f) |
| 100 | + @test findlast(!iszero, cf) == length(cf) |
| 101 | + |
| 102 | + @testset "OneHotVector" begin |
| 103 | + for n in [1, 3, 10_000] |
| 104 | + f = Fun(Chebyshev(), [zeros(n-1); 1]) |
| 105 | + g = ApproxFunBase.basisfunction(Chebyshev(), n) |
| 106 | + @test f == g |
| 107 | + @test f(0.5) == g(0.5) |
| 108 | + end |
| 109 | + end |
| 110 | + end |
| 111 | + |
| 112 | + @testset "multiplication of Funs" begin |
| 113 | + f = Fun(Chebyshev(), Float64[1:101;]) |
| 114 | + g = Fun(Chebyshev(), Float64[1:101;]*im) |
| 115 | + @test f(0.5)*g(0.5) ≈ (f*g)(0.5) |
| 116 | + end |
| 117 | + |
| 118 | + @testset "Multivariate" begin |
| 119 | + @testset for S in Any[Chebyshev(), Legendre()] |
| 120 | + f = Fun(x->ones(2,2), S) |
| 121 | + @test (f+1) * f ≈ (1+f) * f ≈ f^2 + f |
| 122 | + @test (f-1) * f ≈ f^2 - f |
| 123 | + @test (1-f) * f ≈ f - f^2 |
| 124 | + @test f + f ≈ 2f ≈ f*2 |
| 125 | + end |
| 126 | + end |
| 127 | + |
| 128 | + @testset "static coeffs" begin |
| 129 | + f = Fun(Chebyshev(), SA[1,2,3]) |
| 130 | + g = Fun(Chebyshev(), [1,2,3]) |
| 131 | + @test coefficients(f^2) == coefficients(g^2) |
| 132 | + end |
| 133 | + |
| 134 | + @testset "special functions" begin |
| 135 | + for f in Any[Fun(), Fun(-0.5..1), Fun(Segment(1.0+im,2.0+2im))] |
| 136 | + for spfn in Any[sin, cos, exp] |
| 137 | + p = leftendpoint(domain(f)) |
| 138 | + @test spfn(f)(p) ≈ spfn(p) atol=1e-14 |
| 139 | + end |
| 140 | + end |
| 141 | + end |
| 142 | + |
| 143 | + @testset "Derivative" begin |
| 144 | + @test Derivative() == Derivative() |
| 145 | + for d in Any[(), (0..1,)] |
| 146 | + for ST in Any[Chebyshev, Legendre, |
| 147 | + (x...) -> Jacobi(2,2,x...), (x...) -> Jacobi(1.5,2.5,x...)] |
| 148 | + S1 = ST(d...) |
| 149 | + for S in [S1, NormalizedPolynomialSpace(S1)] |
| 150 | + @test Derivative(S) == Derivative(S,1) |
| 151 | + @test Derivative(S)^2 == Derivative(S,2) |
| 152 | + f = Fun(x->x^3, S) |
| 153 | + @test Derivative(S) * f ≈ Fun(x->3x^2, S) |
| 154 | + @test Derivative(S,2) * f ≈ Fun(x->6x, S) |
| 155 | + @test Derivative(S,3) * f ≈ Fun(x->6, S) |
| 156 | + @test Derivative(S,4) * f ≈ zeros(S) |
| 157 | + end |
| 158 | + end |
| 159 | + end |
| 160 | + @test Derivative(Chebyshev()) != Derivative(Chebyshev(), 2) |
| 161 | + @test Derivative(Chebyshev()) != Derivative(Legendre()) |
| 162 | + end |
| 163 | + |
| 164 | + @testset "SubOperator" begin |
| 165 | + D = Derivative(Chebyshev()) |
| 166 | + S = @view D[1:10, 1:10] |
| 167 | + @test rowrange(S, 1) == 2:2 |
| 168 | + @test colrange(S, 2) == 1:1 |
| 169 | + @test (@inferred BandedMatrix(S)) == (@inferred Matrix(S)) |
| 170 | + end |
| 171 | + |
| 172 | + @testset "CachedOperator" begin |
| 173 | + C = cache(Derivative()) |
| 174 | + C = C : Chebyshev() → Ultraspherical(2) |
| 175 | + D = Derivative() : Chebyshev() → Ultraspherical(2) |
| 176 | + @test C[1:2, 1:0] == D[1:2, 1:0] |
| 177 | + @test C[1:10, 1:10] == D[1:10, 1:10] |
| 178 | + for col in 1:5, row in 1:5 |
| 179 | + @test C[row, col] == D[row, col] |
| 180 | + end |
| 181 | + end |
| 182 | + |
| 183 | + @testset "PartialInverseOperator" begin |
| 184 | + @testset "sanity check" begin |
| 185 | + A = UpperTriangular(rand(10, 10)) |
| 186 | + B = inv(A) |
| 187 | + for I in CartesianIndices(B) |
| 188 | + @test B[I] ≈ ApproxFunBase._getindexinv(A, Tuple(I)..., UpperTriangular) |
| 189 | + end |
| 190 | + end |
| 191 | + C = Conversion(Chebyshev(), Ultraspherical(1)) |
| 192 | + P = PartialInverseOperator(C, (0, 6)) |
| 193 | + Iapprox = (P * C)[1:10, 1:10] |
| 194 | + @test all(isone, diag(Iapprox)) |
| 195 | + for k in axes(Iapprox,1), j in k + 1:min(k + bandwidths(P,2), size(Iapprox, 2)) |
| 196 | + @test Iapprox[k,j] ≈ 0 atol=eps(eltype(Iapprox)) |
| 197 | + end |
| 198 | + B = AbstractMatrix(P[1:10, 1:10]) |
| 199 | + @testset for I in CartesianIndices(B) |
| 200 | + @test B[I] ≈ P[Tuple(I)...] rtol=1e-8 atol=eps(eltype(B)) |
| 201 | + end |
| 202 | + end |
| 203 | + |
| 204 | + @testset "istriu/istril" begin |
| 205 | + for D in Any[Derivative(Chebyshev()), |
| 206 | + Conversion(Chebyshev(), Legendre()), |
| 207 | + Multiplication(Fun(Chebyshev()), Chebyshev())] |
| 208 | + D2 = D[1:3, 1:3] |
| 209 | + for f in Any[istriu, istril] |
| 210 | + @test f(D) == f(D2) |
| 211 | + @test f(D') == f(D2') |
| 212 | + end |
| 213 | + end |
| 214 | + end |
| 215 | + |
| 216 | + @testset "inplace ldiv" begin |
| 217 | + @testset for T in [Float32, Float64, ComplexF32, ComplexF64] |
| 218 | + v = rand(T, 4) |
| 219 | + v2 = copy(v) |
| 220 | + ApproxFunBase.ldiv_coefficients!(Conversion(Chebyshev(), Ultraspherical(1)), v) |
| 221 | + @test ApproxFunBase.ldiv_coefficients(Conversion(Chebyshev(), Ultraspherical(1)), v2) ≈ v |
| 222 | + end |
| 223 | + end |
| 224 | + |
| 225 | + @testset "specialfunctionnormalizationpoint" begin |
| 226 | + a = @inferred ApproxFunBase.specialfunctionnormalizationpoint(exp,real,Fun()) |
| 227 | + @test a[1] == 1 |
| 228 | + @test a[2] ≈ exp(1) |
| 229 | + end |
| 230 | +end |
0 commit comments