API Reference
SparseConnectivityTracer uses ADTypes.jl's interface for sparsity detection. In fact, the functions jacobian_sparsity
and hessian_sparsity
are re-exported from ADTypes.
ADTypes.jacobian_sparsity
— Functionjacobian_sparsity(f, x, sd::AbstractSparsityDetector)::AbstractMatrix{Bool}
jacobian_sparsity(f!, y, x, sd::AbstractSparsityDetector)::AbstractMatrix{Bool}
Use detector sd
to construct a (typically sparse) matrix S
describing the pattern of nonzeroes in the Jacobian of f
(resp. f!
) applied at x
(resp. (y, x)
).
ADTypes.hessian_sparsity
— Functionhessian_sparsity(f, x, sd::AbstractSparsityDetector)::AbstractMatrix{Bool}
Use detector sd
to construct a (typically sparse) matrix S
describing the pattern of nonzeroes in the Hessian of f
applied at x
.
To compute global sparsity patterns of f(x)
over the entire input domain x
, use
SparseConnectivityTracer.TracerSparsityDetector
— TypeTracerSparsityDetector <: ADTypes.AbstractSparsityDetector
Singleton struct for integration with the sparsity detection framework of ADTypes.jl.
Computes global sparsity patterns over the entire input domain. For local sparsity patterns at a specific input point, use TracerLocalSparsityDetector
.
Example
julia> using SparseConnectivityTracer
julia> detector = TracerSparsityDetector()
TracerSparsityDetector()
julia> jacobian_sparsity(diff, rand(4), detector)
3×4 SparseArrays.SparseMatrixCSC{Bool, Int64} with 6 stored entries:
1 1 ⋅ ⋅
⋅ 1 1 ⋅
⋅ ⋅ 1 1
julia> f(x) = x[1] + x[2]*x[3] + 1/x[4];
julia> hessian_sparsity(f, rand(4), detector)
4×4 SparseArrays.SparseMatrixCSC{Bool, Int64} with 3 stored entries:
⋅ ⋅ ⋅ ⋅
⋅ ⋅ 1 ⋅
⋅ 1 ⋅ ⋅
⋅ ⋅ ⋅ 1
To compute local sparsity patterns of f(x)
at a specific input x
, use
SparseConnectivityTracer.TracerLocalSparsityDetector
— TypeTracerLocalSparsityDetector <: ADTypes.AbstractSparsityDetector
Singleton struct for integration with the sparsity detection framework of ADTypes.jl.
Computes local sparsity patterns at an input point x
. For global sparsity patterns, use TracerSparsityDetector
.
Example
Local sparsity patterns are less convervative than global patterns and need to be recomputed for each input x
:
julia> using SparseConnectivityTracer
julia> detector = TracerLocalSparsityDetector()
TracerLocalSparsityDetector()
julia> f(x) = x[1] * x[2]; # J_f = [x[2], x[1]]
julia> jacobian_sparsity(f, [1, 0], detector)
1×2 SparseArrays.SparseMatrixCSC{Bool, Int64} with 1 stored entry:
⋅ 1
julia> jacobian_sparsity(f, [0, 1], detector)
1×2 SparseArrays.SparseMatrixCSC{Bool, Int64} with 1 stored entry:
1 ⋅
julia> jacobian_sparsity(f, [0, 0], detector)
1×2 SparseArrays.SparseMatrixCSC{Bool, Int64} with 0 stored entries:
⋅ ⋅
julia> jacobian_sparsity(f, [1, 1], detector)
1×2 SparseArrays.SparseMatrixCSC{Bool, Int64} with 2 stored entries:
1 1
TracerLocalSparsityDetector
can compute sparsity patterns of functions that contain comparisons and ifelse
statements:
julia> f(x) = x[1] > x[2] ? x[1:3] : x[2:4];
julia> jacobian_sparsity(f, [1, 2, 3, 4], TracerLocalSparsityDetector())
3×4 SparseArrays.SparseMatrixCSC{Bool, Int64} with 3 stored entries:
⋅ 1 ⋅ ⋅
⋅ ⋅ 1 ⋅
⋅ ⋅ ⋅ 1
julia> jacobian_sparsity(f, [2, 1, 3, 4], TracerLocalSparsityDetector())
3×4 SparseArrays.SparseMatrixCSC{Bool, Int64} with 3 stored entries:
1 ⋅ ⋅ ⋅
⋅ 1 ⋅ ⋅
⋅ ⋅ 1 ⋅
julia> f(x) = x[1] + max(x[2], x[3]) * x[3] + 1/x[4];
julia> hessian_sparsity(f, [1.0, 2.0, 3.0, 4.0], TracerLocalSparsityDetector())
4×4 SparseArrays.SparseMatrixCSC{Bool, Int64} with 2 stored entries:
⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅
⋅ ⋅ 1 ⋅
⋅ ⋅ ⋅ 1