Skip to content

Commit 4d7835e

Browse files
authored
Fix type-inference for j′vp (#201)
* Avoid composing many functions * Add type-inferribility checks * Increment version number
1 parent 121a238 commit 4d7835e

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "FiniteDifferences"
22
uuid = "26cc04aa-876d-5657-8c51-4c34ba976000"
3-
version = "0.12.21"
3+
version = "0.12.22"
44

55
[deps]
66
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"

src/grad.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ Compute an adjoint with any types of arguments `x` for which [`to_vec`](@ref) is
7070
function j′vp(fdm, f, ȳ, x)
7171
x_vec, vec_to_x = to_vec(x)
7272
ȳ_vec, _ = to_vec(ȳ)
73-
return (vec_to_x(_j′vp(fdm, first to_vec f vec_to_x, ȳ_vec, x_vec)), )
73+
return (vec_to_x(_j′vp(fdm, x -> first(to_vec(f(vec_to_x(x)))), ȳ_vec, x_vec)), )
7474
end
7575

7676
j′vp(fdm, f, ȳ, xs...) = j′vp(fdm, xs->f(xs...), ȳ, xs)[1]

test/grad.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,10 @@ using FiniteDifferences: grad, jacobian, _jvp, jvp, j′vp, _j′vp, to_vec
146146
x, y = randn(rng, T, N), randn(rng, T, M)
147147
= randn(rng, T, N + M)
148148
xy = vcat(x, y)
149-
x̄ȳ_manual = j′vp(fdm, xy->sin.(xy), z̄, xy)[1]
150-
x̄ȳ_auto = j′vp(fdm, x->sin.(vcat(x[1], x[2])), z̄, (x, y))[1]
151-
x̄ȳ_multi = j′vp(fdm, (x, y)->sin.(vcat(x, y)), z̄, x, y)
149+
# Type inference: https://github.com/JuliaDiff/FiniteDifferences.jl/issues/199
150+
x̄ȳ_manual = @inferred(j′vp(fdm, xy->sin.(xy), z̄, xy))[1]
151+
x̄ȳ_auto = @inferred(j′vp(fdm, x->sin.(vcat(x[1], x[2])), z̄, (x, y)))[1]
152+
x̄ȳ_multi = @inferred(j′vp(fdm, (x, y)->sin.(vcat(x, y)), z̄, x, y))
152153
@test x̄ȳ_manual vcat(x̄ȳ_auto...)
153154
@test x̄ȳ_manual vcat(x̄ȳ_multi...)
154155
end

0 commit comments

Comments
 (0)