# Possibility to store outcome of \`ForwardDiff.jacobian\` already in sparse format

**URL:** https://discourse.julialang.org/t/possibility-to-store-outcome-of-forwarddiff-jacobian-already-in-sparse-format/102635
**Category:** Numerics
**Tags:** forwarddiff, sparse
**Created:** [August 9, 2023, 12:22pm UTC](https://discourse.julialang.org/t/possibility-to-store-outcome-of-forwarddiff-jacobian-already-in-sparse-format/102635 "2023-08-09T12:22:06Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![DanDoe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dandoe/32/52717_2.png) [@DanDoe](https://discourse.julialang.org/u/DanDoe)
#### Post date: [August 9, 2023, 12:22pm UTC](https://discourse.julialang.org/t/possibility-to-store-outcome-of-forwarddiff-jacobian-already-in-sparse-format/102635/1 "2023-08-09T12:22:06Z")

</div>

I use `ForwardDiff.jacobian` in the following way:

```julia
function _jacobian_ad_forward(p, t0, u0_ode, du_ode, config)
    J = ForwardDiff.jacobian(du_ode, u0_ode, config) do du_ode, u_ode
        f!(du_ode, u_ode, p, t0)
    end

    return J
end

```

where my Jacobian `J` is typically very sparse.  
I am aware of [SparseDiffTools.jl](https://github.com/JuliaDiff/SparseDiffTools.jl#sparsedifftoolsjl) which offers a sparse Jacobian computation based on a to-be-supplied sparsity pattern.  
To my understanding `SparseDiffTools.jl` does computations then only for the non-zero entries according to the sparsity pattern.

In my case, obtaining the sparsity pattern is not easy without major refactoring of the code behind `f`.  
Also, the actual computation of the Jacobian is not too time consuming, I am more worried about the memory requirements for storing entire `J`.

_Thus, my question: Is it possible to use `ForwardDiff.jacobian` (or a related function) such that only the non-zero outcomes are stored (in a sparse format)?_

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [August 9, 2023, 5:04pm UTC](https://discourse.julialang.org/t/possibility-to-store-outcome-of-forwarddiff-jacobian-already-in-sparse-format/102635/2 "2023-08-09T17:04:40Z")

</div>

> [@DanDoe](#):
>
> Thus, my question: Is it possible to use `ForwardDiff.jacobian` (or a related function) such that only the non-zero outcomes are stored (in a sparse format)?

Not really. You could setup a different accumulation using jvps though, basically calculate each vector, call sparse, and concatenate.

---

<div class="post-metadata">

### Author: ![brianguenter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brianguenter/32/29519_2.png) [@brianguenter](https://discourse.julialang.org/u/brianguenter)
#### Post date: [August 11, 2023, 6:19am UTC](https://discourse.julialang.org/t/possibility-to-store-outcome-of-forwarddiff-jacobian-already-in-sparse-format/102635/3 "2023-08-11T06:19:00Z")

</div>

[FastDifferentiation.jl](https://github.com/brianguenter/FastDifferentiation.jl) might help you. It’s good at detecting sparsity in Jacobians and stores them directly in a sparse format. It has limitations (see the docs) but if it works for your problem it should be efficient.
