-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathpartials.jl
More file actions
230 lines (177 loc) · 8.37 KB
/
partials.jl
File metadata and controls
230 lines (177 loc) · 8.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
struct Partials{N,V} <: AbstractVector{V}
values::NTuple{N,V}
end
##############################
# Utility/Accessor Functions #
##############################
@generated function single_seed(::Type{Partials{N,V}}, ::Val{i}) where {N,V,i}
ex = Expr(:tuple, [ifelse(i === j, :(one(V)), :(zero(V))) for j in 1:N]...)
return :(Partials($(ex)))
end
@inline valtype(::Partials{N,V}) where {N,V} = V
@inline valtype(::Type{Partials{N,V}}) where {N,V} = V
@inline npartials(::Partials{N}) where {N} = N
@inline npartials(::Type{Partials{N,V}}) where {N,V} = N
@inline Base.length(::Partials{N}) where {N} = N
@inline Base.size(::Partials{N}) where {N} = (N,)
@inline Base.@propagate_inbounds Base.getindex(partials::Partials, i::Int) = partials.values[i]
Base.iterate(partials::Partials) = iterate(partials.values)
Base.iterate(partials::Partials, i) = iterate(partials.values, i)
Base.IndexStyle(::Type{<:Partials}) = IndexLinear()
# Can be deleted after https://github.com/JuliaLang/julia/pull/29854 is on a release
Base.mightalias(x::AbstractArray, y::Partials) = false
#####################
# Generic Functions #
#####################
@inline Base.iszero(partials::Partials) = iszero_tuple(partials.values)
@inline Base.zero(partials::Partials) = zero(typeof(partials))
@inline Base.zero(::Type{Partials{N,V}}) where {N,V} = Partials{N,V}(zero_tuple(NTuple{N,V}))
@inline Base.one(partials::Partials) = one(typeof(partials))
@inline Base.one(::Type{Partials{N,V}}) where {N,V} = Partials{N,V}(one_tuple(NTuple{N,V}))
@inline Random.rand(partials::Partials) = rand(typeof(partials))
@inline Random.rand(::Type{Partials{N,V}}) where {N,V} = Partials{N,V}(rand_tuple(NTuple{N,V}))
@inline Random.rand(rng::AbstractRNG, partials::Partials) = rand(rng, typeof(partials))
@inline Random.rand(rng::AbstractRNG, ::Type{Partials{N,V}}) where {N,V} = Partials{N,V}(rand_tuple(rng, NTuple{N,V}))
Base.isequal(a::Partials{N}, b::Partials{N}) where {N} = isequal(a.values, b.values)
Base.:(==)(a::Partials{N}, b::Partials{N}) where {N} = a.values == b.values
Base.:(<)(a::Partials{N}, b::Partials{N}) where {N} = a.values < b.values
Base.isless(a::Partials{N}, b::Partials{N}) where {N} = isless(a.values, b.values)
const PARTIALS_HASH = hash(Partials)
Base.hash(partials::Partials, hsh::UInt64) = hash(hash(partials.values, PARTIALS_HASH), hsh)
@inline Base.copy(partials::Partials) = partials
Base.read(io::IO, ::Type{Partials{N,V}}) where {N,V} = Partials{N,V}(ntuple(i->read(io, V), N))
function Base.write(io::IO, partials::Partials)
for p in partials
write(io, p)
end
end
########################
# Conversion/Promotion #
########################
Base.promote_rule(::Type{Partials{N,A}}, ::Type{Partials{N,B}}) where {N,A,B} = Partials{N,promote_type(A, B)}
Base.convert(::Type{Partials{N,V}}, partials::Partials) where {N,V} = Partials{N,V}(partials.values)
Base.convert(::Type{Partials{N,V}}, partials::Partials{N,V}) where {N,V} = partials
########################
# Arithmetic Functions #
########################
@inline Base.:+(a::Partials{N}, b::Partials{N}) where {N} = Partials(add_tuples(a.values, b.values))
@inline Base.:-(a::Partials{N}, b::Partials{N}) where {N} = Partials(sub_tuples(a.values, b.values))
@inline Base.:-(partials::Partials) = Partials(minus_tuple(partials.values))
@inline Base.:*(x::Real, partials::Partials) = partials*x
@inline function Base.:*(partials::Partials, x::Real)
return Partials(scale_tuple(partials.values, x))
end
@inline function Base.:/(partials::Partials, x::Real)
return Partials(div_tuple_by_scalar(partials.values, x))
end
@inline function _mul_partials(a::Partials{N}, b::Partials{N}, x_a, x_b) where N
return Partials(mul_tuples(a.values, b.values, x_a, x_b))
end
@inline function _div_partials(a::Partials, b::Partials, aval, bval)
return _mul_partials(a, b, inv(bval), -(aval / (bval*bval)))
end
# NaN/Inf-safe methods #
#----------------------#
if NANSAFE_MODE_ENABLED
# A dual number with a zero partial is just an unperturbed non-dual number
# Hence when propagated the resulting dual number is unperturbed as well,
# ie., its partial is zero as well, regardless of the primal value
# However, standard floating point multiplication/division would return `NaN`
# if the primal is not-finite/zero
@inline function _mul_partial(partial::Real, x::Real)
y = partial * x
return iszero(partial) ? zero(y) : y
end
@inline function _div_partial(partial::Real, x::Real)
y = partial / x
return iszero(partial) ? zero(y) : y
end
else
@inline _mul_partial(partial::Real, x::Real) = partial * x
@inline _div_partial(partial::Real, x::Real) = partial / x
end
# edge cases where N == 0 #
#-------------------------#
@inline Base.:+(a::Partials{0,A}, b::Partials{0,B}) where {A,B} = Partials{0,promote_type(A,B)}(tuple())
@inline Base.:+(a::Partials{0,A}, b::Partials{N,B}) where {N,A,B} = convert(Partials{N,promote_type(A,B)}, b)
@inline Base.:+(a::Partials{N,A}, b::Partials{0,B}) where {N,A,B} = convert(Partials{N,promote_type(A,B)}, a)
@inline Base.:-(a::Partials{0,A}, b::Partials{0,B}) where {A,B} = Partials{0,promote_type(A,B)}(tuple())
@inline Base.:-(a::Partials{0,A}, b::Partials{N,B}) where {N,A,B} = -(convert(Partials{N,promote_type(A,B)}, b))
@inline Base.:-(a::Partials{N,A}, b::Partials{0,B}) where {N,A,B} = convert(Partials{N,promote_type(A,B)}, a)
@inline Base.:-(partials::Partials{0,V}) where {V} = partials
@inline Base.:*(partials::Partials{0,V}, x::Real) where {V} = Partials{0,promote_type(V,typeof(x))}(tuple())
@inline Base.:*(x::Real, partials::Partials{0,V}) where {V} = Partials{0,promote_type(V,typeof(x))}(tuple())
@inline Base.:/(partials::Partials{0,V}, x::Real) where {V} = Partials{0,promote_type(V,typeof(x))}(tuple())
@inline _mul_partials(a::Partials{0,A}, b::Partials{0,B}, afactor, bfactor) where {A,B} = Partials{0,promote_type(A,B)}(tuple())
@inline _mul_partials(a::Partials{0,A}, b::Partials{N,B}, afactor, bfactor) where {N,A,B} = bfactor * b
@inline _mul_partials(a::Partials{N,A}, b::Partials{0,B}, afactor, bfactor) where {N,A,B} = afactor * a
##################################
# Generated Functions on NTuples #
##################################
# The below functions are generally
# equivalent to directly mapping over
# tuples using `map`, but run a bit
# faster since they generate inline code
# that doesn't rely on closures.
function tupexpr(f, N)
ex = Expr(:tuple, [f(i) for i=1:N]...)
return quote
$(Expr(:meta, :inline))
@inbounds return $ex
end
end
@inline iszero_tuple(::Tuple{}) = true
@inline zero_tuple(::Type{Tuple{}}) = tuple()
@inline one_tuple(::Type{Tuple{}}) = tuple()
@inline rand_tuple(::AbstractRNG, ::Type{Tuple{}}) = tuple()
@inline rand_tuple(::Type{Tuple{}}) = tuple()
@generated function iszero_tuple(tup::NTuple{N,V}) where {N,V}
ex = Expr(:&&, [:(z == tup[$i]) for i=1:N]...)
return quote
z = zero(V)
$(Expr(:meta, :inline))
@inbounds return $ex
end
end
@generated function zero_tuple(::Type{NTuple{N,V}}) where {N,V}
ex = tupexpr(i -> :(z), N)
return quote
z = zero(V)
return $ex
end
end
@generated function one_tuple(::Type{NTuple{N,V}}) where {N,V}
ex = tupexpr(i -> :(z), N)
return quote
z = one(V)
return $ex
end
end
@generated function rand_tuple(rng::AbstractRNG, ::Type{NTuple{N,V}}) where {N,V}
return tupexpr(i -> :(rand(rng, V)), N)
end
@generated function rand_tuple(::Type{NTuple{N,V}}) where {N,V}
return tupexpr(i -> :(rand(V)), N)
end
@generated function scale_tuple(tup::NTuple{N}, x) where N
return tupexpr(i -> :(_mul_partial(tup[$i], x)), N)
end
@generated function div_tuple_by_scalar(tup::NTuple{N}, x) where N
return tupexpr(i -> :(_div_partial(tup[$i], x)), N)
end
@generated function add_tuples(a::NTuple{N}, b::NTuple{N}) where N
return tupexpr(i -> :(a[$i] + b[$i]), N)
end
@generated function sub_tuples(a::NTuple{N}, b::NTuple{N}) where N
return tupexpr(i -> :(a[$i] - b[$i]), N)
end
@generated function minus_tuple(tup::NTuple{N}) where N
return tupexpr(i -> :(-tup[$i]), N)
end
@generated function mul_tuples(a::NTuple{N}, b::NTuple{N}, afactor, bfactor) where N
return tupexpr(i -> :(_mul_partial(a[$i], afactor) + _mul_partial(b[$i], bfactor)), N)
end
###################
# Pretty Printing #
###################
Base.show(io::IO, p::Partials{N}) where {N} = print(io, "Partials", p.values)