Skip to content

Commit 4c21605

Browse files
authored
Merge pull request #468 from mateuszbaran/mbaran/safer-knot-deduplication
Safer knot deduplication
2 parents eefe448 + 6349308 commit 4c21605

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/gridded/gridded.jl

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@ check_gridded(::Any, ::Tuple{}, ::Tuple{}) = nothing
8686
degree(flag::Gridded) = flag.degree
8787

8888
"""
89-
Interpolations.deduplicate_knots!(knots)
89+
Interpolations.deduplicate_knots!(knots; move_knots = false)
9090
9191
Makes knots unique by incrementing repeated but otherwise sorted knots using `nextfloat`.
92+
If keyword `move_knots` is true, then `nextfloat` will be applied successively until knots
93+
are unique. Otherwise, a warning will be issued.
9294
9395
# Example
9496
@@ -106,16 +108,28 @@ degree(flag::Gridded) = flag.degree
106108
0.0
107109
20.0
108110
20.000000000000004
111+
112+
julia> Interpolations.deduplicate_knots!([1.0, 1.0, 1.0, nextfloat(1.0), nextfloat(1.0)]; move_knots = true)
113+
5-element Vector{Float64}:
114+
1.0
115+
1.0000000000000002
116+
1.0000000000000004
117+
1.0000000000000007
118+
1.0000000000000009
109119
```
110120
"""
111-
function deduplicate_knots!(knots)
121+
function deduplicate_knots!(knots; move_knots::Bool = false)
112122
last_knot = first(knots)
113123
for i = eachindex(knots)
114124
if i == 1
115125
continue
116126
end
117-
if knots[i] == last_knot
127+
if knots[i] == last_knot || (move_knots && (@inbounds knots[i] <= knots[i-1]))
128+
@inbounds knots[i] = nextfloat(knots[i-1])
129+
elseif @inbounds knots[i] <= knots[i-1]
130+
@warn "Successive repeated knots detected. Consider using `move_knots` keyword to Interpolations.deduplicate_knots!" last_knot knots[i-1] knots[i]
118131
@inbounds knots[i] = nextfloat(knots[i-1])
132+
move_knots = true
119133
else
120134
last_knot = @inbounds knots[i]
121135
end

test/gridded/gridded.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,10 @@ using Interpolations, Test
9595
# https://github.com/JuliaMath/Interpolations.jl/commit/318ebc88ca1fc084754f3c741266537f901a3310
9696
knots_not_unique_warning = "Duplicated knots were deduplicated. Use Interpolations.deduplicate_knots!(knots) explicitly to avoid this warning."
9797
@test_logs (:warn, knots_not_unique_warning) interpolate(knots, [1.0, 1.1, 2.0], Gridded(Linear()))
98+
99+
# knot deduplication, issue #467, PR #468
100+
duplicated_knots = [1.0, 1.0, 1.0, nextfloat(1.0), nextfloat(1.0)]
101+
successive_knots_warning = "Successive repeated knots detected. Consider using `move_knots` keyword to Interpolations.deduplicate_knots!"
102+
@test_logs (:warn, successive_knots_warning) Interpolations.deduplicate_knots!(duplicated_knots)
103+
@test allunique(duplicated_knots)
98104
end

0 commit comments

Comments
 (0)