Skip to content

Commit e547610

Browse files
jw3126timholy
authored andcommitted
fix ==(::AbstractInterpolation, ::AbstractInterpolation) (#334)
Fixes #333
1 parent 6eada59 commit e547610

File tree

9 files changed

+78
-4
lines changed

9 files changed

+78
-4
lines changed

src/Interpolations.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ abstract type AbstractInterpolation{T,N,IT<:DimSpec{InterpolationType}} <: Abstr
5050
abstract type AbstractInterpolationWrapper{T,N,ITPT,IT} <: AbstractInterpolation{T,N,IT} end
5151
abstract type AbstractExtrapolation{T,N,ITPT,IT} <: AbstractInterpolationWrapper{T,N,ITPT,IT} end
5252

53+
function Base.:(==)(itp1::AbstractInterpolation, itp2::AbstractInterpolation)
54+
propertynames(itp1) == propertynames(itp2) || return false
55+
for pname in propertynames(itp1)
56+
getproperty(itp1, pname) == getproperty(itp2, pname) || return false
57+
end
58+
return true
59+
end
60+
5361
"""
5462
BoundaryCondition
5563

src/b-splines/b-splines.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ struct BSplineInterpolation{T,N,TCoefs<:AbstractArray,IT<:DimSpec{BSpline},Axs<:
4343
parentaxes::Axs
4444
it::IT
4545
end
46+
47+
function Base.:(==)(o1::BSplineInterpolation, o2::BSplineInterpolation)
48+
o1.it == o2.it &&
49+
o1.parentaxes == o2.parentaxes &&
50+
o1.coefs == o2.coefs
51+
end
52+
4653
function BSplineInterpolation(::Type{TWeights}, A::AbstractArray{Tel,N}, it::IT, axs) where {N,Tel,TWeights<:Real,IT<:DimSpec{BSpline}}
4754
# String interpolation causes allocation, noinline avoids that unless they get called
4855
@noinline err_concrete(IT) = error("The b-spline type must be a concrete type (was $IT)")

src/extrapolation/extrapolation.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ struct Extrapolation{T,N,ITPT,IT,ET} <: AbstractExtrapolation{T,N,ITPT,IT}
33
et::ET
44
end
55

6+
function Base.:(==)(o1::Extrapolation, o2::Extrapolation)
7+
o1.et == o2.et &&
8+
o1.itp == o2.itp
9+
end
10+
611
Base.parent(A::Extrapolation) = A.itp
712
itpflag(etp::Extrapolation) = itpflag(etp.itp)
813

src/extrapolation/filled.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ mutable struct FilledExtrapolation{T,N,ITP<:AbstractInterpolation,IT,FT} <: Abst
33
fillvalue::FT
44
end
55

6+
function Base.:(==)(o1::FilledExtrapolation, o2::FilledExtrapolation)
7+
o1.fillvalue == o2.fillvalue &&
8+
o1.itp == o2.itp
9+
end
10+
611
function FilledExtrapolation(itp::AbstractInterpolation{T,N,IT}, fillvalue) where {T,N,IT}
712
Te = promote_type(T,typeof(fillvalue))
813
FilledExtrapolation{Te,N,typeof(itp),IT,typeof(fillvalue)}(itp, fillvalue)

src/gridded/gridded.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ struct GriddedInterpolation{T,N,TCoefs,IT<:DimSpec{Gridded},K<:Tuple{Vararg{Abst
1717
coefs::Array{TCoefs,N}
1818
it::IT
1919
end
20+
21+
function Base.:(==)(o1::GriddedInterpolation, o2::GriddedInterpolation)
22+
o1.it == o2.it &&
23+
o1.knots == o2.knots &&
24+
o1.coefs == o2.coefs
25+
end
26+
2027
function GriddedInterpolation(::Type{TWeights}, knots::NTuple{N,GridIndex}, A::AbstractArray{TCoefs,N}, it::IT) where {N,TCoefs,TWeights<:Real,IT<:DimSpec{Gridded},pad}
2128
isconcretetype(IT) || error("The b-spline type must be a leaf type (was $IT)")
2229
isconcretetype(TCoefs) || @warn("For performance reasons, consider using an array of a concrete type (eltype(A) == $(eltype(A)))")

src/monotonic/monotonic.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ struct MonotonicInterpolation{T, TCoeffs, TEl, TInterpolationType<:MonotonicInte
118118
d::Vector{TCoeffs} # coefficients of cubic parts of piecewise polynomials
119119
end
120120

121+
function Base.:(==)(o1::MonotonicInterpolation, o2::MonotonicInterpolation)
122+
o1.it == o2.it &&
123+
o1.knots == o2.knots &&
124+
o1.A == o2.A &&
125+
o1.m == o2.m &&
126+
o1.c == o2.c &&
127+
o1.d == o2.d
128+
end
121129

122130
size(A::MonotonicInterpolation) = size(A.knots)
123131
axes(A::MonotonicInterpolation) = axes(A.knots)

src/scaling/scaling.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ struct ScaledInterpolation{T,N,ITPT,IT,RT} <: AbstractInterpolationWrapper{T,N,I
77
ranges::RT
88
end
99

10+
function Base.:(==)(o1::ScaledInterpolation, o2::ScaledInterpolation)
11+
o1.ranges == o2.ranges &&
12+
o1.itp == o2.itp
13+
end
14+
1015
Base.parent(A::ScaledInterpolation) = A.itp
1116
count_interp_dims(::Type{<:ScaledInterpolation{T,N,ITPT}}, n) where {T,N,ITPT} = count_interp_dims(ITPT, n)
1217
BoundsCheckStyle(sitp::ScaledInterpolation) = BoundsCheckStyle(sitp.itp)

test/core.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
11
using Interpolations, Test
22

3+
@testset "==" begin
4+
5+
# issue #333
6+
knots = ([1,1.2],)
7+
vals = [1.0, 2.0]
8+
scheme = Gridded(Linear())
9+
itp = interpolate(knots, vals , scheme)
10+
@test itp == itp
11+
@test itp == deepcopy(itp)
12+
@test itp != interpolate(([1,1.3],), vals, scheme)
13+
@test itp != interpolate(knots, [1.0, 3.0], scheme)
14+
@test itp != interpolate(knots, vals, Gridded(Constant()))
15+
16+
scheme = BSpline(Quadratic(Reflect(OnCell())))
17+
vals = [1.0, 2.0]
18+
itp = interpolate(vals, scheme)
19+
@test itp == itp
20+
@test itp == deepcopy(itp)
21+
@test itp != interpolate(vals, BSpline(Linear()))
22+
@test itp != interpolate(randn(2), scheme)
23+
24+
vals = rand(Float64, 2,3)
25+
itp1 = interpolate(vals, BSpline(Quadratic(Flat(OnGrid()))))
26+
itp2 = interpolate(Float64.(vals), BSpline(Quadratic(Flat(OnGrid()))))
27+
@test itp1 == itp2
28+
end
29+
330
@testset "Core" begin
431
A = reshape([0], 1, 1, 1, 1, 1)
532
wis = ntuple(d->Interpolations.WeightedAdjIndex(1, (1,)), ndims(A))

test/io.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@ using Test
4646
itp = interpolate((range(0.0, stop=0.3, length=4),), A, Gridded(Linear()))
4747
io = IOBuffer()
4848
show(io, MIME("text/plain"), itp)
49-
str = String(take!(io))
50-
@test str == "4-element interpolate((0.0:0.1:0.3,), ::Array{Float64,1}, Gridded(Linear())) with element type Float64:\n 0.25\n 0.5 \n 0.75\n 1.0 "
49+
str1 = String(take!(io))
50+
str2 = "4-element interpolate((0.0:0.1:0.3,), ::Array{Float64,1}, Gridded(Linear())) with element type Float64:\n 0.25\n 0.5 \n 0.75\n 1.0"
51+
@test filter(!isspace, str1) == filter(!isspace, str2)
5152
io2 = IOBuffer()
5253
show(io2, itp)
53-
str2 = String(take!(io2))
54-
@test str2 == "4-element interpolate((0.0:0.1:0.3,), ::Array{Float64,1}, Gridded(Linear())) with element type Float64:\n 0.25\n 0.5 \n 0.75\n 1.0 "
54+
str1 = String(take!(io2))
55+
str2 = "4-element interpolate((0.0:0.1:0.3,), ::Array{Float64,1}, Gridded(Linear())) with element type Float64:\n 0.25\n 0.5 \n 0.75\n 1.0"
56+
@test filter(!isspace, str1) == filter(!isspace, str2)
5557
end
5658

5759
@testset "scaled" begin

0 commit comments

Comments
 (0)