From b6553b842c407eda69361299dd3eb1a4fd2566ed Mon Sep 17 00:00:00 2001 From: Avik Pal Date: Fri, 15 Dec 2023 16:35:01 -0500 Subject: [PATCH] Make it an option --- src/highlevel/coloring.jl | 4 ++-- src/highlevel/common.jl | 14 +++++++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/highlevel/coloring.jl b/src/highlevel/coloring.jl index c11ddabb..3217533b 100644 --- a/src/highlevel/coloring.jl +++ b/src/highlevel/coloring.jl @@ -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) @@ -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) diff --git a/src/highlevel/common.jl b/src/highlevel/common.jl index 45c380e5..1f609fed 100644 --- a/src/highlevel/common.jl +++ b/src/highlevel/common.jl @@ -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. @@ -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