@@ -7,10 +7,10 @@ struct Dual{T,V<:Real,N} <: Real
7
7
partials:: Partials{N,V}
8
8
end
9
9
10
-
11
10
# #############
12
11
# Exceptions #
13
12
# #############
13
+
14
14
struct DualMismatchError{A,B} <: Exception
15
15
a:: A
16
16
b:: B
48
48
49
49
@inline Dual (args... ) = Dual {Void} (args... )
50
50
51
- # ##################
52
- # Promotion #
53
- # ##################
54
-
55
- Base. @pure function Base. promote_rule (:: Type{Dual{T1,V1,N1}} ,
56
- :: Type{Dual{T2,V2,N2}} ) where {T1,V1<: Real ,N1,T2,V2<: Real ,N2}
57
- # V1 and V2 might themselves be Dual types
58
- if T2 ≺ T1
59
- Dual{T1,promote_type (V1,Dual{T2,V2,N2}),N1}
60
- else
61
- Dual{T2,promote_type (V2,Dual{T1,V1,N1}),N2}
62
- end
63
- end
64
-
65
51
# #############################
66
52
# Utility/Accessor Functions #
67
53
# #############################
68
54
69
55
@inline value (x:: Real ) = x
70
56
@inline value (d:: Dual ) = d. value
71
57
72
- @inline value (:: Type{T} , x:: Real ) where T = x
58
+ @inline value (:: Type{T} , x:: Real ) where T = x
73
59
@inline value (:: Type{T} , d:: Dual{T} ) where T = value (d)
74
60
function value (:: Type{T} , d:: Dual{S} ) where {T,S}
75
61
# TODO : in the case of nested Duals, it may be possible to "transpose" the Dual objects
83
69
@inline partials (d:: Dual , i, j) = partials (d, i). partials[j]
84
70
@inline partials (d:: Dual , i, j, k... ) = partials (partials (d, i, j), k... )
85
71
86
- @inline partials (:: Type{T} , x:: Real , i... ) where T =
87
- partials (x, i... )
88
- @inline partials (:: Type{T} , d:: Dual{T} , i... ) where T =
89
- partials (d, i... )
90
- partials (:: Type{T} , d:: Dual{S} , i... ) where {T,S} =
91
- throw (DualMismatchError (T,S))
92
-
93
-
72
+ @inline partials (:: Type{T} , x:: Real , i... ) where T = partials (x, i... )
73
+ @inline partials (:: Type{T} , d:: Dual{T} , i... ) where T = partials (d, i... )
74
+ partials (:: Type{T} , d:: Dual{S} , i... ) where {T,S} = throw (DualMismatchError (T,S))
94
75
95
76
@inline npartials (:: Dual{T,V,N} ) where {T,V,N} = N
96
77
@inline npartials (:: Type{Dual{T,V,N}} ) where {T,V,N} = N
307
288
# Promotion/Conversion #
308
289
# #######################
309
290
291
+ Base. @pure function Base. promote_rule (:: Type{Dual{T1,V1,N1}} ,
292
+ :: Type{Dual{T2,V2,N2}} ) where {T1,V1<: Real ,N1,T2,V2<: Real ,N2}
293
+ # V1 and V2 might themselves be Dual types
294
+ if T2 ≺ T1
295
+ Dual{T1,promote_type (V1,Dual{T2,V2,N2}),N1}
296
+ else
297
+ Dual{T2,promote_type (V2,Dual{T1,V1,N1}),N2}
298
+ end
299
+ end
300
+
310
301
function Base. promote_rule (:: Type{Dual{T,A,N}} ,
311
302
:: Type{Dual{T,B,N}} ) where {T,A<: Real ,B<: Real ,N}
312
303
return Dual{T,promote_type (A, B),N}
@@ -337,7 +328,6 @@ Base.AbstractFloat(d::Dual{T,V,N}) where {T,V,N} = Dual{T,promote_type(V, Float1
337
328
338
329
@inline Base. conj (d:: Dual ) = d
339
330
340
-
341
331
@inline Base. transpose (d:: Dual ) = d
342
332
343
333
@inline Base. ctranspose (d:: Dual ) = d
0 commit comments