Skip to content

Commit

Permalink
Add tests for second derivatives
Browse files Browse the repository at this point in the history
  • Loading branch information
gerlero committed Dec 21, 2023
1 parent 5ffb97c commit ea14c4e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
3 changes: 3 additions & 0 deletions test/finitedifferences.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ using FiniteDifferences
@testset "Jacobian" begin
test_jacobians(backend)
end
@testset "Second derivative" begin
test_second_derivatives(backend)
end
@testset "Hessian" begin
test_hessians(backend)
end
Expand Down
3 changes: 3 additions & 0 deletions test/forwarddiff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ using ForwardDiff
@testset "Jacobian" begin
test_jacobians(backend)
end
@testset "Second derivative" begin
test_second_derivatives(backend)
end
@testset "Hessian" begin
test_hessians(backend)
end
Expand Down
3 changes: 3 additions & 0 deletions test/reversediff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ using ReverseDiff
@testset "Jacobian" begin
test_jacobians(backend)
end
@testset "Second derivative" begin
test_second_derivatives(backend)
end
@testset "Hessian" begin
test_hessians(backend)
end
Expand Down
44 changes: 44 additions & 0 deletions test/test_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Random.seed!(1234)
fder(x, y) = exp(y) * x + y * log(x)
dfderdx(x, y) = exp(y) + y * 1 / x
dfderdy(x, y) = exp(y) * x + log(x)
dfderdxdx(x, y) = -y * 1 / x^2

fgrad(x, y) = prod(x) + sum(y ./ (1:length(y)))
dfgraddx(x, y) = prod(x) ./ x
Expand Down Expand Up @@ -143,6 +144,49 @@ function test_jacobians(backend; multiple_inputs=true, test_types=true)
@test yvec == yvec2
end

function test_second_derivatives(backend; multiple_inputs=false, test_types=true)
if multiple_inputs
# ... but
error("multiple_inputs=true is not supported.")
else
# explicit test that AbstractDifferentiation throws an error
# don't support tuple of second derivatives
@test_throws ArgumentError AD.second_derivative(
backend, x -> fder(x, yscalar), (xscalar, yscalar)
)
@test_throws MethodError AD.second_derivative(
backend, x -> fder(x, yscalar), xscalar, yscalar
)
end

# test if single input (no tuple works)
dder1 = AD.second_derivative(backend, x -> fder(x, yscalar), xscalar)
if test_types
@test dder1[1] isa Float64
end
@test dfderdxdx(xscalar, yscalar) dder1[1] atol = 1e-8
valscalar, dder2 = AD.value_and_second_derivative(
backend, x -> fder(x, yscalar), xscalar
)
if test_types
@test valscalar isa Float64
@test dder2[1] isa Float64
end
@test valscalar == fder(xscalar, yscalar)
@test norm.(dder2 .- dder1) == (0,)
valscalar, der, dder3 = AD.value_and_derivatives(
backend, x -> fder(x, yscalar), xscalar
)
if test_types
@test valscalar isa Float64
@test der[1] isa Float64
@test dder3[1] isa Float64
end
@test valscalar == fder(xscalar, yscalar)
@test norm.(der .- AD.derivative(backend, x -> fder(x, yscalar), xscalar)) == (0,)
@test norm.(dder3 .- dder1) == (0,)
end

function test_hessians(backend; multiple_inputs=false, test_types=true)
if multiple_inputs
# ... but
Expand Down

0 comments on commit ea14c4e

Please sign in to comment.