1
1
2
- using SafeTestsets
2
+ using Test
3
3
4
- @safetestset " Bare Differentiation Operator Backends (out-of-place)" begin
5
- using DerivableFunctions, Test, ForwardDiff
6
- Metric3 (x) = [sinh (x[3 ]) exp (x[1 ])* sin (x[2 ]) 0 ; 0 cosh (x[2 ]) cos (x[2 ])* x[3 ]* x[2 ]; exp (x[2 ]) cos (x[3 ])* x[1 ]* x[2 ] 1. ]
7
-
8
- X = ForwardDiff. gradient (x-> x[1 ]^ 2 + exp (x[2 ]), [5 ,10. ])
9
- Y = ForwardDiff. jacobian (x-> [x[1 ]^ 2 , exp (x[2 ])], [5 ,10. ])
10
- Z = ForwardDiff. hessian (x-> x[1 ]^ 2 + exp (x[2 ]) + x[1 ]* x[2 ], [5 ,10. ])
11
- Mat = reshape (ForwardDiff. jacobian (vec∘ Metric3, [5 ,10 ,15. ]), 3 , 3 , 3 )
12
- Djac = reshape (ForwardDiff. jacobian (p-> vec (ForwardDiff. jacobian (x-> [exp (x[1 ])* sin (x[2 ]), cosh (x[2 ])* x[1 ]* x[2 ]],p)), [5 ,10. ]), 2 ,2 ,2 )
13
-
14
- function MyTest (ADmode:: Symbol ; atol:: Real = 2e-5 , kwargs... )
15
- Grad, Jac, Hess = GetGrad (ADmode; kwargs... ), GetJac (ADmode; kwargs... ), GetHess (ADmode; kwargs... )
16
- MatrixJac = GetMatrixJac (ADmode; order= 8 , kwargs... )
17
-
18
- @test isapprox (Grad (x-> x[1 ]^ 2 + exp (x[2 ]), [5 ,10. ]), X; atol= atol)
19
- @test isapprox (Jac (x-> [x[1 ]^ 2 , exp (x[2 ])], [5 ,10. ]), Y; atol= atol)
20
- @test isapprox (Hess (x-> x[1 ]^ 2 + exp (x[2 ]) + x[1 ]* x[2 ], [5 ,10. ]), Z; atol= atol)
21
- @test maximum (abs .(MatrixJac (Metric3, [5 ,10 ,15. ]) - Mat)) < atol
22
- end
23
-
24
- for ADmode ∈ [:Zygote , :ReverseDiff , :FiniteDifferences ]
25
- MyTest (ADmode)
26
- end
27
- MyTest (:FiniteDiff ; atol= 0.2 )
28
-
29
-
30
- function TestDoubleJac (ADmode:: Symbol ; atol:: Real = 2e-5 , kwargs... )
31
- DoubleJac = GetDoubleJac (ADmode; order= 8 , kwargs... )
32
- maximum (abs .(DoubleJac (x-> [exp (x[1 ])* sin (x[2 ]), cosh (x[2 ])* x[1 ]* x[2 ]], [5 ,10. ]) - Djac)) < atol
33
- end
34
-
35
- for ADmode ∈ [:ReverseDiff , :FiniteDifferences ]
36
- @test TestDoubleJac (ADmode)
37
- end
38
- # Zygote does not support mutating arrays
39
- @test_broken TestDoubleJac (:Zygote )
40
- @test_broken TestDoublejac (:FiniteDiff )
41
- end
42
-
43
- @safetestset " Bare Differentiation Operator Backends (in-place)" begin
44
- using DerivableFunctions, Test, ForwardDiff
45
- Metric3 (x) = [sinh (x[3 ]) exp (x[1 ])* sin (x[2 ]) 0 ; 0 cosh (x[2 ]) cos (x[2 ])* x[3 ]* x[2 ]; exp (x[2 ]) cos (x[3 ])* x[1 ]* x[2 ] 1. ]
46
-
47
- X = ForwardDiff. gradient (x-> x[1 ]^ 2 + exp (x[2 ]), [5 ,10. ])
48
- Y = ForwardDiff. jacobian (x-> [x[1 ]^ 2 , exp (x[2 ])], [5 ,10. ])
49
- Z = ForwardDiff. hessian (x-> x[1 ]^ 2 + exp (x[2 ]) + x[1 ]* x[2 ], [5 ,10. ])
50
- Mat = reshape (ForwardDiff. jacobian (vec∘ Metric3, [5 ,10 ,15. ]), 3 , 3 , 3 )
51
-
52
- function MyInplaceTest (ADmode:: Symbol ; atol:: Real = 2e-5 , kwargs... )
53
- Grad! = GetGrad! (ADmode, x-> x[1 ]^ 2 + exp (x[2 ]); kwargs... )
54
- Jac! = GetJac! (ADmode, x-> [x[1 ]^ 2 , exp (x[2 ])]; kwargs... )
55
- Hess! = GetHess! (ADmode, x-> x[1 ]^ 2 + exp (x[2 ]) + x[1 ]* x[2 ]; kwargs... )
56
- MatrixJac! = GetMatrixJac! (ADmode, Metric3; order= 8 , kwargs... )
57
-
58
- Xres = similar (X); Yres = similar (Y); Zres = similar (Z); Matres = similar (Mat)
59
-
60
- Grad! (Xres, [5 ,10. ]); @test isapprox (Xres, X; atol= atol)
61
- Jac! (Yres, [5 ,10. ]); @test isapprox (Yres, Y; atol= atol)
62
- Hess! (Zres, [5 ,10. ]); @test isapprox (Zres, Z; atol= atol)
63
- MatrixJac! (Matres, [5 ,10 ,15. ]); @test maximum (abs .(Matres - Mat)) < atol
64
- end
65
-
66
- for ADmode ∈ [:Zygote , :ReverseDiff , :FiniteDifferences ]
67
- MyInplaceTest (ADmode)
68
- end
69
- MyInplaceTest (:FiniteDiff ; atol= 0.2 )
70
- end
4
+ @testset " Compiles?" begin
5
+ using DerivableFunctions
6
+ @test true
7
+ end
0 commit comments