Diffractor.jl icon indicating copy to clipboard operation
Diffractor.jl copied to clipboard

Incorrect Jacobian calculation

Open mattsignorelli opened this issue 1 year ago • 0 comments

using ForwardDiff
using Diffractor: DiffractorForwardBackend
import AbstractDifferentiation as AD
 
function test(z0)
  z = Vector{eltype(z0)}(undef, length(z0))
  L  =  0.5 
  k1 = 0.36
  z[1] =  cos(sqrt(k1)*L)*z0[1]+1/sqrt(k1)*sin(sqrt(k1)*L)*z0[2]
  z[2] =  -sqrt(k1)*sin(sqrt(k1)*L)*z0[1]+cos(sqrt(k1)*L)*z0[2]
  return z
end

m(z) = test([z[1], z[2]])

Then:

julia> j = AD.jacobian(DiffractorForwardBackend(), m, zeros(2)) |> only
2×2 Matrix{Float64}:
  0.29552   0.492534
 -0.177312  0.29552

julia> j = AD.jacobian(AD.ForwardDiffBackend(), m, zeros(2)) |> only
2×2 Matrix{Float64}:
  0.955336  0.492534
 -0.177312  0.955336

ForwardDiff.jl's Jacobian was also cross-checked with GTPSA.jl

mattsignorelli avatar Jun 09 '24 16:06 mattsignorelli