diff --git a/src/sampled.jl b/src/sampled.jl index b2e85c70..624d7bbb 100644 --- a/src/sampled.jl +++ b/src/sampled.jl @@ -54,8 +54,14 @@ function evalrule(data::AbstractVector, weights, dim) n = length(data) n == 0 && throw(ArgumentError("No points to integrate")) out = data[1] * weights[1] - @inbounds for i in 2:n - out += weights[i] * data[i] + if weights isa TrapezoidalUniformWeights + @inbounds @simd for i in 2:n + out += weights[i] * data[i] + end + else + @inbounds for i in 2:n + out += weights[i] * data[i] + end end return out else diff --git a/test/sampled_tests.jl b/test/sampled_tests.jl index 1f8e26ee..8b78b82d 100644 --- a/test/sampled_tests.jl +++ b/test/sampled_tests.jl @@ -32,6 +32,28 @@ using Integrals, Test, Unitful end end +@testset "1-D sampled reduction matches sequential reference sum" begin + for N in (8, 100, 1000, 10_000) + x = range(0.0, 1.0; length = N) + xv = collect(x) + y = sin.(x) .+ 0.1 .* collect(x) + for (grid, alg) in ( + (x, TrapezoidalRule()), + (xv, TrapezoidalRule()), + (x, SimpsonsRule()), + (xv, SimpsonsRule()), + ) + cache = init(SampledIntegralProblem(y, grid), alg) + w = cache.cacheval + ref = w[1] * y[1] + @inbounds for i in 2:length(y) + ref += w[i] * y[i] + end + @test solve!(cache).u ≈ ref rtol = 1.0e-12 + end + end +end + @testset "Sampled integration with dimensional axes" begin data = ones(2, 3) axis = [0.0, 1.0, 2.0]u"m"