Skip to content

Commit 4a83b8c

Browse files
Merge pull request #137 from dingraha/jacobian_range_arg
Make `finite_difference_jacobian` work with range argument
2 parents cfee15e + 979b380 commit 4a83b8c

3 files changed

Lines changed: 36 additions & 10 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "FiniteDiff"
22
uuid = "6a86dc24-6348-571c-b903-95158fe2bd41"
3-
version = "2.12.1"
3+
version = "2.12.2"
44

55
[deps]
66
ArrayInterfaceCore = "30b0a656-2188-435a-8636-2ec0e6a096e2"

src/jacobians.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
mutable struct JacobianCache{CacheType1,CacheType2,CacheType3,ColorType,SparsityType,fdtype,returntype}
1+
mutable struct JacobianCache{CacheType1,CacheType2,CacheType3,CacheType4,ColorType,SparsityType,fdtype,returntype}
22
x1 :: CacheType1
3-
x2 :: CacheType1
4-
fx :: CacheType2
5-
fx1 :: CacheType3
3+
x2 :: CacheType2
4+
fx :: CacheType3
5+
fx1 :: CacheType4
66
colorvec :: ColorType
77
sparsity :: SparsityType
88
end
@@ -98,7 +98,7 @@ function JacobianCache(
9898
_fx = fx
9999
end
100100
_x2 = zero(_x1)
101-
JacobianCache{typeof(_x1),typeof(_fx),typeof(fx1),typeof(colorvec),typeof(sparsity),fdtype,returntype}(_x1,_x2,_fx,fx1,colorvec,sparsity)
101+
JacobianCache{typeof(_x1),typeof(_x2),typeof(_fx),typeof(fx1),typeof(colorvec),typeof(sparsity),fdtype,returntype}(_x1,_x2,_fx,fx1,colorvec,sparsity)
102102
end
103103

104104
function _make_Ji(::SparseMatrixCSC, rows_index,cols_index,dx,colorvec,color_i,nrows,ncols)
@@ -157,14 +157,14 @@ void_setindex!(args...) = (setindex!(args...); return)
157157
function finite_difference_jacobian(
158158
f,
159159
x,
160-
cache::JacobianCache{T1,T2,T3,cType,sType,fdtype,returntype},
160+
cache::JacobianCache{T1,T2,T3,T4,cType,sType,fdtype,returntype},
161161
f_in=nothing;
162162
relstep=default_relstep(fdtype, eltype(x)),
163163
absstep=relstep,
164164
colorvec = cache.colorvec,
165165
sparsity = cache.sparsity,
166166
jac_prototype = nothing,
167-
dir=true) where {T1,T2,T3,cType,sType,fdtype,returntype}
167+
dir=true) where {T1,T2,T3,T4,cType,sType,fdtype,returntype}
168168

169169
x1, fx, fx1 = cache.x1, cache.fx, cache.fx1
170170

@@ -325,13 +325,13 @@ function finite_difference_jacobian!(
325325
J,
326326
f,
327327
x,
328-
cache::JacobianCache{T1,T2,T3,cType,sType,fdtype,returntype},
328+
cache::JacobianCache{T1,T2,T3,T4,cType,sType,fdtype,returntype},
329329
f_in = nothing;
330330
relstep = default_relstep(fdtype, eltype(x)),
331331
absstep = relstep,
332332
colorvec = cache.colorvec,
333333
sparsity = cache.sparsity,
334-
dir = true) where {T1,T2,T3,cType,sType,fdtype,returntype}
334+
dir = true) where {T1,T2,T3,T4,cType,sType,fdtype,returntype}
335335

336336
m, n = size(J)
337337
_color = reshape(colorvec, axes(x)...)

test/finitedifftests.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,32 @@ J_ref = Matrix{Float64}(I,4,4)
409409
@test err_func(test_iipJac(J_ref, iipf, x, Val{:central}, eltype(x), iipf(similar(x),x)), J_ref) < 1e-8
410410
@test err_func(test_iipJac(J_ref, iipf, x, Val{:complex}, eltype(x), iipf(similar(x),x)), J_ref) < 1e-8
411411
end
412+
413+
# Range input for out-of-place function
414+
x = range(rand(), rand(); length=2)
415+
z = copy(x)
416+
y = similar(x)
417+
oopf(x) = @. x^2 + sin(x)
418+
J_ref = [2*x[1] + cos(x[1]) 0.0; 0.0 2*x[2] + cos(x[2])]
419+
J = zero(J_ref)
420+
oopff(x) = !all(x .<= z) ? error() : oopf(x)
421+
epsilon = zero(x)
422+
forward_cache = FiniteDiff.JacobianCache(x,Val{:forward},eltype(x))
423+
central_cache = FiniteDiff.JacobianCache(x,Val{:central},eltype(x))
424+
complex_cache = FiniteDiff.JacobianCache(x,Val{:complex},eltype(x))
425+
f_in = oopf(x)
426+
427+
@time @testset "Out-of-Place Jacobian real-valued tests with range input" begin
428+
@test err_func(FiniteDiff.finite_difference_jacobian(oopf, x, forward_cache), J_ref) < 1e-4
429+
@test err_func(FiniteDiff.finite_difference_jacobian(oopff, x, forward_cache, dir=-1), J_ref) < 1e-4
430+
@test_throws Any err_func(FiniteDiff.finite_difference_jacobian(oopff, x, forward_cache), J_ref) < 1e-4
431+
@test err_func(FiniteDiff.finite_difference_jacobian(oopf, x, forward_cache, relstep=sqrt(eps())), J_ref) < 1e-4
432+
@test err_func(FiniteDiff.finite_difference_jacobian(oopf, x, forward_cache, f_in), J_ref) < 1e-4
433+
@test err_func(FiniteDiff.finite_difference_jacobian(oopf, x, central_cache), J_ref) < 1e-8
434+
@test err_func(FiniteDiff.finite_difference_jacobian(oopf, x, Val{:central}), J_ref) < 1e-8
435+
@test err_func(FiniteDiff.finite_difference_jacobian(oopf, x, complex_cache), J_ref) < 1e-14
436+
end
437+
412438
# Hessian tests
413439

414440
f(x) = sin(x[1]) + cos(x[2])

0 commit comments

Comments
 (0)