Skip to content

Commit 25d42a2

Browse files
authored
Merge pull request #240 from JuliaMath/teh/fix_239
Define fallbacks for `gradient!` and `hessian!` (fixes #239)
2 parents 44d6946 + c47073f commit 25d42a2

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

src/Interpolations.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,12 +348,18 @@ function gradient(itp::AbstractInterpolation, x::Vararg{UnexpandedIndexTypes})
348348
xi == x && error("gradient of $itp not supported for position $x")
349349
gradient(itp, xi...)
350350
end
351+
@propagate_inbounds function gradient!(dest, itp::AbstractInterpolation{T,N}, x::Vararg{Number,N}) where {T,N}
352+
dest .= gradient(itp, x...)
353+
end
351354
function gradient!(dest, itp::AbstractInterpolation, x::Vararg{UnexpandedIndexTypes})
352355
gradient!(dest, itp, to_indices(itp, x)...)
353356
end
354357
function hessian(itp::AbstractInterpolation, x::Vararg{UnexpandedIndexTypes})
355358
hessian(itp, to_indices(itp, x)...)
356359
end
360+
@propagate_inbounds function hessian!(dest, itp::AbstractInterpolation{T,N}, x::Vararg{Number,N}) where {T,N}
361+
dest .= hessian(itp, x...)
362+
end
357363
function hessian!(dest, itp::AbstractInterpolation, x::Vararg{UnexpandedIndexTypes})
358364
hessian!(dest, itp, to_indices(itp, x)...)
359365
end

src/gridded/indexing.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ end
1313
wis = weightedindexes((value_weights, gradient_weights), itpinfo(itp)..., x)
1414
SVector(map(inds->coefficients(itp)[inds...], wis))
1515
end
16-
@propagate_inbounds function gradient!(dest, itp::GriddedInterpolation{T,N}, x::Vararg{Number,N}) where {T,N}
17-
dest .= gradient(itp, x...)
18-
end
1916

2017
itpinfo(itp::GriddedInterpolation) = (tcollect(itpflag, itp), itp.knots)
2118

test/gradient.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,13 @@ using Test, Interpolations, DualNumbers, LinearAlgebra
163163
end
164164
end
165165

166+
# issue #239
167+
f(x) = log(x)
168+
xs = 1:0.2:5
169+
A = [f(x) for x in xs]
170+
interp_linear = LinearInterpolation(xs, A)
171+
g = Interpolations.gradient(interp_linear, 1.5)
172+
dest = Vector{Float64}(undef, 1)
173+
Interpolations.gradient!(dest, interp_linear, 1.5)
174+
@test dest == g
166175
end

0 commit comments

Comments
 (0)