Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/derivative.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Set `check` to `Val{false}()` to disable tag checking. This can lead to perturba
end

derivative(f, x::AbstractArray) = throw(DimensionMismatch("derivative(f, x) expects that x is a real number. Perhaps you meant gradient(f, x)?"))
derivative(f, x::Complex) = throw(DimensionMismatch("derivative(f, x) expects that x is a real number (does not support Wirtinger derivatives). Separate real and imaginary parts of the input."))

#####################
# result extraction #
Expand All @@ -78,9 +79,13 @@ derivative(f, x::AbstractArray) = throw(DimensionMismatch("derivative(f, x) expe
# non-mutating #
#--------------#

@inline extract_derivative(::Type{T}, y::Dual) where {T} = partials(T, y, 1)
@inline extract_derivative(::Type{T}, y::Real) where {T} = zero(y)
@inline extract_derivative(::Type{T}, y::Complex) where {T} = zero(y)
@inline extract_derivative(::Type{T}, y::Dual) where {T} = partials(T, y, 1)
@inline extract_derivative(::Type{T}, y::AbstractArray) where {T} = map(d -> extract_derivative(T,d), y)
@inline function extract_derivative(::Type{T}, y::Complex{TD}) where {T, TD <: Dual}
complex(partials(T, real(y), 1), partials(T, imag(y), 1))
end

# mutating #
#----------#
Expand Down
4 changes: 4 additions & 0 deletions test/DerivativeTest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,8 @@ end
@test_throws DimensionMismatch ForwardDiff.derivative(sum, fill(2pi, 3))
end

@testset "complex output" begin
@test ForwardDiff.derivative(x -> (1+im)*x, 0) == (1+im)
end

end # module