# Using ForwardDiff results in a stack overflow

Hi, im using the ForwardDiff package. The following code is only a simplification of the real problem.
But I am getting there as well a stack overflow in the line of the LU decomposition.

``````using 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 = sparse(r,c,v);
LU = lu(A)
y = LU \ rhs;
return y
end
J = ForwardDiff.jacobian(foo,[1.,2.])``````

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

Hi,
ERROR: MethodError: Cannot `convert` an object of type