Skip to content

Commit 3ffce0d

Browse files
committed
Extend extrapolation methods to allow for arbitrary arrays.
1 parent 7da27cb commit 3ffce0d

File tree

3 files changed

+29
-45
lines changed

3 files changed

+29
-45
lines changed

src/extrapolation/aitken_neville.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ where
1111
* `ti`: interpolation nodes
1212
* `xi`: interpolation values
1313
"""
14-
function aitken_neville!(x::AbstractVector, t::TT, ti::AbstractVector{TT}, xi::AbstractMatrix) where {TT}
15-
@assert length(ti) == size(xi,2)
16-
@assert length(x) == size(xi,1)
14+
function aitken_neville!(x::AbstractArray, t::TT, ti::AbstractVector{TT}, xi::AbstractVector) where {TT}
15+
@assert length(ti) == length(xi)
16+
17+
for _xi in xi
18+
@assert axes(x) == axes(_xi)
19+
end
1720

1821
for j in eachindex(ti)
19-
for i in 1:(length(ti)-j)
20-
for k in axes(x,1)
21-
xi[k,i] = xi[k,i+1] + (xi[k,i] - xi[k,i+1]) * (ti[i+j] - t) / (ti[i+j] - ti[i])
22-
end
22+
for i in eachindex(ti)[begin:end-j]
23+
@. xi[i] = xi[i+1] + (xi[i] - xi[i+1]) * (ti[i+j] - t) / (ti[i+j] - ti[i])
2324
end
2425
end
25-
for k in eachindex(x)
26-
x[k] = xi[k,1]
27-
end
26+
27+
copyto!(x, xi[1])
2828
end

src/extrapolation/euler.jl

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,24 @@ struct EulerExtrapolation <: Extrapolation
3434
end
3535

3636

37-
function extrapolate!(t₀::TT, x₀::AbstractVector{DT},
38-
t₁::TT, x₁::AbstractVector{DT},
37+
function extrapolate!(t₀::TT, x₀::AbstractArray{DT},
38+
t₁::TT, x₁::AbstractArray{DT},
3939
problem::AbstractProblemODE,
4040
extrap::EulerExtrapolation) where {DT,TT}
4141

4242
@assert axes(x₀) == axes(x₁)
4343

4444
local F = collect(1:(extrap.s+1))
4545
local σ = (t₁ - t₀) ./ F
46-
local pts = repeat(x₀, outer = [1, extrap.s+1])
46+
local pts = [copy(x₀) for _ in F]
4747

48-
local xᵢ = zero(x₀)
4948
local vᵢ = zero(x₀)
5049

5150
for i in F
5251
for _ in 1:(F[i]-1)
53-
tᵢ = t₀ + σ[i]
54-
for k in axes(pts,1)
55-
xᵢ[k] = pts[k,i]
56-
end
57-
initialguess(problem).v(vᵢ, tᵢ, xᵢ, parameters(problem))
58-
for k in axes(pts,1)
59-
pts[k,i] += σ[i] * vᵢ[k]
60-
end
52+
tᵢ = t₀ + σ[i]
53+
initialguess(problem).v(vᵢ, tᵢ, pts[i], parameters(problem))
54+
pts[i] .+= σ[i] * vᵢ
6155
end
6256
end
6357

src/extrapolation/midpoint.jl

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ end
9292

9393

9494
function extrapolate!(
95-
t₀::TT, x₀::AbstractVector{DT},
96-
t₁::TT, x₁::AbstractVector{DT},
95+
t₀::TT, x₀::AbstractArray{DT},
96+
t₁::TT, x₁::AbstractArray{DT},
9797
problem::Union{AbstractProblemODE, SODEProblem},
9898
extrap::MidpointExtrapolation) where {DT,TT}
9999

@@ -102,7 +102,7 @@ function extrapolate!(
102102
local F = [2i*one(TT) for i in 1:extrap.s+1]
103103
local σ = (t₁ - t₀) ./ F
104104
local σ² = σ.^2
105-
local pts = zeros(DT, axes(x₀)..., extrap.s+1)
105+
local pts = [zero(x₀) for _ in 1:extrap.s+1]
106106

107107
local xᵢ₁ = zero(x₀)
108108
local xᵢ₂ = zero(x₀)
@@ -112,7 +112,7 @@ function extrapolate!(
112112

113113
initialguess(problem).v(v₀, t₀, x₀, parameters(problem))
114114

115-
for i in 1:extrap.s+1
115+
for i in eachindex(pts)
116116
tᵢ = t₀ + σ[i]
117117
xᵢ₁ .= x₀
118118
xᵢ₂ .= x₀ .+ σ[i] .* v₀
@@ -122,9 +122,7 @@ function extrapolate!(
122122
xᵢ₁ .= xᵢ₂
123123
xᵢ₂ .= xᵢₜ
124124
end
125-
for k in axes(pts,1)
126-
pts[k,i] += xᵢ₂[k]
127-
end
125+
pts[i] .+= xᵢ₂
128126
end
129127

130128
aitken_neville!(x₁, zero(TT), σ², pts)
@@ -150,8 +148,8 @@ function extrapolate!(t₀::TT, q₀::AbstractVector{DT}, p₀::AbstractVector{D
150148
local σ = (t₁ - t₀) ./ F
151149
local σ2 = σ.^2
152150

153-
local qts = zeros(DT, axes(q₀)..., extrap.s+1)
154-
local pts = zeros(DT, axes(p₀)..., extrap.s+1)
151+
local qts = [zero(q₀) for _ in 1:extrap.s+1]
152+
local pts = [zero(p₀) for _ in 1:extrap.s+1]
155153

156154
local qᵢ₁= zero(q₀)
157155
local qᵢ₂= zero(q₀)
@@ -186,12 +184,8 @@ function extrapolate!(t₀::TT, q₀::AbstractVector{DT}, p₀::AbstractVector{D
186184
pᵢ₁ .= pᵢ₂
187185
pᵢ₂ .= pᵢₜ
188186
end
189-
for k in axes(qts,1)
190-
qts[k,i] += qᵢ₂[k]
191-
end
192-
for k in axes(pts,1)
193-
pts[k,i] += pᵢ₂[k]
194-
end
187+
qts[i] .+= qᵢ₂
188+
pts[i] .+= pᵢ₂
195189
end
196190

197191
aitken_neville!(q₁, zero(TT), σ2, qts)
@@ -219,8 +213,8 @@ function extrapolate!(
219213
local σ = (t₁ - t₀) ./ F
220214
local σ2 = σ.^2
221215

222-
local qts = zeros(DT, axes(q₀)..., extrap.s+1)
223-
local pts = zeros(DT, axes(p₀)..., extrap.s+1)
216+
local qts = [zero(q₀) for _ in 1:extrap.s+1]
217+
local pts = [zero(p₀) for _ in 1:extrap.s+1]
224218

225219
local qᵢ₁= zero(q₀)
226220
local qᵢ₂= zero(q₀)
@@ -255,12 +249,8 @@ function extrapolate!(
255249
pᵢ₁ .= pᵢ₂
256250
pᵢ₂ .= pᵢₜ
257251
end
258-
for k in axes(qts,1)
259-
qts[k,i] += qᵢ₂[k]
260-
end
261-
for k in axes(pts,1)
262-
pts[k,i] += pᵢ₂[k]
263-
end
252+
qts[i] .+= qᵢ₂
253+
pts[i] .+= pᵢ₂
264254
end
265255

266256
aitken_neville!(q₁, zero(TT), σ2, qts)

0 commit comments

Comments
 (0)