Skip to content

Commit a705f00

Browse files
committed
improve default for sparsity
1 parent 4525275 commit a705f00

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

src/differentiation/compute_jacobian_ad.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function forwarddiff_color_jacobian!(J::AbstractMatrix{<:Number},
8181
x::AbstractArray{<:Number};
8282
dx = nothing,
8383
color = eachindex(x),
84-
sparsity = nothing)
84+
sparsity = J isa SparseMatrixCSC ? J : nothing)
8585
forwarddiff_color_jacobian!(J,f,x,ForwardColorJacCache(f,x,dx=dx,color=color,sparsity=sparsity))
8686
end
8787

test/test_ad.jl

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,27 @@ _J = sparse(J)
3131

3232
fcalls = 0
3333
_J1 = similar(_J)
34-
forwarddiff_color_jacobian!(_J1, f, x, color = repeat(1:3,10), sparsity=_J1)
34+
forwarddiff_color_jacobian!(_J1, f, x, color = repeat(1:3,10))
3535
@test _J1 J
3636
@test fcalls == 1
3737

3838
fcalls = 0
39-
jac_cache = ForwardColorJacCache(f,x,color = repeat(1:3,10), sparsity=_J1)
39+
jac_cache = ForwardColorJacCache(f,x,color = repeat(1:3,10), sparsity = _J1)
4040
forwarddiff_color_jacobian!(_J1, f, x, jac_cache)
4141
@test _J1 J
4242
@test fcalls == 1
43+
44+
fcalls = 0
45+
_J1 = similar(_J)
46+
_denseJ1 = collect(_J1)
47+
forwarddiff_color_jacobian!(_denseJ1, f, x, color = repeat(1:3,10), sparsity = _J1)
48+
@test _denseJ1 J
49+
@test fcalls == 1
50+
51+
fcalls = 0
52+
_J1 = similar(_J)
53+
_denseJ1 = collect(_J1)
54+
jac_cache = ForwardColorJacCache(f,x,color = repeat(1:3,10), sparsity = _J1)
55+
forwarddiff_color_jacobian!(_denseJ1, f, x, jac_cache)
56+
@test _denseJ1 J
57+
@test fcalls == 1

test/test_integration.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ J = DiffEqDiffTools.finite_difference_jacobian(f, rand(30))
3838
#Jacobian computed with coloring vectors
3939
fcalls = 0
4040
_J = similar(true_jac)
41-
DiffEqDiffTools.finite_difference_jacobian!(_J, f, rand(30), color = colors, sparsity=sparse(_J))
41+
DiffEqDiffTools.finite_difference_jacobian!(_J, f, rand(30), color = colors)
4242
@test fcalls == 4
4343
@test _J J
4444

4545
fcalls = 0
4646
_J = similar(true_jac)
4747
_denseJ = collect(_J)
48-
DiffEqDiffTools.finite_difference_jacobian!(_denseJ, f, rand(30), color = colors, sparsity=sparse(_J))
48+
DiffEqDiffTools.finite_difference_jacobian!(_denseJ, f, rand(30), color = colors, sparsity=_J)
4949
@test fcalls == 4
5050
@test _denseJ J

0 commit comments

Comments
 (0)