1+ # module forward_mutation
12using Diffractor
23using Diffractor: ββ, ZeroBundle, TaylorBundle
3- using Diffractor: bundle
4+ using Diffractor: bundle, first_partial, TaylorTangentIndex
5+ using ChainRulesCore
6+ using Test
7+
48
59mutable struct MDemo1
610 x:: Float64
711end
812
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
1024
1125function double! (val:: MDemo1 )
1226 val. x *= 2.0
@@ -16,4 +30,22 @@ function wrap_and_double(x)
1630 val = MDemo1 (x)
1731 double! (val)
1832end
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