Skip to content

Allow to access fx from jac_cache via public API #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ forwarddiff_color_jacobian!(J::AbstractMatrix{<:Number},
`dx` is a pre-allocated output vector which is used to declare the output size,
and thus allows for specifying a non-square Jacobian.

Also, it is possible retrieve the function value via `value(jac_cache)` or
`value!(result, jac_cache)`


If one is using an out-of-place function `f(x)`, then the alternative form
ca be used:

Expand Down
3 changes: 2 additions & 1 deletion src/SparseDiffTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ export contract_color,
autonum_hesvec, autonum_hesvec!,
num_hesvecgrad, num_hesvecgrad!,
auto_hesvecgrad, auto_hesvecgrad!,
JacVec, HesVec, HesVecGrad
JacVec, HesVec, HesVecGrad,
value!

include("coloring/high_level.jl")
include("coloring/backtracking_coloring.jl")
Expand Down
3 changes: 3 additions & 0 deletions src/differentiation/compute_jacobian_ad.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ struct ForwardColorJacCache{T, T2, T3, T4, T5, T6}
chunksize::Int
end

ForwardDiff.value(cache::ForwardColorJacCache) = ForwardDiff.value.(cache.fx)
value!(fx, cache::ForwardColorJacCache) = fx .= ForwardDiff.value.(cache.fx)

getsize(::Val{N}) where {N} = N
getsize(N::Integer) = N
void_setindex!(args...) = (setindex!(args...); return)
Expand Down
14 changes: 13 additions & 1 deletion test/test_ad.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using SparseDiffTools
using ForwardDiff: Dual, jacobian
using ForwardDiff: Dual, jacobian, value
using SparseArrays, Test
using LinearAlgebra
using BlockBandedMatrices, ArrayInterfaceBlockBandedMatrices
Expand Down Expand Up @@ -212,6 +212,12 @@ jac_cache = ForwardColorJacCache(f, x, colorvec = repeat(1:3, 10), sparsity = _J
forwarddiff_color_jacobian!(_J1, f, x, jac_cache)
@test _J1 ≈ J
@test fcalls == 1
dx = similar(x)
f(dx, x)
@test value(jac_cache) == dx
dx1 = similar(dx)
value!(dx1, jac_cache)
@test dx1 == dx

fcalls = 0
_J1 = similar(_J)
Expand All @@ -227,6 +233,12 @@ jac_cache = ForwardColorJacCache(f, x, colorvec = repeat(1:3, 10), sparsity = _J
forwarddiff_color_jacobian!(_denseJ1, f, x, jac_cache)
@test _denseJ1 ≈ J
@test fcalls == 1
dx = similar(x)
f(dx, x)
@test value(jac_cache) == dx
dx1 = similar(dx)
value!(dx1, jac_cache)
@test dx1 == dx

_Jt = similar(Tridiagonal(J))
forwarddiff_color_jacobian!(_Jt, f, x, colorvec = repeat(1:3, 10), sparsity = _Jt)
Expand Down