@@ -47,6 +47,67 @@ function g(x) # out-of-place
4747end
4848```
4949
50+ ## High Level API
51+
52+ We need to perform the following steps to utilize SparseDiffTools:
53+
54+ 1 . Specify a Sparsity Detection Algorithm. There are 3 possible choices currently:
55+ 1 . ` NoSparsityDetection ` : This will ignore any AD choice and compute the dense Jacobian
56+ 2 . ` JacPrototypeSparsityDetection ` : If you already know the sparsity pattern, you can
57+ specify it as ` JacPrototypeSparsityDetection(; jac_prototype=<sparsity pattern>) ` .
58+ 3 . ` SymbolicsSparsityDetection ` : This will use ` Symbolics.jl ` to automatically detect
59+ the sparsity pattern. (Note that ` Symbolics.jl ` must be explicitly loaded before
60+ using this functionality.)
61+ 2 . Now choose an AD backend from ` ADTypes.jl ` :
62+ 1 . If using a Non ` *Sparse* ` type, then we will not use sparsity detection.
63+ 2 . All other sparse AD types will internally compute the proper sparsity pattern, and
64+ try to exploit that.
65+ 3 . Now there are 2 options:
66+ 1 . Precompute the cache using ` sparse_jacobian_cache ` and use the ` sparse_jacobian ` or
67+ ` sparse_jacobian! ` functions to compute the Jacobian. This option is recommended if
68+ you are repeatedly computing the Jacobian for the same function.
69+ 2 . Directly use ` sparse_jacobian ` or ` sparse_jacobian! ` to compute the Jacobian. This
70+ option should be used if you are only computing the Jacobian once.
71+
72+ ``` julia
73+ using Symbolics
74+
75+ sd = SymbolicsSparsityDetection ()
76+ adtype = AutoSparseFiniteDiff ()
77+ x = rand (30 )
78+ y = similar (x)
79+
80+ # Option 1
81+ # # OOP Function
82+ cache = sparse_jacobian_cache (adtype, sd, g, x; fx= y) # Passing `fx` is needed if size(y) != size(x)
83+ J = sparse_jacobian (adtype, cache, g, x)
84+ # ## Or
85+ J_preallocated = similar (J)
86+ sparse_jacobian! (J_preallocated, adtype, cache, g, x)
87+
88+ # # IIP Function
89+ cache = sparse_jacobian_cache (adtype, sd, f, y, x)
90+ J = sparse_jacobian (adtype, cache, f, y, x)
91+ # ## Or
92+ J_preallocated = similar (J)
93+ sparse_jacobian! (J_preallocated, adtype, cache, f, y, x)
94+
95+ # Option 2
96+ # # OOP Function
97+ J = sparse_jacobian (adtype, sd, g, x)
98+ # ## Or
99+ J_preallocated = similar (J)
100+ sparse_jacobian! (J_preallocated, adtype, sd, g, x)
101+
102+ # # IIP Function
103+ J = sparse_jacobian (adtype, sd, f, y, x)
104+ # ## Or
105+ J_preallocated = similar (J)
106+ sparse_jacobian! (J_preallocated, adtype, sd, f, y, x)
107+ ```
108+
109+ ## Lower Level API
110+
50111For this function, we know that the sparsity pattern of the Jacobian is a
51112` Tridiagonal ` matrix. However, if we didn't know the sparsity pattern for
52113the Jacobian, we could use the ` Symbolics.jacobian_sparsity ` function to automatically
0 commit comments