Skip to content

Linrange scaling #294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there potentially more instances that need a consistent change? Something like rescale_hessian for instance?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. AFAICT no, rescale_hessian_components, for example, uses steps, which accepts any AbstractRange subtype. We should not have changed any behaviour. It presumably can't hurt to test the hessian scaling also though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, I think you have found an oversight. #165 was marked as resolved by #226. So it indeed could be that this had been fixed in some places but not all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are now tests for hessians specifically.. I'm not sure if they are useful tests.

tests/scaling.jl also tests for iteration behaviour and other float types when interpolating; but I don't think we especially want to clone those also for LinRange


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