Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions src/simpsons.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,29 @@ struct SimpsonUniformWeights{T} <: UniformWeights
end

@inline function Base.getindex(w::SimpsonUniformWeights, i)
# evenly spaced simpson's 1/3, 3/8 rule
h = w.h
n = w.n
(i == 1 || i == n) && return 17h / 48
(i == 2 || i == n - 1) && return 59h / 48
(i == 3 || i == n - 2) && return 43h / 48
(i == 4 || i == n - 3) && return 49h / 48
return h
if n >= 8
# evenly spaced simpson's 1/3, 3/8 rule
(i == 1 || i == n) && return 17h / 48
(i == 2 || i == n - 1) && return 59h / 48
(i == 3 || i == n - 2) && return 43h / 48
(i == 4 || i == n - 3) && return 49h / 48
return h
elseif isodd(n)
(i == 1 || i == n) && return h / 3
return iseven(i) ? 4h / 3 : 2h / 3
else
if i <= 4
i == 1 && return 3h / 8
i == 4 && return n == 4 ? 3h / 8 : (3h / 8 + h / 3)
return 9h / 8
else
i == n && return h / 3
s = i - 3
return iseven(s) ? 4h / 3 : 2h / 3
end
end
end

"""
Expand Down Expand Up @@ -82,6 +97,10 @@ end
end

function find_weights(x::AbstractVector, ::SimpsonsRule)
x isa AbstractRange && return SimpsonUniformWeights(length(x), step(x))
if x isa AbstractRange
length(x) < 3 &&
throw(ArgumentError("The length of the grid must exceed 2 for simpsons rule."))
return SimpsonUniformWeights(length(x), step(x))
end
return SimpsonNonuniformWeights(x)
end
36 changes: 36 additions & 0 deletions test/sampled_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,28 @@ using Integrals, Test, Unitful
end
end
end

# Uniform-grid SimpsonsRule end corrections overlap for n < 8 without small-n weights.
@testset "SimpsonsRule small uniform grids" begin
for T in (Float64, Float32, BigFloat)
for n in 3:10
x = range(zero(T), one(T); length = n)
xv = collect(x)
for y in (ones(T, n), xv .^ 2)
u_range = solve(SampledIntegralProblem(y, x), SimpsonsRule()).u
u_vec = solve(SampledIntegralProblem(y, xv), SimpsonsRule()).u
@test u_range ≈ u_vec
end
@test solve(SampledIntegralProblem(ones(T, n), x), SimpsonsRule()).u ≈ one(T)
@test solve(SampledIntegralProblem(xv .^ 2, x), SimpsonsRule()).u ≈ one(T) / 3
@test solve(SampledIntegralProblem(xv .^ 3, x), SimpsonsRule()).u ≈ one(T) / 4
end
end
x = range(0, 1; length = 5)
@inferred Integrals.find_weights(x, SimpsonsRule())
@test Integrals.find_weights(x, SimpsonsRule()) isa Integrals.SimpsonUniformWeights
@test_throws ArgumentError Integrals.find_weights(range(0, 1; length = 2), SimpsonsRule())
end
end

@testset "Sampled integration with dimensional axes" begin
Expand Down Expand Up @@ -80,4 +102,18 @@ end
sol2 = solve!(cache)

@test sol2 == solve(SampledIntegralProblem(y, x, dim = 1), alg)

@testset "SimpsonsRule cache reuse across n = 8" begin
alg = SimpsonsRule()
for (n1, n2) in ((5, 11), (11, 5))
x1 = range(0, 1; length = n1)
cache = init(SampledIntegralProblem(ones(n1), x1), alg)
@test solve!(cache).u ≈ 1
x2 = range(0, 1; length = n2)
cache.x = x2
cache.y = ones(n2)
@test solve!(cache).u ≈ 1
@test solve!(cache).u == solve(SampledIntegralProblem(ones(n2), x2), alg).u
end
end
end
Loading