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()
Global sparsity detection over the entire input domain using SparseConnectivityTracer.jl. For use with ADTypes.jl's AbstractSparsityDetector
interface.
For local sparsity patterns, use TracerLocalSparsityDetector
.
Keyword arguments
gradient_pattern_type::Type
: Data structure used for bookkeeping of gradient sparsity patters, used injacobian_sparsity
. Supports concrete subtypes ofAbstactSet{<:Integer}
. Defaults toBitSet
.hessian_pattern_type::Type
: Data structure used for bookkeeping of Hessian sparsity patters, used inhessian_sparsity
. Supports concrete subtypes ofAbstractDict{I, AbstractSet{I}}
orAbstractSet{Tuple{I, I}}
, whereI <: Integer
. Defaults toDict{Int64, BitSet}
.shared_hessian_pattern::Bool
: Indicate whether second-order information in Hessian sparsity patterns always shares memory and whether operators are allowed to mutateHessianTracers
. Defaults tofalse
.
If support for further pattern representations is needed, please open a feature request: https://github.com/adrhill/SparseConnectivityTracer.jl/issues
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()
Local sparsity detection using SparseConnectivityTracer.jl. For use with ADTypes.jl's AbstractSparsityDetector
interface.
For global sparsity patterns, use TracerSparsityDetector
.
Keyword arguments
gradient_pattern_type::Type
: Data structure used for bookkeeping of gradient sparsity patters, used injacobian_sparsity
. Supports concrete subtypes ofAbstactSet{<:Integer}
. Defaults toBitSet
.hessian_pattern_type::Type
: Data structure used for bookkeeping of Hessian sparsity patters, used inhessian_sparsity
. Supports concrete subtypes ofAbstractDict{I, AbstractSet{I}}
orAbstractSet{Tuple{I, I}}
, whereI <: Integer
. Defaults toDict{Int64, BitSet}
.shared_hessian_pattern::Bool
: Indicate whether second-order information in Hessian sparsity patterns always shares memory and whether operators are allowed to mutateHessianTracers
. Defaults tofalse
.
If support for further pattern representations is needed, please open a feature request: https://github.com/adrhill/SparseConnectivityTracer.jl/issues
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
Memory allocation
For developers requiring the allocation of output buffers that support our tracers, we additionally provide
SparseConnectivityTracer.jacobian_eltype
— Functionjacobian_eltype(x, detector)
Act like eltype(x)
but return the matching number type used inside Jacobian sparsity detection.
SparseConnectivityTracer.hessian_eltype
— Functionhessian_eltype(x, detector)
Act like eltype(x)
but return the matching number type used inside Hessian sparsity detection.
SparseConnectivityTracer.jacobian_buffer
— Functionjacobian_buffer(x, detector)
Allocate a buffer similiar to x
with the required tracer type for Jacobian sparsity detection. Thin wrapper around similar
that doesn't expose internal types.
SparseConnectivityTracer.hessian_buffer
— Functionhessian_buffer(x, detector)
Allocate a buffer similiar to x
with the required tracer type for Hessian sparsity detection. Thin wrapper around similar
that doesn't expose internal types.
Please note that the DiffCache
from PreallocationTools.jl can be used to make caches compatible with both ForwardDiff.jl and SparseConnectivityTracer.jl.