Skip to content

Commit 121a238

Browse files
authored
Fix and test type inference of jacobian (#200)
* Fix and test type inference of `jacobian` * Update Project.toml
1 parent 178cfb5 commit 121a238

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
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.20"
3+
version = "0.12.21"
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
@@ -21,7 +21,7 @@ function jacobian(fdm, f, x::Vector{<:Real}; len=nothing)
2121
return ret
2222
end
2323
end
24-
return (hcat(ẏs...), )
24+
return (reduce(hcat, ẏs), )
2525
end
2626

2727
function jacobian(fdm, f, x; len=nothing)

test/grad.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ using FiniteDifferences: grad, jacobian, _jvp, jvp, j′vp, _j′vp, to_vec
99
ẋ, ẏ = randn(rng, T, N), randn(rng, T, M)
1010
xy, ẋẏ = vcat(x, y), vcat(ẋ, ẏ)
1111
ż_manual = _jvp(fdm, (xy)->sum(sin, xy), xy, ẋẏ)[1]
12-
ż_auto = jvp(fdm, x->sum(sin, x[1]) + sum(sin, x[2]), ((x, y), (ẋ, ẏ)))
13-
ż_multi = jvp(fdm, (x, y)->sum(sin, x) + sum(sin, y), (x, ẋ), (y, ẏ))
12+
ż_auto = @inferred(jvp(fdm, x->sum(sin, x[1]) + sum(sin, x[2]), ((x, y), (ẋ, ẏ))))
13+
ż_multi = @inferred(jvp(fdm, (x, y)->sum(sin, x) + sum(sin, y), (x, ẋ), (y, ẏ)))
1414
@test ż_manual ż_auto
1515
@test ż_manual ż_multi
1616
end
@@ -52,7 +52,7 @@ using FiniteDifferences: grad, jacobian, _jvp, jvp, j′vp, _j′vp, to_vec
5252
@assert length(ȳ) == length(f(x))
5353

5454
# Check that the jacobian is as expected.
55-
J_fdm = jacobian(fdm, f, x)[1]
55+
J_fdm = @inferred(jacobian(fdm, f, x))[1]
5656
@test size(J_fdm) == (length(ȳ), length(x))
5757
@test J_fdm J_exact
5858
@test J_fdm == jacobian(fdm, f, x)[1]
@@ -79,7 +79,8 @@ using FiniteDifferences: grad, jacobian, _jvp, jvp, j′vp, _j′vp, to_vec
7979
@test Ac == A
8080

8181
# Prevent regression against https://github.com/JuliaDiff/FiniteDifferences.jl/issues/67
82-
J = first(jacobian(fdm, identity, x))
82+
# Type inference: https://github.com/JuliaDiff/FiniteDifferences.jl/issues/199
83+
J = first(@inferred(jacobian(fdm, identity, x)))
8384
@test J one(Matrix{T}(undef, size(J)))
8485
end
8586

0 commit comments

Comments
 (0)