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

Reinterpret discards information and returns incorrect result

Open antoine-levitt opened this issue 1 year ago • 4 comments

using ForwardDiff
E(x) = abs2(x[1])
xf = randn(ComplexF64, 2)
# WRONG
r2c(x) = reinterpret(ComplexF64, x)
c2r(x) = reinterpret(Float64, x)
# # OK
# r2c(x) = x[1:2] + im*x[3:4]
# c2r(x) = vcat(real(x), imag(x))
ForwardDiff.gradient(x -> E(r2c(x)), c2r(x))

antoine-levitt avatar May 21 '24 15:05 antoine-levitt