Skip to content

Commit

Permalink
Override ChainRules' imag rule
Browse files Browse the repository at this point in the history
  • Loading branch information
sethaxen committed Jun 30, 2020
1 parent 59cd2df commit 6a2bf00
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/lib/number.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ end

@adjoint (T::Type{<:Complex})(re, im) = T(re, im), c̄ -> (nothing, real(c̄), imag(c̄))

# we define these here because ChainRules.jl only defines them for x::Union{Real,Complex}

@adjoint abs2(x::Number) = abs2(x), Δ -> (real(Δ)*(x + x),)
@adjoint real(x::Number) = real(x), r̄ -> (real(r̄),)
@adjoint conj(x::Number) = conj(x), r̄ -> (conj(r̄),)
@adjoint imag(x::Number) = imag(x), ī -> (real(ī)*im,)

@adjoint abs2(x::Number) = abs2(x), Δ -> (real(Δ)*(x + x),)
# for real x, ChainRules pulls back a zero real adjoint, whereas we treat x
# as embedded in the complex numbers and pull back a pure imaginary adjoint
@adjoint imag(x::Real) = zero(x), ī -> (real(ī)*im,)

0 comments on commit 6a2bf00

Please sign in to comment.