Skip to content

Commit d41e652

Browse files
committed
restore lost performance for +/-
1 parent 3ef306b commit d41e652

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/dual.jl

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ Base.AbstractFloat(d::Dual{T,V,N}) where {T,V,N} = convert(Dual{T,promote_type(V
317317
@inline Base.abs(d::Dual) = signbit(value(d)) ? -d : d
318318

319319
for (M, f, arity) in DiffRules.diffrules()
320-
in((M, f), ((:Base, :^), (:NaNMath, :pow), (:Base, :/))) && continue
320+
in((M, f), ((:Base, :^), (:NaNMath, :pow), (:Base, :/), (:Base, :+), (:Base, :-))) && continue
321321
if arity == 1
322322
eval(unary_dual_definition(M, f))
323323
elseif arity == 2
@@ -331,6 +331,31 @@ end
331331
# Special Cases #
332332
#################
333333

334+
# +/- #
335+
#-----#
336+
337+
@define_binary_dual_op(
338+
Base.:+,
339+
begin
340+
vx, vy = value(x), value(y)
341+
Dual{Txy}(vx + vy, partials(x) + partials(y))
342+
end,
343+
Dual{Tx}(value(x) + y, partials(x)),
344+
Dual{Ty}(x + value(y), partials(y))
345+
)
346+
347+
@define_binary_dual_op(
348+
Base.:-,
349+
begin
350+
vx, vy = value(x), value(y)
351+
Dual{Txy}(vx - vy, partials(x) - partials(y))
352+
end,
353+
Dual{Tx}(value(x) - y, partials(x)),
354+
Dual{Ty}(x - value(y), -partials(y))
355+
)
356+
357+
@inline Base.:-(d::Dual{T}) where {T} = Dual{T}(-value(d), -partials(d))
358+
334359
# * #
335360
#---#
336361

0 commit comments

Comments
 (0)