Skip to content

Commit 0137837

Browse files
Merge pull request #210 from j-fu/access_fx
Allow to access fx from jac_cache via public API
2 parents 7f00964 + 023c741 commit 0137837

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

docs/src/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ forwarddiff_color_jacobian!(J::AbstractMatrix{<:Number},
183183
`dx` is a pre-allocated output vector which is used to declare the output size,
184184
and thus allows for specifying a non-square Jacobian.
185185

186+
Also, it is possible retrieve the function value via `value(jac_cache)` or
187+
`value!(result, jac_cache)`
188+
189+
186190
If one is using an out-of-place function `f(x)`, then the alternative form
187191
ca be used:
188192

src/SparseDiffTools.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ export contract_color,
4242
autonum_hesvec, autonum_hesvec!,
4343
num_hesvecgrad, num_hesvecgrad!,
4444
auto_hesvecgrad, auto_hesvecgrad!,
45-
JacVec, HesVec, HesVecGrad
45+
JacVec, HesVec, HesVecGrad,
46+
value!
4647

4748
include("coloring/high_level.jl")
4849
include("coloring/backtracking_coloring.jl")

src/differentiation/compute_jacobian_ad.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ struct ForwardColorJacCache{T, T2, T3, T4, T5, T6}
88
chunksize::Int
99
end
1010

11+
ForwardDiff.value(cache::ForwardColorJacCache) = ForwardDiff.value.(cache.fx)
12+
value!(fx, cache::ForwardColorJacCache) = fx .= ForwardDiff.value.(cache.fx)
13+
1114
getsize(::Val{N}) where {N} = N
1215
getsize(N::Integer) = N
1316
void_setindex!(args...) = (setindex!(args...); return)

test/test_ad.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using SparseDiffTools
2-
using ForwardDiff: Dual, jacobian
2+
using ForwardDiff: Dual, jacobian, value
33
using SparseArrays, Test
44
using LinearAlgebra
55
using BlockBandedMatrices, ArrayInterfaceBlockBandedMatrices
@@ -212,6 +212,12 @@ jac_cache = ForwardColorJacCache(f, x, colorvec = repeat(1:3, 10), sparsity = _J
212212
forwarddiff_color_jacobian!(_J1, f, x, jac_cache)
213213
@test _J1 J
214214
@test fcalls == 1
215+
dx = similar(x)
216+
f(dx, x)
217+
@test value(jac_cache) == dx
218+
dx1 = similar(dx)
219+
value!(dx1, jac_cache)
220+
@test dx1 == dx
215221

216222
fcalls = 0
217223
_J1 = similar(_J)
@@ -227,6 +233,12 @@ jac_cache = ForwardColorJacCache(f, x, colorvec = repeat(1:3, 10), sparsity = _J
227233
forwarddiff_color_jacobian!(_denseJ1, f, x, jac_cache)
228234
@test _denseJ1 J
229235
@test fcalls == 1
236+
dx = similar(x)
237+
f(dx, x)
238+
@test value(jac_cache) == dx
239+
dx1 = similar(dx)
240+
value!(dx1, jac_cache)
241+
@test dx1 == dx
230242

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

0 commit comments

Comments
 (0)