# ANN: Sparspak.jl 0.3.0 works with ForwardDiff.Dual and other number types

**URL:** <https://discourse.julialang.org/t/ann-sparspak-jl-0-3-0-works-with-forwarddiff-dual-and-other-number-types/87948>\
**Category:** Package Announcements\
**Created:** [September 28, 2022, 8:11pm UTC](https://discourse.julialang.org/t/ann-sparspak-jl-0-3-0-works-with-forwarddiff-dual-and-other-number-types/87948 "2022-09-28T20:11:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![j-fu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j-fu/32/11373_2.png) [@j-fu](https://discourse.julialang.org/u/j-fu)\
**Post date:** [September 28, 2022, 8:11pm UTC](https://discourse.julialang.org/t/ann-sparspak-jl-0-3-0-works-with-forwarddiff-dual-and-other-number-types/87948/1 "2022-09-28T20:11:36Z")

</div>

This is a follow-up to the [Announcement of Sparspak.jl](https://discourse.julialang.org/t/ann-sparspak-suite-of-solvers-for-linear-algebraic-equations-in-julia/82461/2), a sparse direct solver package resulting from the translation of the Sparspak Fortran90 library to Julia performed by @PetrKryslUCSD. With consent of the Fortran code authors Alan George and Joseph Liu, Sparspak.jl is released under an MIT license.

Starting with version 0.3.0, the internal calls to BLAS/LAPACK for number types not supported by the corresponding binary libraries are dispatched to specifically tailored, non-allocating implementations in Julia. This allows to auto-differentiate through a sparse matrix solve.

Here is an example:

```julia
using LinearAlgebra
using Sparspak
using Sparspak.SparseSolver: SparseSolver, solve!
using Sparspak.SpkProblem: Problem, insparse!, infullrhs!
using SparseArrays
using ForwardDiff

function tridiagonal(p,n)
    T=typeof(p)
    a=fill(T(-0.1),n-1)
    b=1.0.+p.*collect(1:n)
    Tridiagonal(a,b,a)
end
    

f(x)=sum(tridiagonal(x,20)\ones(20))
df(x)=ForwardDiff.derivative(f,x)

function g(x)
    n=20
    M=sparse(tridiagonal(x,n))
    pr = Problem(n,n,nnz(M),zero(x))
    insparse!(pr, M)
    infullrhs!(pr,ones(n))
    solve!(SparseSolver(pr))    
    sum(pr.x)
end

dg(x)=ForwardDiff.derivative(g,x)

```

```julia
julia> f(1.0)
2.7401598813234886

julia> df(1.0)
-2.1873699271126963

julia> g(1.0)
2.7401598813234886

julia> dg(1.0)
-2.1873699271126963

```
