Skip to content

Commit

Permalink
Merge pull request #468 from mateuszbaran/mbaran/safer-knot-deduplica…
Browse files Browse the repository at this point in the history
…tion

Safer knot deduplication
  • Loading branch information
mkitti authored Dec 14, 2021
2 parents eefe448 + 6349308 commit 4c21605
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/gridded/gridded.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ check_gridded(::Any, ::Tuple{}, ::Tuple{}) = nothing
degree(flag::Gridded) = flag.degree

"""
Interpolations.deduplicate_knots!(knots)
Interpolations.deduplicate_knots!(knots; move_knots = false)
Makes knots unique by incrementing repeated but otherwise sorted knots using `nextfloat`.
If keyword `move_knots` is true, then `nextfloat` will be applied successively until knots
are unique. Otherwise, a warning will be issued.
# Example
Expand All @@ -106,16 +108,28 @@ degree(flag::Gridded) = flag.degree
0.0
20.0
20.000000000000004
julia> Interpolations.deduplicate_knots!([1.0, 1.0, 1.0, nextfloat(1.0), nextfloat(1.0)]; move_knots = true)
5-element Vector{Float64}:
1.0
1.0000000000000002
1.0000000000000004
1.0000000000000007
1.0000000000000009
```
"""
function deduplicate_knots!(knots)
function deduplicate_knots!(knots; move_knots::Bool = false)
last_knot = first(knots)
for i = eachindex(knots)
if i == 1
continue
end
if knots[i] == last_knot
if knots[i] == last_knot || (move_knots && (@inbounds knots[i] <= knots[i-1]))
@inbounds knots[i] = nextfloat(knots[i-1])
elseif @inbounds knots[i] <= knots[i-1]
@warn "Successive repeated knots detected. Consider using `move_knots` keyword to Interpolations.deduplicate_knots!" last_knot knots[i-1] knots[i]
@inbounds knots[i] = nextfloat(knots[i-1])
move_knots = true
else
last_knot = @inbounds knots[i]
end
Expand Down
6 changes: 6 additions & 0 deletions test/gridded/gridded.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,10 @@ using Interpolations, Test
# https://github.com/JuliaMath/Interpolations.jl/commit/318ebc88ca1fc084754f3c741266537f901a3310
knots_not_unique_warning = "Duplicated knots were deduplicated. Use Interpolations.deduplicate_knots!(knots) explicitly to avoid this warning."
@test_logs (:warn, knots_not_unique_warning) interpolate(knots, [1.0, 1.1, 2.0], Gridded(Linear()))

# knot deduplication, issue #467, PR #468
duplicated_knots = [1.0, 1.0, 1.0, nextfloat(1.0), nextfloat(1.0)]
successive_knots_warning = "Successive repeated knots detected. Consider using `move_knots` keyword to Interpolations.deduplicate_knots!"
@test_logs (:warn, successive_knots_warning) Interpolations.deduplicate_knots!(duplicated_knots)
@test allunique(duplicated_knots)
end

2 comments on commit 4c21605

@mkitti
Copy link
Collaborator Author

@mkitti mkitti commented on 4c21605 Dec 14, 2021

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/50511

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.13.5 -m "<description of version>" 4c2160597451fe5c5a44f9ace7bc3075e861258e
git push origin v0.13.5

Please sign in to comment.