# Sparse matrix error with forward diff

**URL:** <https://discourse.julialang.org/t/sparse-matrix-error-with-forward-diff/108170>\
**Category:** General Usage\
**Tags:** forwarddiff, sparse\
**Created:** [December 30, 2023, 1:39am UTC](https://discourse.julialang.org/t/sparse-matrix-error-with-forward-diff/108170 "2023-12-30T01:39:08Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 30, 2023, 8:44pm UTC](https://discourse.julialang.org/t/sparse-matrix-error-with-forward-diff/108170/4 "2023-12-30T20:44:23Z")

</div>

> [@slwu89](#):
>
> ```julia
> [4] lu(A::SparseMatrixCSC{ForwardDiff.Dual{ForwardDiff.Tag{var"#7#8", Float64}, Float64, 2}, Int64}; check::Bool) (repeats 21635 times)
> @ SparseArrays.UMFPACK ~/.julia/juliaup/julia-1.9.4+0.x64.linux.gnu/share/julia/stdlib/v1.9/SparseArrays/src/solvers/umfpack.jl:398
> [5] logabsdet(A::SparseMatrixCSC{ForwardDiff.Dual{ForwardDiff.Tag{var"#7#8", Float64}, Float64, 2}, Int64})
> @ LinearAlgebra ~/.julia/juliaup/julia-1.9.4+0.x64.linux.gnu/share/julia/stdlib/v1.9/LinearAlgebra/src/generic.jl:1663
> 
> ```

The problem is that `logabsdet` calls `lu` factorization, but LU factorization for sparse matrices in Julia uses UMFPACK from [SuiteSparse](https://people.engr.tamu.edu/davis/suitesparse.html), which is an external library that only supports the standard hardware floating-point types. It won’t work for dual numbers, hence it won’t work with ForwardDiff.

(In particular, [this line](https://github.com/JuliaSparse/SparseArrays.jl/blob/feb54ee5e49008bd157227099cafe604a67c36fb/src/solvers/umfpack.jl#L404) is calling `float` to convert the matrix to a floating-point type, but this hits a dispatch loop because the `Dual` numbers already _are_ floating-point, and it overflows the stack. It really should be fixed to give a more comprehensible error message.)

In general, I find that automatic differentiation through sparse-matrix computations is almost always problematic, and one often has to write derivative rules out by hand. (See also [this discussion](https://discourse.julialang.org/t/zygote-jl-how-to-get-the-gradient-of-sparse-matrix/59067/6).) Fortunately, it is quite straightforward to take the derivative of a log determinant, as explained in [these notes](https://rawcdn.githack.com/mitmath/matrixcalc/b08435612045b17745707f03900e4e4187a6f489/notes/determinant_and_inverse.html) and [this lecture](https://ocw.mit.edu/courses/18-s096-matrix-calculus-for-machine-learning-and-beyond-january-iap-2023/resources/ocw_18s096_lecture05-part1-new_2023jan27_mp4/) from our [_Matrix Calculus_ course at MIT](https://ocw.mit.edu/courses/18-s096-matrix-calculus-for-machine-learning-and-beyond-january-iap-2023/).

---

_[View the full topic](https://discourse.julialang.org/t/sparse-matrix-error-with-forward-diff/108170)._
