Skip to content

Commit

Permalink
Linrange scaling (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
danmackinlay authored and timholy committed Jan 29, 2019
1 parent ebe5044 commit 73afe12
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/scaling/scaling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
29 changes: 28 additions & 1 deletion test/scaling/scaling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 73afe12

Please sign in to comment.