Diffractor.jl icon indicating copy to clipboard operation
Diffractor.jl copied to clipboard

`x.re` gives a Tangent not a Complex

Open mcabbott opened this issue 3 years ago • 1 comments
trafficstars

julia> gradient(x -> x.re, 2+3im)
(Tangent{Complex{Int64}}(re = 1,),)

julia> gradient(x -> abs2(x * x.re), 4+5im)
ERROR: MethodError: no method matching +(::ComplexF64, ::Tangent{Complex{Int64}, NamedTuple{(:re,), Tuple{Float64}}})

mcabbott avatar Jan 12 '22 03:01 mcabbott

~~I suspect we should fix this in ChainRulesCore. IIUC this works in Zygote because it turns the Tangent types into NamedTuples internally. Should we move the issue there?~~

julia> t = Tangent{Complex{Int64}}(re = 1,)

julia> t + 1im
1 + 1im

julia> t + 1.0im
ERROR: MethodError: no method matching +(::Tangent{Complex{Int64}, NamedTuple{(:re,), Tuple{Int64}}}, ::ComplexF64)

~~We probably have to modify Base.:+(a::Tangent{P}, b::P) where {P} = b + a to handle projections? (Did not check)~~

Oh, sorry, I retract all the above, indeed it should just return a Complex number

mzgubic avatar Jan 12 '22 11:01 mzgubic