Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisRackauckas committed Nov 3, 2023
1 parent 8728b71 commit fc39a0e
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion test/nested_ad_tests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Integrals, ForwardDiff, Cubature, Test
using Integrals, ForwardDiff, Cubature, Cuba, Test

my_parameters = [1.0, 2.0]
my_function(x, p) = x^2 + p[1]^3 * x + p[2]^2
Expand All @@ -11,3 +11,36 @@ my_solution = my_integration(my_parameters)
@test ForwardDiff.jacobian(my_integration, my_parameters) == [0.0 8.0]
@test ForwardDiff.jacobian(x->ForwardDiff.jacobian(my_integration, x), my_parameters) == [0.0 0.0
0.0 4.0]

ff(x,p) = sum(sin.(x .* p))
lb = ones(2)
ub = 3ones(2)
p = [1.5,2.0]

function testf(p)
prob = IntegralProblem(ff,lb,ub,p)
sin(solve(prob,CubaCuhre(),reltol=1e-6,abstol=1e-6)[1])
end

hp1 = FiniteDiff.finite_difference_hessian(testf,p)
hp2 = ForwardDiff.hessian(testf,p)
@test hp1 hp2 atol=1e-4

ff2(x,p) = x*p[1].+p[2]*p[3]
lb =1.0
ub = 3.0
p = [2.0, 3.0, 4.0]
_ff3 = BatchIntegralFunction(ff2)
prob = IntegralProblem(_ff3,lb,ub,p)

function testf3(lb,ub,p; f=_ff3)
prob = IntegralProblem(_ff3,lb,ub,p)
solve(prob, CubatureJLh(); reltol=1e-3,abstol=1e-3)[1]
end

dp1 = ForwardDiff.gradient(p->testf3(lb,ub,p),p)
dp2 = Zygote.gradient(p->testf3(lb,ub,p),p)[1]
dp3 = FiniteDiff.finite_difference_gradient(p->testf3(lb,ub,p),p)

@test dp1 dp3
@test dp2 dp3

0 comments on commit fc39a0e

Please sign in to comment.