JuliaDiff / JuliaDiff/ForwardDiff.jl

Error for Jacobian of in-place function that internally creates complex numbers

Open
#520 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
1k
Forks
160
PR merge metrics
No merged PRs in 30d

Description

Hi there,

MWE

newton_f(x, p) = x^p[1] - 1.0
newton_df(x, p)= p[1]*x^(p[1] - 1.0)
function newton_map(dz, z, p, n)
    z1 = z[1] + im*z[2]
    dz1 = newton_f(z1, p)/newton_df(z1, p)
    z1 = z1 - dz1
    dz[1] = real(z1)
    dz[2] = imag(z1)
    return
end

import ForwardDiff
s = [0.1, 0.2]
p = [3.0]
dum = deepcopy(s)
inplace_f_2args = (y, x) -> newton_map(y, x, p, 0)
cfg = ForwardDiff.JacobianConfig(inplace_f_2args, dum, s)
jac! = (J, u, p, t) -> ForwardDiff.jacobian!(
    J, inplace_f_2args, dum, u, cfg, Val{false}()
)

jac!(zeros(2, 2), s, p, 0)

        
ERROR: LoadError: MethodError: no method matching Int64(::ForwardDiff.Dual{ForwardDiff.Tag{var"#7#8", Float64}, Float64, 2})
Closest candidates are:
  (::Type{T})(::T) where T<:Number at boot.jl:760
  (::Type{T})(::AbstractChar) where T<:Union{AbstractChar, Number} at char.jl:50
  (::Type{T})(::BigInt) where T<:Union{Int128, Int16, Int32, Int64, Int8} at gmp.jl:356
  ...
Stacktrace:
  [1] convert(#unused#::Type{Int64}, x::ForwardDiff.Dual{ForwardDiff.Tag{var"#7#8", Float64}, Float64, 2})
    @ Base .\number.jl:7
  [2] _cpow(z::Complex{ForwardDiff.Dual{ForwardDiff.Tag{var"#7#8", Float64}, Float64, 2}}, p::ForwardDiff.Dual{ForwardDiff.Tag{var"#7#8", Float64}, Float64, 2})
    @ Base .\complex.jl:740
  [3] ^
    @ .\complex.jl:809 [inlined]
  [4] ^
    @ .\complex.jl:824 [inlined]
  [5] newton_f
    @ c:\Users\datse\.julia\dev\DynamicalSystemsBase\test\dynsys_types.jl:111 [inlined]
  [6] newton_map(dz::Vector{ForwardDiff.Dual{ForwardDiff.Tag{var"#7#8", Float64}, Float64, 2}}, z::Vector{ForwardDiff.Dual{ForwardDiff.Tag{var"#7#8", Float64}, Float64, 2}}, p::Vector{Float64}, 
n::Int64)
    @ Main c:\Users\datse\.julia\dev\DynamicalSystemsBase\test\dynsys_types.jl:115
  [7] (::var"#7#8")(y::Vector{ForwardDiff.Dual{ForwardDiff.Tag{var"#7#8", Float64}, Float64, 2}}, x::Vector{ForwardDiff.Dual{ForwardDiff.Tag{var"#7#8", Float64}, Float64, 2}})
    @ Main c:\Users\datse\.julia\dev\DynamicalSystemsBase\test\dynsys_types.jl:135
  [8] vector_mode_dual_eval
    @ C:\Users\datse\.julia\packages\ForwardDiff\QOqCN\src\apiutils.jl:44 [inlined]
  [9] vector_mode_jacobian!(result::Matrix{Float64}, f!::var"#7#8", y::Vector{Float64}, x::Vector{Float64}, cfg::ForwardDiff.JacobianConfig{ForwardDiff.Tag{var"#7#8", Float64}, Float64, 2, Tuple{Vector{ForwardDiff.Dual{ForwardDiff.Tag{var"#7#8", Float64}, Float64, 2}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{var"#7#8", Float64}, Float64, 2}}}})
    @ ForwardDiff C:\Users\datse\.julia\packages\ForwardDiff\QOqCN\src\jacobian.jl:172
 [10] jacobian!(result::Matrix{Float64}, f!::Function, y::Vector{Float64}, x::Vector{Float64}, cfg::ForwardDiff.JacobianConfig{ForwardDiff.Tag{var"#7#8", Float64}, Float64, 2, Tuple{Vector{ForwardDiff.Dual{ForwardDiff.Tag{var"#7#8", Float64}, Float64, 2}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{var"#7#8", Float64}, Float64, 2}}}}, ::Val{false})
    @ ForwardDiff C:\Users\datse\.julia\packages\ForwardDiff\QOqCN\src\jacobian.jl:78

I need to create the Jacobian in this format of (J, x, p, t), which is why all these anonymous functions got there. Making a function that does not internally make complex numbers works with the exact same syntax as above.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the MWE from test/dynsys_types.jl, then inspect ForwardDiff/src/apiutils.jl and src/jacobian.jl around vector-mode Jacobian evaluation. Compare this with the complex-power conversion failure shown in the Julia stack trace, and determine the expected behavior for in-place functions that create complex values. Done means the behavior is covered by a regression test or the limitation is clearly documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.