From 390cb70cb3c5ce4f24d0dbf6e9750edfabfc083f Mon Sep 17 00:00:00 2001 From: ChrisRackauckas-Claude Date: Fri, 9 Oct 2026 18:13:36 -0400 Subject: [PATCH 1/2] Fix SimpsonsRule on uniform grids with fewer than 8 points. The 17/59/43/49 end corrections overlap for n < 8, so fall back to the nonuniform composite weights on small AbstractRange grids. Co-Authored-By: Chris Rackauckas Co-Authored-By: Cursor Agent Agent-Harness: Cursor Agent 2026.10.01-14929f9 Agent-Model: unknown (Cursor auto) Agent-Session: local session, transcript /home/crackauc/sandbox/goals/performance/jobs/integrals/int-simpson/log.txt on amdci2 --- src/simpsons.jl | 4 +++- test/sampled_tests.jl | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/simpsons.jl b/src/simpsons.jl index 4ba56410..d4982946 100644 --- a/src/simpsons.jl +++ b/src/simpsons.jl @@ -82,6 +82,8 @@ end end function find_weights(x::AbstractVector, ::SimpsonsRule) - x isa AbstractRange && return SimpsonUniformWeights(length(x), step(x)) + if x isa AbstractRange && length(x) >= 8 + return SimpsonUniformWeights(length(x), step(x)) + end return SimpsonNonuniformWeights(x) end diff --git a/test/sampled_tests.jl b/test/sampled_tests.jl index 1f8e26ee..e6d1919f 100644 --- a/test/sampled_tests.jl +++ b/test/sampled_tests.jl @@ -30,6 +30,21 @@ using Integrals, Test, Unitful end end end + + # Uniform-grid SimpsonsRule end corrections overlap for n < 8; compare to Vector path. + @testset "SimpsonsRule small uniform grids" begin + for n in 3:10 + x = range(0, 1; length = n) + xv = collect(x) + for y in (ones(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(n), x), SimpsonsRule()).u ≈ 1 + @test solve(SampledIntegralProblem(xv .^ 2, x), SimpsonsRule()).u ≈ 1 / 3 + end + end end @testset "Sampled integration with dimensional axes" begin From 42e98d447f1910ed3eb03105256a2cd1aef8b657 Mon Sep 17 00:00:00 2001 From: ChrisRackauckas-Claude Date: Sat, 10 Oct 2026 17:38:16 -0400 Subject: [PATCH 2/2] Keep SimpsonUniformWeights for all AbstractRange lengths. Returning a different weight type for n < 8 broke SampledIntegralCache reuse when the grid crossed n = 8. Small-n composite 1/3 and 3/8 weights now live in SimpsonUniformWeights.getindex so the cache type is stable. Co-Authored-By: Chris Rackauckas Co-Authored-By: Cursor Agent Agent-Harness: Cursor Agent 2026.10.01-14929f9 Agent-Model: unknown (Cursor auto) Agent-Session: local session, transcript /home/crackauc/sandbox/goals/performance/jobs/integrals/int-simpson/log.txt on amdci2 --- src/simpsons.jl | 31 ++++++++++++++++++++++++------- test/sampled_tests.jl | 41 +++++++++++++++++++++++++++++++---------- 2 files changed, 55 insertions(+), 17 deletions(-) diff --git a/src/simpsons.jl b/src/simpsons.jl index d4982946..8cb3d811 100644 --- a/src/simpsons.jl +++ b/src/simpsons.jl @@ -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 """ @@ -82,7 +97,9 @@ end end function find_weights(x::AbstractVector, ::SimpsonsRule) - if x isa AbstractRange && length(x) >= 8 + 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) diff --git a/test/sampled_tests.jl b/test/sampled_tests.jl index e6d1919f..6f97659a 100644 --- a/test/sampled_tests.jl +++ b/test/sampled_tests.jl @@ -31,19 +31,26 @@ using Integrals, Test, Unitful end end - # Uniform-grid SimpsonsRule end corrections overlap for n < 8; compare to Vector path. + # Uniform-grid SimpsonsRule end corrections overlap for n < 8 without small-n weights. @testset "SimpsonsRule small uniform grids" begin - for n in 3:10 - x = range(0, 1; length = n) - xv = collect(x) - for y in (ones(n), xv .^ 2) - u_range = solve(SampledIntegralProblem(y, x), SimpsonsRule()).u - u_vec = solve(SampledIntegralProblem(y, xv), SimpsonsRule()).u - @test u_range ≈ u_vec + 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 - @test solve(SampledIntegralProblem(ones(n), x), SimpsonsRule()).u ≈ 1 - @test solve(SampledIntegralProblem(xv .^ 2, x), SimpsonsRule()).u ≈ 1 / 3 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 @@ -95,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