Skip to content

Commit c39942b

Browse files
fgasdiafgasdia
andauthored
Fix final accuracy estimate in RombergEven method (#24)
* Fix final error check in RombergEven. * Change variable names (`row`->`col`) to reflect usage * Fix type instability of `c` * Remove unnecessary type notation of `maxsteps` * Update "Raising Warnings" test set * switch initialization of c to eltype(y) for consistency Co-authored-by: fgasdia <[email protected]>
1 parent 080deb0 commit c39942b

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

src/NumericalIntegration.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -147,35 +147,35 @@ end
147147
function integrate(x::AbstractVector, y::AbstractVector, m::RombergEven)
148148
@assert length(x) == length(y) "x and y vectors must be of the same length!"
149149
@assert ((length(x) - 1) & (length(x) - 2)) == 0 "Need length of vector to be 2^n + 1"
150-
maxsteps::Integer = Int(log2(length(x)-1))
150+
maxsteps = Int(log2(length(x)-1))
151151
rombaux = zeros(eltype(y), maxsteps, 2)
152-
prevrow = 1
153-
currrow = 2
152+
prevcol = 1
153+
currcol = 2
154154
@inbounds h = x[end] - x[1]
155-
@inbounds rombaux[prevrow, 1] = (y[1] + y[end])*h*HALF
155+
@inbounds rombaux[1, 1] = (y[1] + y[end])*h*HALF
156156
@inbounds for i in 1 : (maxsteps-1)
157157
h *= HALF
158158
npoints = 1 << (i-1)
159159
jumpsize = div(length(x)-1, 2*npoints)
160-
c = 0.0
160+
c = zero(eltype(y))
161161
for j in 1 : npoints
162162
c += y[1 + (2*j-1)*jumpsize]
163163
end
164-
rombaux[1, currrow] = h*c + HALF*rombaux[1, prevrow]
164+
rombaux[1, currcol] = h*c + HALF*rombaux[1, prevcol]
165165
for j in 2 : (i+1)
166166
n_k = 4^(j-1)
167-
rombaux[j, currrow] = (n_k*rombaux[j-1, currrow] - rombaux[j-1, prevrow])/(n_k - 1)
167+
rombaux[j, currcol] = (n_k*rombaux[j-1, currcol] - rombaux[j-1, prevcol])/(n_k - 1)
168168
end
169169

170-
if i > maxsteps//3 && norm(rombaux[i, prevrow] - rombaux[i+1, currrow], Inf) < m.acc
171-
return rombaux[i+1, currrow]
170+
if i > maxsteps//3 && norm(rombaux[i, prevcol] - rombaux[i+1, currcol], Inf) < m.acc
171+
return rombaux[i+1, currcol]
172172
end
173173

174-
prevrow, currrow = currrow, prevrow
174+
prevcol, currcol = currcol, prevcol
175175
end
176-
finalerr = norm(rombaux[maxsteps-1, prevrow] - rombaux[maxsteps, currrow], Inf)
176+
finalerr = norm(rombaux[maxsteps-1, currcol] - rombaux[maxsteps, prevcol], Inf)
177177
@warn "RombergEven :: final step reached, but accuracy not: $finalerr > $(m.acc)"
178-
@inbounds return rombaux[maxsteps, prevrow]
178+
@inbounds return rombaux[maxsteps, prevcol]
179179
end
180180

181181

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@ end
6060
xs = collect(-1.0 : 0.5 : 1.0)
6161
ys = xs.^2
6262
m = RombergEven()
63-
expwarn = "RombergEven :: final step reached, but accuracy not: 1.0 > 1.0e-12"
63+
expwarn = "RombergEven :: final step reached, but accuracy not: 1.3333333333333335 > 1.0e-12"
6464
@test_logs (:warn, expwarn) integrate(xs, ys, m)
6565
end

0 commit comments

Comments
 (0)