File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -25,6 +25,22 @@ struct Tangent{P,T} <: AbstractTangent
25
25
# Note: If T is a Tuple/Dict, then P is also a Tuple/Dict
26
26
# (but potentially a different one, as it doesn't contain differentials)
27
27
backing:: T
28
+
29
+ function Tangent {P,T} (backing) where {P,T}
30
+ function backing_error (P, G, E)
31
+ msg = " Tangent for the primal $P should be backed by a $E type, not by $G ."
32
+ throw (ArgumentError (msg))
33
+ end
34
+
35
+ if P <: Tuple
36
+ T <: Tuple || backing_error (P, T, Tuple)
37
+ elseif P <: AbstractDict
38
+ T <: AbstractDict || backing_error (P, T, AbstractDict)
39
+ else
40
+ T <: NamedTuple || backing_error (P, T, NamedTuple)
41
+ end
42
+ return new (backing)
43
+ end
28
44
end
29
45
30
46
function Tangent {P} (; kwargs... ) where {P}
Original file line number Diff line number Diff line change 23
23
@test typeof (Tangent {Tuple{}} ()) == Tangent{Tuple{},Tuple{}}
24
24
end
25
25
26
+ @testset " constructor" begin
27
+ t = (1.0 , 2.0 )
28
+ nt = (x = 1 , y= 2.0 )
29
+ d = Dict (:x => 1.0 , :y => 2.0 )
30
+ vals = [1 , 2 ]
31
+
32
+ @test_throws ArgumentError Tangent {typeof(t), typeof(nt)} (nt)
33
+ @test_throws ArgumentError Tangent {typeof(t), typeof(d)} (d)
34
+
35
+ @test_throws ArgumentError Tangent {typeof(d), typeof(nt)} (nt)
36
+ @test_throws ArgumentError Tangent {typeof(d), typeof(t)} (t)
37
+
38
+ @test_throws ArgumentError Tangent {typeof(nt), typeof(vals)} (vals)
39
+ @test_throws ArgumentError Tangent {typeof(nt), typeof(d)} (d)
40
+ @test_throws ArgumentError Tangent {typeof(nt), typeof(t)} (t)
41
+
42
+ @test_throws ArgumentError Tangent {Foo, typeof(d)} (d)
43
+ @test_throws ArgumentError Tangent {Foo, typeof(t)} (t)
44
+ end
45
+
26
46
@testset " ==" begin
27
47
@test Tangent {Foo} (; x= 0.1 , y= 2.5 ) == Tangent {Foo} (; x= 0.1 , y= 2.5 )
28
48
@test Tangent {Foo} (; x= 0.1 , y= 2.5 ) == Tangent {Foo} (; y= 2.5 , x= 0.1 )
You can’t perform that action at this time.
0 commit comments