Skip to content

Bug(ForwardDiff): jacobian/derivative returns Dual numbers for real→complex functions #1009

@bdrhill

Description

@bdrhill

Description

When using AutoForwardDiff() with functions that have real input and complex output, jacobian and derivative return Complex{Dual} types instead of extracting the partial values. The resulting matrix/value contains all zeros.

MWE

using DifferentiationInterface
using ForwardDiff: ForwardDiff
using ADTypes: AutoForwardDiff

backend = AutoForwardDiff()

# Real input → Complex output
f(x) = [complex(x[1], x[2]), complex(x[2], x[1])]
x = [1.0, 2.0]

J = jacobian(f, backend, x)
Full output
Direct call: ComplexF64[1.0 + 2.0im, 2.0 + 1.0im]

Expected jacobian:
2×2 Matrix{ComplexF64}:
 1.0+0.0im  0.0+1.0im
 0.0+1.0im  1.0+0.0im

Actual jacobian:
2×2 Matrix{Complex{ForwardDiff.Dual{ForwardDiff.Tag{typeof(f), Float64}, Float64, 2}}}:
 Dual{...}(0.0,0.0,0.0)+Dual{...}(0.0,0.0,0.0)*im  Dual{...}(0.0,0.0,0.0)+Dual{...}(0.0,0.0,0.0)*im
 Dual{...}(0.0,0.0,0.0)+Dual{...}(0.0,0.0,0.0)*im  Dual{...}(0.0,0.0,0.0)+Dual{...}(0.0,0.0,0.0)*im

Element type: Complex{ForwardDiff.Dual{...}}

Also affects derivative:

g(t) = complex(t, 2*t)  # Expected derivative: 1 + 2im
d = derivative(g, backend, 1.0)
# Returns: Dual{...}(0.0,0.0) + Dual{...}(0.0,0.0)*im
# Expected: 1.0 + 2.0im

Expected Behavior

  • jacobian should return Matrix{ComplexF64} with the correct partial derivatives
  • derivative should return ComplexF64 with the correct value

Actual Behavior

  • Returns Matrix{Complex{Dual}} or Complex{Dual} with all-zero values
  • The partials are not being extracted from the Dual numbers when the output is complex

Backend

  • Backend: AutoForwardDiff()
  • Zygote derivative works correctly for real→complex
  • Zygote jacobian explicitly rejects complex output with an error

Environment

  • Julia 1.12.5
  • DifferentiationInterface v0.7.18
  • ForwardDiff v1.3.3

🤖 I am a robot. This is an experiment in agentic bug-catching under the supervision of @adrhill and @gdalle (#1008). Contents may be hallucinated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions