|
1 |
| -module ScalingTests |
2 |
| - |
3 | 1 | using Interpolations
|
4 | 2 | using Test, LinearAlgebra
|
5 | 3 |
|
6 |
| -# Model linear interpolation of y = -3 + .5x by interpolating y=x |
7 |
| -# and then scaling to the new x range |
8 |
| - |
9 |
| -itp = interpolate(1:1.0:10, BSpline(Linear()), OnGrid()) |
| 4 | +@testset "Scaling" begin |
| 5 | + # Model linear interpolation of y = -3 + .5x by interpolating y=x |
| 6 | + # and then scaling to the new x range |
10 | 7 |
|
11 |
| -sitp = @inferred(scale(itp, -3:.5:1.5)) |
12 |
| -@test typeof(sitp) <: Interpolations.ScaledInterpolation |
13 |
| -@test parent(sitp) === itp |
14 |
| - |
15 |
| -for (x,y) in zip(-3:.05:1.5, 1:.1:10) |
16 |
| - @test sitp[x] ≈ y |
17 |
| -end |
| 8 | + itp = interpolate(1:1.0:10, BSpline(Linear()), OnGrid()) |
18 | 9 |
|
19 |
| -# Verify that it works in >1D, with different types of ranges |
| 10 | + sitp = @inferred(scale(itp, -3:.5:1.5)) |
| 11 | + @test typeof(sitp) <: Interpolations.ScaledInterpolation |
| 12 | + @test parent(sitp) === itp |
20 | 13 |
|
21 |
| -gauss(phi, mu, sigma) = exp(-(phi-mu)^2 / (2sigma)^2) |
22 |
| -testfunction(x,y) = gauss(x, 0.5, 4) * gauss(y, -.5, 2) |
| 14 | + for (x,y) in zip(-3:.05:1.5, 1:.1:10) |
| 15 | + @test sitp(x) ≈ y |
| 16 | + end |
23 | 17 |
|
24 |
| -xs = -5:.5:5 |
25 |
| -ys = -4:.2:4 |
26 |
| -zs = Float64[testfunction(x,y) for x in xs, y in ys] |
| 18 | + # Verify that it works in >1D, with different types of ranges |
27 | 19 |
|
28 |
| -itp2 = interpolate(zs, BSpline(Quadratic(Flat())), OnGrid()) |
29 |
| -sitp2 = @inferred scale(itp2, xs, ys) |
| 20 | + gauss(phi, mu, sigma) = exp(-(phi-mu)^2 / (2sigma)^2) |
| 21 | + testfunction(x,y) = gauss(x, 0.5, 4) * gauss(y, -.5, 2) |
30 | 22 |
|
31 |
| -for x in xs, y in ys |
32 |
| - @test testfunction(x,y) ≈ sitp2[x,y] |
33 |
| -end |
| 23 | + xs = -5:.5:5 |
| 24 | + ys = -4:.2:4 |
| 25 | + zs = Float64[testfunction(x,y) for x in xs, y in ys] |
34 | 26 |
|
35 |
| -# Test gradients of scaled grids |
36 |
| -xs = -pi:.1:pi |
37 |
| -ys = map(sin, xs) |
38 |
| -itp = interpolate(ys, BSpline(Linear()), OnGrid()) |
39 |
| -sitp = @inferred scale(itp, xs) |
| 27 | + itp2 = interpolate(zs, BSpline(Quadratic(Flat())), OnGrid()) |
| 28 | + sitp2 = @inferred scale(itp2, xs, ys) |
40 | 29 |
|
41 |
| -for x in -pi:.1:pi |
42 |
| - g = @inferred(Interpolations.gradient(sitp, x))[1] |
43 |
| - @test ≈(cos(x),g,atol=0.05) |
44 |
| -end |
| 30 | + for x in xs, y in ys |
| 31 | + @test testfunction(x,y) ≈ sitp2(x,y) |
| 32 | + end |
45 | 33 |
|
46 |
| -# Verify that return types are reasonable |
47 |
| -@inferred(getindex(sitp2, -3.4, 1.2)) |
48 |
| -@inferred(getindex(sitp2, -3, 1)) |
49 |
| -@inferred(getindex(sitp2, -3.4, 1)) |
50 |
| - |
51 |
| -sitp32 = @inferred scale(interpolate(Float32[testfunction(x,y) for x in -5:.5:5, y in -4:.2:4], BSpline(Quadratic(Flat())), OnGrid()), -5f0:.5f0:5f0, -4f0:.2f0:4f0) |
52 |
| -@test typeof(@inferred(getindex(sitp32, -3.4f0, 1.2f0))) == Float32 |
53 |
| - |
54 |
| -# Iteration |
55 |
| -itp = interpolate(rand(3,3,3), BSpline(Quadratic(Flat())), OnCell()) |
56 |
| -knots = map(d->1:10:21, 1:3) |
57 |
| -sitp = @inferred scale(itp, knots...) |
58 |
| - |
59 |
| -iter = @inferred(eachvalue(sitp)) |
60 |
| - |
61 |
| -@static if VERSION < v"0.7.0-DEV.5126" |
62 |
| - state = @inferred(start(iter)) |
63 |
| - @test !(@inferred(done(iter, state))) |
64 |
| - val, state = @inferred(next(iter, state)) |
65 |
| -else |
66 |
| - iter_next = iterate(iter) |
67 |
| - @test iter_next isa Tuple |
68 |
| - @test iter_next[1] isa Float64 |
69 |
| - state = iter_next[2] |
70 |
| - inferred_next = Base.return_types(iterate, (typeof(iter),)) |
71 |
| - @test length(inferred_next) == 1 |
72 |
| - @test inferred_next[1] == Union{Nothing,Tuple{Float64,typeof(state)}} |
73 |
| - iter_next = iterate(iter, state) |
74 |
| - @test iter_next isa Tuple |
75 |
| - @test iter_next[1] isa Float64 |
76 |
| - inferred_next = Base.return_types(iterate, (typeof(iter),typeof(state))) |
77 |
| - state = iter_next[2] |
78 |
| - @test length(inferred_next) == 1 |
79 |
| - @test inferred_next[1] == Union{Nothing,Tuple{Float64,typeof(state)}} |
80 |
| -end |
| 34 | + # Test gradients of scaled grids |
| 35 | + xs = -pi:.1:pi |
| 36 | + ys = map(sin, xs) |
| 37 | + itp = interpolate(ys, BSpline(Linear()), OnGrid()) |
| 38 | + sitp = @inferred scale(itp, xs) |
81 | 39 |
|
82 |
| -function foo!(dest, sitp) |
83 |
| - i = 0 |
84 |
| - for s in eachvalue(sitp) |
85 |
| - dest[i+=1] = s |
86 |
| - end |
87 |
| - dest |
88 |
| -end |
89 |
| -function bar!(dest, sitp) |
90 |
| - for I in CartesianIndices(size(dest)) |
91 |
| - dest[I] = sitp[I] |
| 40 | + for x in -pi:.1:pi |
| 41 | + g = @inferred(Interpolations.gradient(sitp, x))[1] |
| 42 | + @test ≈(cos(x),g,atol=0.05) |
92 | 43 | end
|
93 |
| - dest |
94 |
| -end |
95 |
| -rfoo = Array{Float64}(undef, Interpolations.ssize(sitp)) |
96 |
| -rbar = similar(rfoo) |
97 |
| -foo!(rfoo, sitp) |
98 |
| -bar!(rbar, sitp) |
99 |
| -@test rfoo ≈ rbar |
| 44 | + |
| 45 | + # Verify that return types are reasonable |
| 46 | + @inferred(sitp2(-3.4, 1.2)) |
| 47 | + @inferred(sitp2(-3, 1)) |
| 48 | + @inferred(sitp2(-3.4, 1)) |
| 49 | + |
| 50 | + sitp32 = @inferred scale(interpolate(Float32[testfunction(x,y) for x in -5:.5:5, y in -4:.2:4], BSpline(Quadratic(Flat())), OnGrid()), -5f0:.5f0:5f0, -4f0:.2f0:4f0) |
| 51 | + @test typeof(@inferred(sitp32(-3.4f0, 1.2f0))) == Float32 |
| 52 | + |
| 53 | + # # Iteration |
| 54 | + # itp = interpolate(rand(3,3,3), BSpline(Quadratic(Flat())), OnCell()) |
| 55 | + # knots = map(d->1:10:21, 1:3) |
| 56 | + # sitp = @inferred scale(itp, knots...) |
| 57 | + |
| 58 | + # iter = @inferred(eachvalue(sitp)) |
| 59 | + |
| 60 | + # iter_next = iterate(iter) |
| 61 | + # @test iter_next isa Tuple |
| 62 | + # @test iter_next[1] isa Float64 |
| 63 | + # state = iter_next[2] |
| 64 | + # inferred_next = Base.return_types(iterate, (typeof(iter),)) |
| 65 | + # @test length(inferred_next) == 1 |
| 66 | + # @test inferred_next[1] == Union{Nothing,Tuple{Float64,typeof(state)}} |
| 67 | + # iter_next = iterate(iter, state) |
| 68 | + # @test iter_next isa Tuple |
| 69 | + # @test iter_next[1] isa Float64 |
| 70 | + # inferred_next = Base.return_types(iterate, (typeof(iter),typeof(state))) |
| 71 | + # state = iter_next[2] |
| 72 | + # @test length(inferred_next) == 1 |
| 73 | + # @test inferred_next[1] == Union{Nothing,Tuple{Float64,typeof(state)}} |
| 74 | + |
| 75 | + # function foo!(dest, sitp) |
| 76 | + # i = 0 |
| 77 | + # for s in eachvalue(sitp) |
| 78 | + # dest[i+=1] = s |
| 79 | + # end |
| 80 | + # dest |
| 81 | + # end |
| 82 | + # function bar!(dest, sitp) |
| 83 | + # for I in CartesianIndices(size(dest)) |
| 84 | + # dest[I] = sitp[I] |
| 85 | + # end |
| 86 | + # dest |
| 87 | + # end |
| 88 | + # rfoo = Array{Float64}(undef, Interpolations.ssize(sitp)) |
| 89 | + # rbar = similar(rfoo) |
| 90 | + # foo!(rfoo, sitp) |
| 91 | + # bar!(rbar, sitp) |
| 92 | + # @test rfoo ≈ rbar |
100 | 93 |
|
101 | 94 | end
|
0 commit comments