Skip to content

Commit

Permalink
Merge pull request #169 from ffreyer/ff/adapted_grid
Browse files Browse the repository at this point in the history
Use StableRNGs for `adapted_grid()`
  • Loading branch information
SimonDanisch authored Oct 16, 2024
2 parents 45ee45e + 39d2e34 commit 9421008
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
6 changes: 4 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ version = "1.4.1"
ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4"
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[compat]
ColorSchemes = "3.19 - 4"
Colors = "0.12"
Dates = "<0.0.1, 1"
PrecompileTools = "1"
Printf = "<0.0.1, 1"
Random = "<0.0.1, 1"
Reexport = "1"
PrecompileTools = "1"
StableRNGs = "1"
Statistics = "<0.0.1, 1"
julia = "1.6"

Expand Down
2 changes: 1 addition & 1 deletion src/PlotUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using Dates

@reexport using Colors
import Base: getindex
import Random: MersenneTwister
import StableRNGs: StableRNG

export ColorGradient,
ColorPalette,
Expand Down
4 changes: 2 additions & 2 deletions src/adapted_grid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function adapted_grid(
minmax::Tuple{Number,Number};
max_recursions = 7,
max_curvature = 0.01,
n_points = 31,
n_points = 31
)
if minmax[1] > minmax[2]
throw(ArgumentError("interval must be given as (min, max)"))
Expand All @@ -34,7 +34,7 @@ function adapted_grid(
xs[end - 1] = xs[end] - (xs[end] - xs[end - 1]) / 4

# Wiggle interior points a bit to prevent aliasing and other degenerate cases
rng = MersenneTwister(1337)
rng = StableRNG(1337)
rand_factor = 0.05
for i 2:(length(xs) - 1)
xs[i] += 2rand_factor * (rand(rng) - 0.5) * (xs[i + 1] - xs[i - 1])
Expand Down
12 changes: 9 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,15 @@ end
@test fs |> extrema |> collect |> diff |> first > 1.9
end

let f = sinc, int = (0, 40) # JuliaPlots/Plots.jl/issues/3894
xs, fs = adapted_grid(f, int)
@test length(xs) > 400
let f = sinc, int = (-40, 40) # JuliaPlots/Plots.jl/issues/3894
xs, fs = adapted_grid(sinc, int)
roots = vcat(int[1]:-1, 1:int[2])
count_per_extrema = map(1:length(roots)-1) do idx
left = roots[idx]; right = roots[idx+1]
return count(x -> left < x < right, xs)
end
# check that we have at least 5 points for each extrema
@test all(count_per_extrema .>= 5)
end
end

Expand Down

0 comments on commit 9421008

Please sign in to comment.