From 55ad935e9578fdeb5992c48da1ba41fa2386a2b4 Mon Sep 17 00:00:00 2001 From: Adam Wheeler Date: Fri, 16 May 2025 11:17:34 -0400 Subject: [PATCH 1/3] make hessians of scaled interpolants work, and add a test that would have caught the bug --- ext/InterpolationsForwardDiffExt.jl | 9 +++++---- test/hessian.jl | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/ext/InterpolationsForwardDiffExt.jl b/ext/InterpolationsForwardDiffExt.jl index 39a09a21..19d89b40 100644 --- a/ext/InterpolationsForwardDiffExt.jl +++ b/ext/InterpolationsForwardDiffExt.jl @@ -14,10 +14,11 @@ end # apply partials from arbitrarily nested ForwardDiff.Dual to a value # used in maybe_clamp, above -function apply_partials(x_dual::D, val::Number) where D <: ForwardDiff.Dual - ∂s = ForwardDiff.partials(x_dual) - apply_partials(ForwardDiff.value(x_dual), D(val, ∂s)) +function apply_partials(dual_number::D, x::Number) where D <: ForwardDiff.Dual + ∂s = ForwardDiff.partials(dual_number) + val = ForwardDiff.value(dual_number) + D(apply_partials(val, x) , ∂s) end -apply_partials(_::Number, val::Number) = val +apply_partials(_::Number, x::Number) = x end diff --git a/test/hessian.jl b/test/hessian.jl index 614936c8..6de27282 100644 --- a/test/hessian.jl +++ b/test/hessian.jl @@ -72,4 +72,21 @@ using Test, Interpolations, LinearAlgebra, ForwardDiff end end end + + @testset "scaled interpolators / convienience constructors (issue 627)" begin + N = 11 + x = range(1.0, 3.0, N) + y = range(-3.0, 3.0, N) + u = rand(N, N) + + for uitp in [ + Interpolations.linear_interpolation((x, y), u), + Interpolations.cubic_spline_interpolation((x, y), u), + ] + f = x -> uitp(x[1], x[2]) + x = [π/3, -π/2] + + @test ForwardDiff.hessian(f, x) ≈ Interpolations.hessian(uitp, x[1], x[2]) + end + end end From 364b90d275882a8a3601d12866832645ffbb9f2c Mon Sep 17 00:00:00 2001 From: Adam Wheeler Date: Fri, 16 May 2025 15:40:35 -0400 Subject: [PATCH 2/3] make the test deterministic --- test/hessian.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/hessian.jl b/test/hessian.jl index 6de27282..39ad0903 100644 --- a/test/hessian.jl +++ b/test/hessian.jl @@ -77,7 +77,7 @@ using Test, Interpolations, LinearAlgebra, ForwardDiff N = 11 x = range(1.0, 3.0, N) y = range(-3.0, 3.0, N) - u = rand(N, N) + u = reshape(sin.(1 : (N^2)), N, N) for uitp in [ Interpolations.linear_interpolation((x, y), u), From a625a349606a96fda30209052b2db8b1b9c4e3d2 Mon Sep 17 00:00:00 2001 From: Mark Kittisopikul Date: Mon, 19 May 2025 03:15:18 -0400 Subject: [PATCH 3/3] Bump version and news --- NEWS.md | 5 +++++ Project.toml | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 088234da..c3c45957 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,10 @@ For a comprehensive list of changes, see [Releases](https://github.com/JuliaMath/Interpolations.jl/releases). +# v0.16.1 + +Fixes: +- Fix `hessian` with ForwardDiff.jl for scaled interpolants ([#628](https://github.com/JuliaMath/Interpolations.jl/pull/628)) + # v0.16 Breaking changes: diff --git a/Project.toml b/Project.toml index b464c710..ede2f88b 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "Interpolations" uuid = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59" -version = "0.16.0" +version = "0.16.1" [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"