1
+ # module forward_mutation
1
2
using Diffractor
2
3
using Diffractor: ββ, ZeroBundle, TaylorBundle
3
- using Diffractor: bundle
4
+ using Diffractor: bundle, first_partial, TaylorTangentIndex
5
+ using ChainRulesCore
6
+ using Test
7
+
4
8
5
9
mutable struct MDemo1
6
10
x:: Float64
7
11
end
8
12
9
- ββ {1} ()(ZeroBundle {1} (MDemo1), TaylorBundle {1} (1.5 , (1.0 ,)))
13
+ @testset " construction" begin
14
+ π = ββ {1} ()(ZeroBundle {1} (MDemo1), TaylorBundle {1} (1.5 , (1.0 ,)))
15
+ @test π[TaylorTangentIndex (1 )] isa MutableTangent{MDemo1}
16
+ @test π[TaylorTangentIndex (1 )]. x == 1.0
17
+
18
+ π₯― = ββ {2} ()(ZeroBundle {2} (MDemo1), TaylorBundle {2} (1.5 , (1.0 , 10.0 )))
19
+ @test π₯―[TaylorTangentIndex (1 )] isa MutableTangent{MDemo1}
20
+ @test π₯―[TaylorTangentIndex (1 )]. x == 1.0
21
+ @test π₯―[TaylorTangentIndex (2 )] isa MutableTangent
22
+ @test π₯―[TaylorTangentIndex (2 )]. x == 10.0
23
+ end
10
24
11
25
function double! (val:: MDemo1 )
12
26
val. x *= 2.0
@@ -16,4 +30,22 @@ function wrap_and_double(x)
16
30
val = MDemo1 (x)
17
31
double! (val)
18
32
end
19
- ββ {1} ()(ZeroBundle {1} (wrap_and_double), TaylorBundle {1} (1.5 , (1.0 ,)))
33
+ π° = ββ {1} ()(ZeroBundle {1} (wrap_and_double), TaylorBundle {1} (1.5 , (1.0 ,)))
34
+ @test first_partial (π°) isa MutableTangent{MDemo1}
35
+ @test first_partial (π°). x == 2.0
36
+
37
+ # second derivative
38
+ π = ββ {2} ()(ZeroBundle {2} (wrap_and_double), TaylorBundle {2} (1.5 , (1.0 , 10.0 )))
39
+ @test π[TaylorTangentIndex (1 )] isa MutableTangent{MDemo1}
40
+ @test π[TaylorTangentIndex (1 )]. x == 2.0
41
+ @test π[TaylorTangentIndex (2 )] isa MutableTangent
42
+ @test π[TaylorTangentIndex (2 )] == 0.0 # returns 20
43
+
44
+
45
+
46
+ foo (val) = val^ 2
47
+ π₯ = ββ {2} ()(ZeroBundle {2} (foo), TaylorBundle {2} (1.0 , (0.0 , 10.0 )))
48
+ π₯[TaylorTangentIndex (1 )] # returns 0
49
+ π₯[TaylorTangentIndex (2 )] # returns 20
50
+
51
+ # end # module
0 commit comments