YingboMa / ForwardDiff2.jl

Other
52 stars 4 forks source link

promotion of types, unable to change any argument #46

Closed francescoalemanno closed 3 years ago

francescoalemanno commented 4 years ago

I cant's seem to make this kind of code work with ForwardDiff2


function gaussian(f,N)
    a=-8.38149 # exp(-x^2/2)/sqrt(2π) == eps(1.0)
    b=8.38149
    dx=(b-a)/(N)
    Δ=(b-a)/(2N)
    x=a+Δ
    I=(exp(-x*x/2).*f(x)).*dx
    for i in 2:N
        x=a+Δ*(2i-1)
        I=I.+(exp(-x*x/2).*f(x)).*dx
    end
    I/sqrt(2*π)
end

using ForwardDiff2: DI

f(α) = gaussian(x->tanh(x+α),100)

f(2.0) # 0.8646647167633871

DI(f)(2.0)

using StaticArrays

g(α) = gaussian(x->@SVector([tanh(2x+α),tanh(x+α)]),100)

g(2.0) #  [0.6389517914690043, 0.8646647167633871]

DI(g)(2.0)

error message

promotion of types Float64 and ForwardDiff2.Dual{ForwardDiff2.Tag{Nothing},Float64,Float64} failed to change any arguments
error(::String, ::String, ::String) at error.jl:42
sametype_error(::Tuple{Float64,ForwardDiff2.Dual{ForwardDiff2.Tag{Nothing},Float64,Float64}}) at promotion.jl:306
...

my setup

Julia Version 1.4.1
Commit 381693d3df* (2020-04-14 17:20 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: AMD Ryzen Threadripper 3970X 32-Core Processor 
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-8.0.1 (ORCJIT, znver1)
Environment:
  JULIA_EDITOR = "C:\Users\User\AppData\Local\atom\app-1.45.0\atom.exe"  -a
  JULIA_NUM_THREADS = 64