You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It seems using ForwardColorJacCache does not work with anonymous functions or when the function fed to it is different from the function fed to forwarddiff_color_jacobian!. (Sorry I couldn't think of anything more specific for the title.)
Minimal example
Below, I'm simply trying to get the Jacobian of a (in-place) linear function x -> jac * x:
using SparseDiffTools, LinearAlgebra # SparseDiffTools v1.13.0
nt, nb =3, 4
jac =vcat((hcat((Diagonal(ones(nb)) for _ in1:nt)...) for _ in1:nt)...) # nt×nt blocks of diagonals
colors =matrix_colors(jac)
x =ones(nt*nb)
jac2 =similar(jac)
# works without ForwardColorJacCache:forwarddiff_color_jacobian!(jac2, (du,u)->mul!(du,jac,u), x, colorvec=colors, sparsity=jac)
# does not work with ForwardColorJacCache:
jac_cache =ForwardColorJacCache((du,u)->mul!(du,jac,u), x, colorvec=colors, sparsity=jac)
forwarddiff_color_jacobian!(jac2, (du,u)->mul!(du,jac,u), x, jac_cache)
The problem seems to go away when the function is not anonymous:
f!(du,u) =mul!(du,jac,u) # use f! instead of anonymous function
jac_cache =ForwardColorJacCache(f!, x, colorvec=colors, sparsity=jac)
forwarddiff_color_jacobian!(jac2, f!, x, jac_cache) # works!
But the problem comes back when the function used to build ForwardColorJacCache is a different instance than that used to call forwarddiff_color_jacobian:
g!(du,u) =mul!(du,jac,u) # A function with a different name
jac_cache =ForwardColorJacCache(g!, x, colorvec=colors, sparsity=jac) # use g!forwarddiff_color_jacobian!(jac2, f!, x, jac_cache) # use f! -> throws the same error
FWIW, I think I could use this second example with actually different functions that share the same sparsity for the Jacobian.
The text was updated successfully, but these errors were encountered:
It seems using
ForwardColorJacCache
does not work with anonymous functions or when the function fed to it is different from the function fed toforwarddiff_color_jacobian!
. (Sorry I couldn't think of anything more specific for the title.)Minimal example
Below, I'm simply trying to get the Jacobian of a (in-place) linear function
x -> jac * x
:stack trace
Additional notes
The problem seems to go away when the function is not anonymous:
But the problem comes back when the function used to build
ForwardColorJacCache
is a different instance than that used to callforwarddiff_color_jacobian
:FWIW, I think I could use this second example with actually different functions that share the same sparsity for the Jacobian.
The text was updated successfully, but these errors were encountered: