Using ForwardDiff.gradient for numerical loop with static arrays and complex numbers

I’m having trouble getting ForwardDiff working with a numerical loop that involved complex numbers. Specifically I’m trying to use static arrays for speed (advised on my previous post), but it seems to be butting heads with the need for complex numbers. My full MWE below:

# Imports
begin
    using SpecialFunctions
    using ForwardDiff
    using StaticArrays
    using BenchmarkTools
end

# Functions
begin
    function CalculateSL(U, (L, μ, k, r, Ecm))
        dr = r[2] - r[1]
        len = size(r)[1]-1
        ur = zeros(eltype(U),3)
        ui = zeros(eltype(U),3)
        dur = zeros(eltype(U),3)
        dui = zeros(eltype(U),3)
        a = r[end-2]
        ur[2] = 1e-6
        ui[1] = 1e-12
        ui[2] = 1e-6
        for i in 3:len
            vreal = Ecm - U[i,1]
            vimag = -U[i,2]
            w = 2*μ/ħ^2*complex(vreal, vimag) - L*(L+1)/r[i]^2
            vreal = Ecm -U[i-1,1]
            vimag = -U[i-1,2]
            wmo = 2*μ/ħ^2*complex(vreal, vimag) - L*(L+1)/r[i]^2
            vreal = Ecm - U[i+1,1]
            vimag = -U[i+1,2]
            wpo = 2*μ/ħ^2*complex(vreal, vimag) - L*(L+1)/r[i]^2
            uval = (2*complex(ur[2],ui[2])-complex(ur[1],ui[1])-(dr^2/12.)*(10*w*complex(ur[2],ui[2])+wmo*complex(ur[1],ui[1])))/(1+(dr^2/12)*wpo)
            
            ur[3] = real.(uval)
            dur[3] = 0.5*(ur[3]-ur[1])/dr
            ui[3] = imag.(uval)
            dui[3] = 0.5*(ui[3]-ui[1])/dr

            ur[1:2] = ur[2:3]
            dur[1:2] = dur[2:3]
            ui[1:2] = ui[2:3]
            dui[1:2] = dui[2:3]
        end
        ua = complex(ur[2],ui[2])
        dua = complex(dur[3],dui[3])
        
        RL = ua / dua
        SLtop = Hminus(k, a, L) - RL*Hminusprime(k, a, L)
        SLbot = Hplus(k, a, L) - RL*Hplusprime(k, a, L)

        SL = SLtop/SLbot
        return SL
    end

    # Koning-Delaroche Potential
    function kd_params(A, Z, E)
        N = A - Z
        Ef = -11.23814 + 0.02646*A
        v1 = 59.3 - 21.0*(N-Z)/A - 0.024*A
        v2 = 0.007228 - (1.48e-6)*A
        v3 = 1.994e-5 - (2.e-8)*A
        v4 = 7.e-9
        Vo = v1*(1-v2*(E-Ef)+v3*(E-Ef)^2-v4*(E-Ef)^3)
        ro = (1.3039 - 0.4054/A^(1/3))*A^(1/3)
        ao = 0.6778 - (1.487e-4)*A
        w1 = 12.195 + 0.0167*A
        w2 = 73.55 + 0.0795*A
        Wro = w1*(E-Ef)^2/((E-Ef)^2+w2^2)
        d1 = 16 - 16*(N-Z)/A
        d2 = 0.0180 + 0.003802/(1+exp((A-156.)/8.))
        d3 = 11.5
        rw = (1.3424 - 0.01585*A^(1/3))*A^(1/3)
        aw = 0.5446 - (1.656e-4)*A
        Wso = d1*(E-Ef)^2*exp(-d2*(E-Ef))/((E-Ef)^2+d3^2)
        return Vo, ro, ao, Wro, Wso, rw, aw
    end
    function kd_pots(A, Z, E, r)
        Vo, ro, ao, Wro, Wso, rw, aw = kd_params(A, Z, E)
        Vr = -Vo ./(1 .+ exp.(-(ro.-r)./ao))
        W = -Wro ./(1 .+ exp.(-(ro.-r)./ao))
        Ws = -4 .* Wso .* exp.(-(rw.-r)./aw) ./(1 .+exp.(-(rw.-r)./aw)).^2
        return Vr, W, Ws
    end

    # Coulomb funcs
    function GL(k, r, L)
        return -k*r*sphericalbessely(L, k*r)
    end
    function FL(k, r, L)
        return k*r*sphericalbesselj(L, k*r)
    end 

    # Spherical Hankel functions
    function Hminus(k, r, L)
        return complex(GL(k, r, L), -FL(k, r, L))
    end
    function Hplus(k, r, L)
        return complex(GL(k, r, L), FL(k, r, L))
    end

    # Derivatives
    function  Hminusprime(k, r, L)
        return complex(ForwardDiff.derivative(x -> GL(k, x, L), r)[1], -ForwardDiff.derivative(x -> FL(k, x, L), r)[1])
    end
    function Hplusprime(k, r, L)
        return complex(ForwardDiff.derivative(x -> GL(k, x, L), r)[1], ForwardDiff.derivative(x -> FL(k, x, L), r)[1])
    end
end


# Set up particular scattering problem
A = 65
Z = 29
N = A - Z
E = 10
L = 1
Ecm = 9.848393154293218
μ = 925.3211722114523
k = 0.6841596644044445
r = Vector(LinRange(0, 20, 20))
const global ħ = 197.3269804

# General a potential from K-D
Vreal, Wv, Ws = kd_pots(A, Z, E, r);
U = hcat(Vreal, Wv + Ws);
U_static = [SVector(U[i, 1], U[i, 2]) for i in axes(U, 1)]

# Check SL value 
f = Base.Fix2(CalculateSL, (L, μ, k, r, Ecm))
# f(U)

# Try to calculate gradient
@btime df_dU = ForwardDiff.gradient(f, U_static)

This gives me the following:

julia> include("MWEs/fd_coreloop.jl")
ERROR: LoadError: MethodError: no method matching one(::Type{SVector{2, Float64}})

Closest candidates are:
  one(::Union{Type{T}, T}) where T<:AbstractString
   @ Base strings/basic.jl:262
  one(::Union{Type{P}, P}) where P<:Period
   @ Dates ~/julia-1.9.4/share/julia/stdlib/v1.9/Dates/src/periods.jl:52
  one(::LinearAlgebra.LowerTriangular)
   @ LinearAlgebra ~/julia-1.9.4/share/julia/stdlib/v1.9/LinearAlgebra/src/special.jl:370
  ...

Stacktrace:
  [1] macro expansion
    @ ~/.julia/packages/ForwardDiff/onEFt/src/partials.jl:0 [inlined]
  [2] single_seed(#unused#::Type{ForwardDiff.Partials{10, SVector{2, Float64}}}, #unused#::Val{1})
    @ ForwardDiff ~/.julia/packages/ForwardDiff/onEFt/src/partials.jl:9
  [3] macro expansion
    @ ~/.julia/packages/ForwardDiff/onEFt/src/apiutils.jl:0 [inlined]
  [4] construct_seeds(#unused#::Type{ForwardDiff.Partials{10, SVector{2, Float64}}})
    @ ForwardDiff ~/.julia/packages/ForwardDiff/onEFt/src/apiutils.jl:39
  [5] ForwardDiff.GradientConfig(f::Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, x::Vector{SVector{2, Float64}}, ::ForwardDiff.Chunk{10}, ::ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, SVector{2, Float64}})
    @ ForwardDiff ~/.julia/packages/ForwardDiff/onEFt/src/config.jl:121
  [6] ForwardDiff.GradientConfig(f::Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, x::Vector{SVector{2, Float64}}, ::ForwardDiff.Chunk{10})
    @ ForwardDiff ~/.julia/packages/ForwardDiff/onEFt/src/config.jl:121
  [7] ForwardDiff.GradientConfig(f::Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, x::Vector{SVector{2, Float64}})
    @ ForwardDiff ~/.julia/packages/ForwardDiff/onEFt/src/config.jl:121
  [8] gradient(f::Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, x::Vector{SVector{2, Float64}})
    @ ForwardDiff ~/.julia/packages/ForwardDiff/onEFt/src/gradient.jl:17
  [9] var"##core#345"()
    @ Main ~/.julia/packages/BenchmarkTools/QNsku/src/execution.jl:561
 [10] var"##sample#346"(::Tuple{}, __params::BenchmarkTools.Parameters)
    @ Main ~/.julia/packages/BenchmarkTools/QNsku/src/execution.jl:570
 [11] _lineartrial(b::BenchmarkTools.Benchmark, p::BenchmarkTools.Parameters; maxevals::Int64, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ BenchmarkTools ~/.julia/packages/BenchmarkTools/QNsku/src/execution.jl:187
 [12] _lineartrial(b::BenchmarkTools.Benchmark, p::BenchmarkTools.Parameters)
    @ BenchmarkTools ~/.julia/packages/BenchmarkTools/QNsku/src/execution.jl:182
 [13] #invokelatest#2
    @ ./essentials.jl:819 [inlined]
 [14] invokelatest
    @ ./essentials.jl:816 [inlined]
 [15] #lineartrial#46
    @ ~/.julia/packages/BenchmarkTools/QNsku/src/execution.jl:51 [inlined]
 [16] lineartrial
    @ ~/.julia/packages/BenchmarkTools/QNsku/src/execution.jl:50 [inlined]
 [17] tune!(b::BenchmarkTools.Benchmark, p::BenchmarkTools.Parameters; progressid::Nothing, nleaves::Float64, ndone::Float64, verbose::Bool, pad::String, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ BenchmarkTools ~/.julia/packages/BenchmarkTools/QNsku/src/execution.jl:300
 [18] tune!
    @ ~/.julia/packages/BenchmarkTools/QNsku/src/execution.jl:289 [inlined]
 [19] tune!(b::BenchmarkTools.Benchmark)
    @ BenchmarkTools ~/.julia/packages/BenchmarkTools/QNsku/src/execution.jl:289
 [20] top-level scope
    @ ~/.julia/packages/BenchmarkTools/QNsku/src/execution.jl:666
 [21] include(fname::String)
    @ Base.MainInclude ./client.jl:478
 [22] top-level scope
    @ REPL[4]:1
in expression starting at MWEs/fd_coreloop.jl:135

If I make the change in line 135 to remove the static version of U:

@btime df_dU = ForwardDiff.gradient(f, U)

the code runs, but I get the confusing output

20×2 Matrix{Complex{ForwardDiff.Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}, Float64, 10}}}:
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im  …  Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im  …  Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im  …  Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im  …  Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im

I’d also like help on why the above is so convoluted, and why all the values are 0.

Start by getting f(U_static) to run. When running just that function alone, the error message was quite clear. Don’t try to run it with gradient until the basic function works, as it’ll just make the stacktrace awful.

U is a Matrix{Float64} while U_static is a Vector{SVector{2,Float64}}. Your CalculateSL function has many lines that will not work when the input is a vector-of-vectors like U_static, which is why you get this error.

I wouldn’t expect the U_static version to be much faster than your original in this situation. You aren’t using the SVectors as vectors (it looks like you want to simply extract the values and use them in straight-line calculations, and there isn’t much way to make those faster) so most of the specialized code that would make them fast is irrelevant.

1 Like

In that case, I’d like to focus on the zero result for gradient rather than mess around with static arrays. My intent is to get a gradient value at each U, but I seem to be getting many values (all 0) for each U:

20×2 Matrix{Complex{ForwardDiff.Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}, Float64, 10}}}:
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im  …  Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im  …  Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im  …  Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im  …  Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im
 Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im     Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)+Dual{ForwardDiff.Tag{Base.Fix2{typeof(CalculateSL), Tuple{Int64, Float64, Float64, Vector{Float64}, Float64}}, Float64}}(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)*im

Am I using gradient or anonymous functions incorrectly here?

Just like ForwardDiff didn’t like complex inputs, it doesn’t seem to like complex outputs. I’m surprised it gives you what it does (instead of an error), but it’s supposed to give you a normal-ish Array{Float64} type of output (not Array{<:Dual}, which is an implementation detail).

Changing the last line of CalculateSL to return [real(SL), imag(SL)] and calling df_dU = ForwardDiff.jacobian(f, U) appears to give something sensible (you’ll need to decide whether it’s right, however). You can use reinterpret(reshape, Complex{eltype(df_dU)}, df_dU) to re-organize that real matrix into a complex vector.

1 Like