Skip to content

Commit

Permalink
Make it an option
Browse files Browse the repository at this point in the history
  • Loading branch information
avik-pal committed Dec 15, 2023
1 parent 5727eed commit b6553b8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/highlevel/coloring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function (alg::ApproximateJacobianSparsity)(ad::AutoSparseFiniteDiff, f::F, x; f
cache = FiniteDiff.JacobianCache(x, fx)
J = fill!(similar(fx, length(fx), length(x)), 0)
x_ = similar(x)
ε = eps(eltype(x)) * 100
ε = ifelse(alg.epsilon === nothing, eps(eltype(x)) * 100, alg.epsilon)
for _ in 1:ntrials
randn!(rng, x_)
J_cache = FiniteDiff.finite_difference_jacobian(f, x, cache)
Expand All @@ -95,7 +95,7 @@ function (alg::ApproximateJacobianSparsity)(ad::AutoSparseFiniteDiff, f!::F, fx,
J = fill!(similar(fx, length(fx), length(x)), 0)
J_cache = similar(J)
x_ = similar(x)
ε = eps(eltype(x)) * 100
ε = ifelse(alg.epsilon === nothing, eps(eltype(x)) * 100, alg.epsilon)
for _ in 1:ntrials
randn!(rng, x_)
FiniteDiff.finite_difference_jacobian!(J_cache, f!, x_, cache)
Expand Down
14 changes: 9 additions & 5 deletions src/highlevel/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ end

"""
ApproximateJacobianSparsity(; ntrials = 5, rng = Random.default_rng(),
alg = GreedyD1Color())
epsilon = nothing, alg = GreedyD1Color())
Use `ntrials` random vectors to compute the sparsity pattern of the Jacobian. This is an
approximate method and the sparsity pattern may not be exact.
Expand All @@ -124,17 +124,21 @@ approximate method and the sparsity pattern may not be exact.
- `ntrials`: The number of random vectors to use for computing the sparsity pattern
- `rng`: The random number generator used for generating the random vectors
- `alg`: The algorithm used for computing the matrix colors
- `epsilon`: For Finite Differencing based Jacobian Approximations, any number smaller
than `epsilon` is considered to be zero. If `nothing` is specified, then this value
is calculated as `100 * eps(eltype(x))`
"""
struct ApproximateJacobianSparsity{R <: AbstractRNG,
A <: ArrayInterface.ColoringAlgorithm} <: AbstractSparsityDetection
struct ApproximateJacobianSparsity{R <: AbstractRNG, A <: ArrayInterface.ColoringAlgorithm,
E} <: AbstractSparsityDetection
ntrials::Int
rng::R
alg::A
epsilon::E
end

function ApproximateJacobianSparsity(; ntrials::Int = 3,
function ApproximateJacobianSparsity(; ntrials::Int = 3, epsilon = nothing,
rng::AbstractRNG = Random.default_rng(), alg = GreedyD1Color())
return ApproximateJacobianSparsity(ntrials, rng, alg)
return ApproximateJacobianSparsity(ntrials, rng, alg, epsilon)
end

# No one should be using this currently
Expand Down

0 comments on commit b6553b8

Please sign in to comment.