Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "FiniteDifferences"
uuid = "26cc04aa-876d-5657-8c51-4c34ba976000"
version = "0.10.3"
version = "0.10.4"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
1 change: 1 addition & 0 deletions src/grad.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ end
j′vp(fdm, f, ȳ, xs...) = j′vp(fdm, xs->f(xs...), ȳ, xs)[1]

function _j′vp(fdm, f, ȳ::Vector{<:Real}, x::Vector{<:Real})
isempty(x) && return eltype(ȳ)[] # if x is empty, then so is the jacobian and x̄
return transpose(first(jacobian(fdm, f, x))) * ȳ
end

Expand Down
20 changes: 11 additions & 9 deletions test/grad.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,17 @@ using FiniteDifferences: grad, jacobian, _jvp, jvp, j′vp, _j′vp, to_vec
end

@testset "j′vp(::$T)" for T in (Float64,)
rng, N, M, fdm = MersenneTwister(123456), 2, 3, central_fdm(5, 1)
x, y = randn(rng, T, N), randn(rng, T, M)
z̄ = randn(rng, T, N + M)
xy = vcat(x, y)
x̄ȳ_manual = j′vp(fdm, xy->sin.(xy), z̄, xy)[1]
x̄ȳ_auto = j′vp(fdm, x->sin.(vcat(x[1], x[2])), z̄, (x, y))[1]
x̄ȳ_multi = j′vp(fdm, (x, y)->sin.(vcat(x, y)), z̄, x, y)
@test x̄ȳ_manual ≈ vcat(x̄ȳ_auto...)
@test x̄ȳ_manual ≈ vcat(x̄ȳ_multi...)
rng, fdm = MersenneTwister(123456), central_fdm(5, 1)
@testset "x with length $N, y with length $M" for (N, M) in ((2, 3), (0, 0), (0, 3))
x, y = randn(rng, T, N), randn(rng, T, M)
z̄ = randn(rng, T, N + M)
xy = vcat(x, y)
x̄ȳ_manual = j′vp(fdm, xy->sin.(xy), z̄, xy)[1]
x̄ȳ_auto = j′vp(fdm, x->sin.(vcat(x[1], x[2])), z̄, (x, y))[1]
x̄ȳ_multi = j′vp(fdm, (x, y)->sin.(vcat(x, y)), z̄, x, y)
@test x̄ȳ_manual ≈ vcat(x̄ȳ_auto...)
@test x̄ȳ_manual ≈ vcat(x̄ȳ_multi...)
end
end

# Tests for complex numbers, to prevent regressions against
Expand Down