Skip to content

Commit 9869e54

Browse files
authored
Fixed errors in cumul_integrate and included a new test (#28)
1 parent 17f28a8 commit 9869e54

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/NumericalIntegration.jl

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function integrate(x,y...) end
4242
4343
Compute cumulative numerical integral of y(x) from x=x[1] to x=x[end]. Return a vector with elements of the same type as the input. If no method is supplied, use Trapezdoial.
4444
"""
45-
function cumul_integrate(x,y...) end
45+
function cumul_integrate end
4646

4747
#implementation
4848

@@ -256,9 +256,8 @@ end
256256
Use Trapezoidal rule. This is the default when no method is supplied.
257257
"""
258258
function cumul_integrate(x::AbstractVector, y::AbstractVector, ::Trapezoidal)
259-
n = length(x)
260-
@assert length(y) == n "x and y vectors must be of the same length!"
261-
n 2 || error("vectors must contain at least two elements")
259+
@assert length(x) == length(y) "x and y vectors must be of the same length!"
260+
length(x) 2 || error("vectors must contain at least two elements")
262261

263262
return cumul_integrate(x, y, TrapezoidalFast())
264263
end
@@ -273,7 +272,7 @@ Use Trapezoidal rule, assuming evenly spaced vector x.
273272
"""
274273
function cumul_integrate(x::AbstractVector, y::AbstractVector, ::TrapezoidalEven)
275274
@assert length(x) == length(y) "x and y vectors must be of the same length!"
276-
n 2 || error("vectors must contain at least two elements")
275+
length(x) 2 || error("vectors must contain at least two elements")
277276

278277
return cumul_integrate(x, y, TrapezoidalEvenFast())
279278
end
@@ -286,7 +285,8 @@ Use Trapezoidal rule. Unsafe method: no bound checking.
286285
function cumul_integrate(x::AbstractVector, y::AbstractVector, ::TrapezoidalFast)
287286
# compute initial value
288287
@inbounds init = (x[2] - x[1]) * (y[1] + y[2])
289-
retarr[i] = Vector{typeof(init)}(undef, n)
288+
n = length(x)
289+
retarr = Vector{typeof(init)}(undef, n)
290290
retarr[1] = init
291291

292292
# for all other values
@@ -303,7 +303,12 @@ end
303303
Use Trapezoidal rule, assuming evenly spaced vector x. Unsafe method: no bound checking.
304304
"""
305305
function cumul_integrate(x::AbstractVector, y::AbstractVector, ::TrapezoidalEvenFast)
306-
retarr = _zeros(x,y)
306+
@inbounds init = y[1] + y[2]
307+
n = length(x)
308+
retarr = Vector{typeof(init)}(undef, n)
309+
retarr[1] = init
310+
311+
# for all other values
307312
@fastmath for i in 2 : length(y)
308313
@inbounds retarr[i] = retarr[i-1] + (y[i] + y[i-1])
309314
end

test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@ using HCubature # for testing n-dimensional integration
2323
end
2424
end
2525

26+
@testset "cumulative integration" begin
27+
x = collect(-π : 2*π/2048 : π)
28+
y = sin.(x)
29+
exact = @. -cos(x) + cos(-π)
30+
for M in subtypes(IntegrationMethod)
31+
hasmethod(cumul_integrate, Tuple{AbstractVector, AbstractVector, M}) || continue
32+
for T in [Float32, Float64, BigFloat]
33+
result = @inferred cumul_integrate(T.(x), T.(y), M())
34+
@test typeof(result) == Vector{T}
35+
@test all(isapprox.(result, exact, atol=1e-4))
36+
end
37+
end
38+
end
39+
2640
@testset "n-dimensional integration testing" begin
2741
X = range(0,stop=2π,length=10)
2842
Y = range(-π,stop=π,length=10)

0 commit comments

Comments
 (0)