Using ForwardDiff results in a stack overflow

Interesting that this pops up as a stack overflow error…

The point is that \ for SparseMatrixCSC is channeled to UMFPACK from SuiteSparse which is implemented in C and thus not able to work with ForwardDiff.Dual numbers.

That said, recently, the package Sparspak.jl was announced which is a pure Julia implementation of the classical Sparspak by George and Liu and thus provides an LU factorization working with general number types. The ExtendableSparse.jl package (shameless plug: I am the author…) provides the ExtendableSparseMatrix type which has a \ which falls back to Sparspak on number types not supported by SuiteSparse/UMFPACK .

So now we have:

using ExtendableSparse, ForwardDiff, LinearAlgebra, SparseArrays
function foo(x)
         r = [1,2,1];
         c = [1,2,2];
         v = [x[1],x[2],x[1]];
         rhs = [1.0,2.0];
         A = ExtendableSparseMatrix(sparse(r,c,v));
         LU = lu(A)
         y = LU \ rhs;
         return y
end

J = ForwardDiff.jacobian(foo,[1.,2.])
2×2 Matrix{Float64}:
 -1.0   0.5
  0.0  -0.5
1 Like