diff --git a/src/scaling/scaling.jl b/src/scaling/scaling.jl index 6927c88a..65cde39f 100644 --- a/src/scaling/scaling.jl +++ b/src/scaling/scaling.jl @@ -92,9 +92,9 @@ coordlookup(r::UnitRange, x) = x - r.start + oneunit(eltype(r)) # coordlookup(i::Bool, r::AbstractRange, x) = i ? coordlookup(r, x) : convert(typeof(coordlookup(r,x)), x) coordlookup(r::StepRange, x) = (x - r.start) / r.step + oneunit(eltype(r)) -coordlookup(r::StepRangeLen, x) = (x - first(r)) / step(r) + oneunit(eltype(r)) -boundstep(r::StepRangeLen) = 0.5*step(r) -rescale_gradient(r::StepRangeLen, g) = g / step(r) +coordlookup(r::AbstractRange, x) = (x - first(r)) / step(r) + oneunit(eltype(r)) +boundstep(r::AbstractRange) = 0.5*step(r) +rescale_gradient(r::AbstractRange, g) = g / step(r) basetype(::Type{ScaledInterpolation{T,N,ITPT,IT,RT}}) where {T,N,ITPT,IT,RT} = ITPT basetype(sitp::ScaledInterpolation) = basetype(typeof(sitp)) diff --git a/test/scaling/scaling.jl b/test/scaling/scaling.jl index f9e42e7f..13d93985 100644 --- a/test/scaling/scaling.jl +++ b/test/scaling/scaling.jl @@ -11,12 +11,25 @@ using Test, LinearAlgebra, StaticArrays @test typeof(sitp) <: Interpolations.ScaledInterpolation @test parent(sitp) === itp - for (x,y) in zip(-3:.05:1.5, 1:.1:10) + for (x,y) in zip(-3:.05:1.5, 1:.1:10,) @test sitp(x) ≈ y end @test_throws ArgumentError scale(itp, reverse(-3:.5:1.5)) + # Model linear interpolation of y = -3 + .5x by interpolating y=x + # and then scaling to the new x range, this time using a LinRange, + # which should give the same values as with the StepRangeLen used previously + + itp = interpolate(1:1.0:10, BSpline(Linear())) + + sitp = @inferred(scale(itp, LinRange(-3,1.5,10))) + @test typeof(sitp) <: Interpolations.ScaledInterpolation + + for (x,y) in zip(-3:.05:1.5, 1:.1:10,) + @test sitp(x) ≈ y + end + # Verify that it works in >1D, with different types of ranges gauss(phi, mu, sigma) = exp(-(phi-mu)^2 / (2sigma)^2) @@ -58,6 +71,20 @@ using Test, LinearAlgebra, StaticArrays cos(x) * cos(y) -sin(x) * sin(y)] ≈ h atol=0.03 end + # Test Hessians of scaled grids with LinRange + xs = LinRange(-pi, pi, 62) + ys = LinRange(-pi, pi, 32) + zs = sin.(xs) .* sin.(ys') + itp = interpolate(zs, BSpline(Cubic(Line(OnGrid())))) + sitp = @inferred scale(itp, xs, ys) + + for x in xs[2:end-1], y in ys[2:end-1] + h = @inferred(Interpolations.hessian(sitp, x, y)) + @test issymmetric(h) + @test [-sin(x) * sin(y) cos(x) * cos(y) + cos(x) * cos(y) -sin(x) * sin(y)] ≈ h atol=0.03 + end + # Verify that return types are reasonable @inferred(sitp2(-3.4, 1.2)) @inferred(sitp2(-3, 1))