Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TimSiebert1 committed Jul 3, 2024
1 parent 79cd040 commit 43daaba
Show file tree
Hide file tree
Showing 5 changed files with 297 additions and 3 deletions.
4 changes: 1 addition & 3 deletions test/abs_normal/test_derivative!.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
1,
3,
x,
:abs_normal;
tape_id=abs_normal_form.tape_id,
reuse_tape=true,
:abs_normal
)

@test abs_normal_form.Y[1, 1] == -1.5
Expand Down
8 changes: 8 additions & 0 deletions test/first_order/test_derivative!.jl
Original file line number Diff line number Diff line change
Expand Up @@ -351,3 +351,11 @@ end
@test res[3, 2] == 0.0
@test res[3, 3] == 0.0
end

@testset "tape_less_forward!" begin()
f(x) = x[1]^2
x = 2.0
res = allocator(1, 1, :jac, 0, 0)
ADOLC.tape_less_forward!(res, f, 1, x)
@test res[1] == 4.0
end
37 changes: 37 additions & 0 deletions test/first_order/test_derivative.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ end

@test res[1] == -1.0
@test res[2] == 0.0

end

@testset "reuse_jac_vec" begin
Expand All @@ -151,6 +152,11 @@ end

@test res[1] == 1.0
@test res[2] == 0.0

res = derivative(f, [2.0, 1.0, 2.0], :jac_vec; dir=CxxVector([0.0, 1.0, 0.0]), reuse_tape=true)

@test res[1] == 1.0
@test res[2] == 0.0
end

@testset "jac_mat" begin
Expand Down Expand Up @@ -206,6 +212,17 @@ end
@test res[2, 1] == 0.0
@test res[2, 2] == 0.0
@test res[2, 3] == 12.0


res = derivative(f, [2.0, 1.0, 2.0], :jac_mat; dir=CxxMatrix(dir), reuse_tape=true)

@test res[1, 1] == 0.0
@test res[1, 2] == -3.0
@test res[1, 3] == 0.0

@test res[2, 1] == 0.0
@test res[2, 2] == 0.0
@test res[2, 3] == 12.0
end

@testset "vec_jac" begin
Expand Down Expand Up @@ -240,6 +257,12 @@ end
@test res[1] == 0.0
@test res[2] == 0.0
@test res[3] == 12

res = derivative(f, [2.0, 1.0, 2.0], :vec_jac; weights=CxxVector([0.0, 1.0]), reuse_tape=true)

@test res[1] == 0.0
@test res[2] == 0.0
@test res[3] == 12
end

@testset "mat_jac" begin
Expand Down Expand Up @@ -330,4 +353,18 @@ end
@test res[3, 1] == 0.0
@test res[3, 2] == 0.0
@test res[3, 3] == 0.0


res = derivative(f, [2.0, 1.0, 2.0], :mat_jac; weights=CxxMatrix(weights), reuse_tape=true)
@test res[1, 1] == 0.0
@test res[1, 2] == 0.0
@test res[1, 3] == -12.0

@test res[2, 1] == 0.0
@test res[2, 2] == 0.0
@test res[2, 3] == 12.0

@test res[3, 1] == 0.0
@test res[3, 2] == 0.0
@test res[3, 3] == 0.0
end
101 changes: 101 additions & 0 deletions test/higher_order/test_derivative!.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,104 @@ end
@test res[8] 0.0
@test res[9] 24.0
end


@testset "higher_order_full_tensor" begin()

function f(x)
return [x[1]^2 * x[2]^2, x[3]^2 * x[4]^2]
end


x = [1.0, 2.0, 3.0, 4.0]
m = 2
n = 4
num_seeds = n
degree = 2
res = CxxMatrix(m, binomial(num_seeds + degree, degree))
derivative!(
res,
f,
m,
n,
x,
degree,
CxxMatrix(create_cxx_identity(n, n), n, n)
)

@test res[1, ADOLC.tensor_address(degree, [1, 0])] == 8.0
@test res[1, ADOLC.tensor_address(degree, [2, 0])] == 4.0
@test res[1, ADOLC.tensor_address(degree, [3, 0])] == 0.0
@test res[1, ADOLC.tensor_address(degree, [4, 0])] == 0.0

@test res[2, ADOLC.tensor_address(degree, [1, 0])] == 0.0
@test res[2, ADOLC.tensor_address(degree, [2, 0])] == 0.0
@test res[2, ADOLC.tensor_address(degree, [3, 0])] == 96.0
@test res[2, ADOLC.tensor_address(degree, [4, 0])] == 72.0

@test res[1, ADOLC.tensor_address(degree, [1, 1])] == 8.0
@test res[1, ADOLC.tensor_address(degree, [2, 1])] == 8.0
@test res[1, ADOLC.tensor_address(degree, [3, 1])] == 0.0
@test res[1, ADOLC.tensor_address(degree, [4, 1])] == 0.0
@test res[1, ADOLC.tensor_address(degree, [2, 2])] == 2.0
@test res[1, ADOLC.tensor_address(degree, [3, 2])] == 0.0
@test res[1, ADOLC.tensor_address(degree, [4, 3])] == 0.0
@test res[1, ADOLC.tensor_address(degree, [3, 3])] == 0.0
@test res[1, ADOLC.tensor_address(degree, [4, 3])] == 0.0
@test res[1, ADOLC.tensor_address(degree, [4, 4])] == 0.0
@test res[1, ADOLC.tensor_address(degree, [4, 2])] == 0.0

@test res[2, ADOLC.tensor_address(degree, [1, 1])] == 0.0
@test res[2, ADOLC.tensor_address(degree, [2, 1])] == 0.0
@test res[2, ADOLC.tensor_address(degree, [3, 1])] == 0.0
@test res[2, ADOLC.tensor_address(degree, [4, 1])] == 0.0
@test res[2, ADOLC.tensor_address(degree, [2, 2])] == 0.0
@test res[2, ADOLC.tensor_address(degree, [3, 2])] == 0.0
@test res[2, ADOLC.tensor_address(degree, [4, 2])] == 0.0
@test res[2, ADOLC.tensor_address(degree, [4, 3])] == 48.0
@test res[2, ADOLC.tensor_address(degree, [3, 3])] == 32.0
@test res[2, ADOLC.tensor_address(degree, [4, 4])] == 18.0

derivative!(
res,
f,
m,
n,
x,
degree
)

@test res[1, ADOLC.tensor_address(degree, [1, 0])] == 8.0
@test res[1, ADOLC.tensor_address(degree, [2, 0])] == 4.0
@test res[1, ADOLC.tensor_address(degree, [3, 0])] == 0.0
@test res[1, ADOLC.tensor_address(degree, [4, 0])] == 0.0

@test res[2, ADOLC.tensor_address(degree, [1, 0])] == 0.0
@test res[2, ADOLC.tensor_address(degree, [2, 0])] == 0.0
@test res[2, ADOLC.tensor_address(degree, [3, 0])] == 96.0
@test res[2, ADOLC.tensor_address(degree, [4, 0])] == 72.0

@test res[1, ADOLC.tensor_address(degree, [1, 1])] == 8.0
@test res[1, ADOLC.tensor_address(degree, [2, 1])] == 8.0
@test res[1, ADOLC.tensor_address(degree, [3, 1])] == 0.0
@test res[1, ADOLC.tensor_address(degree, [4, 1])] == 0.0
@test res[1, ADOLC.tensor_address(degree, [2, 2])] == 2.0
@test res[1, ADOLC.tensor_address(degree, [3, 2])] == 0.0
@test res[1, ADOLC.tensor_address(degree, [4, 3])] == 0.0
@test res[1, ADOLC.tensor_address(degree, [3, 3])] == 0.0
@test res[1, ADOLC.tensor_address(degree, [4, 3])] == 0.0
@test res[1, ADOLC.tensor_address(degree, [4, 4])] == 0.0
@test res[1, ADOLC.tensor_address(degree, [4, 2])] == 0.0

@test res[2, ADOLC.tensor_address(degree, [1, 1])] == 0.0
@test res[2, ADOLC.tensor_address(degree, [2, 1])] == 0.0
@test res[2, ADOLC.tensor_address(degree, [3, 1])] == 0.0
@test res[2, ADOLC.tensor_address(degree, [4, 1])] == 0.0
@test res[2, ADOLC.tensor_address(degree, [2, 2])] == 0.0
@test res[2, ADOLC.tensor_address(degree, [3, 2])] == 0.0
@test res[2, ADOLC.tensor_address(degree, [4, 2])] == 0.0
@test res[2, ADOLC.tensor_address(degree, [4, 3])] == 48.0
@test res[2, ADOLC.tensor_address(degree, [3, 3])] == 32.0
@test res[2, ADOLC.tensor_address(degree, [4, 4])] == 18.0

end
Loading

0 comments on commit 43daaba

Please sign in to comment.