Skip to content

Commit 35eb371

Browse files
authored
Fix scaled interpolation for Unitful ranges. (#490)
1 parent fc3d8d2 commit 35eb371

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

src/scaling/scaling.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ coordslookup(::Any, ::Tuple{}, ::Tuple{}) = ()
9999
coordlookup(::NoInterp, r, i) = i
100100
coordlookup(::Flag, r, x) = coordlookup(r, x)
101101

102-
coordlookup(r::AbstractUnitRange, x) = x - first(r) + oneunit(eltype(r))
102+
coordlookup(r::AbstractUnitRange, x) = (x - first(r))/oneunit(eltype(r)) + one(eltype(r))
103103
# coordlookup(i::Bool, r::AbstractRange, x) = i ? coordlookup(r, x) : convert(typeof(coordlookup(r,x)), x)
104-
coordlookup(r::StepRange, x) = (x - r.start) / r.step + oneunit(eltype(r))
104+
coordlookup(r::StepRange, x) = (x - r.start) / r.step + one(eltype(r))
105105

106-
coordlookup(r::AbstractRange, x) = (x - first(r)) / step(r) + oneunit(eltype(r))
106+
coordlookup(r::AbstractRange, x) = (x - first(r)) / step(r) + one(eltype(r))
107107
boundstep(r::AbstractRange) = 0.5*step(r)
108108
rescale_gradient(r::AbstractRange, g) = g / step(r)
109109

test/scaling/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ include("scaling.jl")
22
include("dimspecs.jl")
33
include("nointerp.jl")
44
include("withextrap.jl")
5+
include("unitful.jl")

test/scaling/unitful.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Unitful
2+
3+
@testset "Unitful scaled interpolation" begin
4+
# Abstract range
5+
Xs = (0:.2:5)u"m"
6+
Ys = Xs .^ 2
7+
sitp = scale(interpolate(Ys, BSpline(Quadratic(Free(OnGrid())))), Xs)
8+
@test sitp.((0:1:3)u"cm") ((0:1:3)u"cm") .^ 2
9+
10+
# Step Range
11+
Xss = (0:5)u"m"
12+
Yss = Float64.(Xss .^ 2)
13+
sitp = scale(interpolate(Yss, BSpline(Quadratic(Free(OnGrid())))), Xss)
14+
@test sitp((0:1:3)u"cm") ((0:1:3)u"cm") .^ 2
15+
16+
end

0 commit comments

Comments
 (0)